UNPKG

1.29 kBPlain TextView Raw
1#!/usr/bin/env node
2
3if (require.main !== module) {
4 throw new Error('Executable-only module should not be required');
5}
6
7// we must import global ShellJS methods after the require.main check to prevent the global
8// namespace from being polluted if the error is caught
9require('../global');
10
11function exitWithErrorMessage(msg) {
12 console.log(msg);
13 console.log();
14 process.exit(1);
15}
16
17if (process.argv.length < 3) {
18 exitWithErrorMessage('ShellJS: missing argument (script name)');
19}
20
21var args,
22 scriptName = process.argv[2];
23env['NODE_PATH'] = __dirname + '/../..';
24
25if (!scriptName.match(/\.js/) && !scriptName.match(/\.coffee/)) {
26 if (test('-f', scriptName + '.js'))
27 scriptName += '.js';
28 if (test('-f', scriptName + '.coffee'))
29 scriptName += '.coffee';
30}
31
32if (!test('-f', scriptName)) {
33 exitWithErrorMessage('ShellJS: script not found ('+scriptName+')');
34}
35
36args = process.argv.slice(3);
37
38for (var i = 0, l = args.length; i < l; i++) {
39 if (args[i][0] !== "-"){
40 args[i] = '"' + args[i] + '"'; // fixes arguments with multiple words
41 }
42}
43
44var path = require('path');
45var extensions = require('interpret').extensions;
46var rechoir = require('rechoir');
47rechoir.prepare(extensions, scriptName);
48require(require.resolve(path.resolve(process.cwd(), scriptName)));