1 | #!/usr/bin/env node
|
2 |
|
3 | if (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
|
9 | require('../global');
|
10 |
|
11 | function exitWithErrorMessage(msg) {
|
12 | console.log(msg);
|
13 | console.log();
|
14 | process.exit(1);
|
15 | }
|
16 |
|
17 | if (process.argv.length < 3) {
|
18 | exitWithErrorMessage('ShellJS: missing argument (script name)');
|
19 | }
|
20 |
|
21 | var args,
|
22 | scriptName = process.argv[2];
|
23 | env['NODE_PATH'] = __dirname + '/../..';
|
24 |
|
25 | if (!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 |
|
32 | if (!test('-f', scriptName)) {
|
33 | exitWithErrorMessage('ShellJS: script not found ('+scriptName+')');
|
34 | }
|
35 |
|
36 | args = process.argv.slice(3);
|
37 |
|
38 | for (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 |
|
44 | var path = require('path');
|
45 | var extensions = require('interpret').extensions;
|
46 | var rechoir = require('rechoir');
|
47 | rechoir.prepare(extensions, scriptName);
|
48 | require(require.resolve(path.resolve(process.cwd(), scriptName)));
|