diff --git a/bin/shjs b/bin/shjs index aa9259a..aecf469 100755 --- a/bin/shjs +++ b/bin/shjs @@ -4,40 +4,4 @@ if (require.main !== module) { throw new Error('Executable-only module should not be required'); } -// we must import global ShellJS methods after the require.main check to prevent the global -// namespace from being polluted if the error is caught -require('shelljs/global'); - -function exitWithErrorMessage(msg) { - console.log(msg); - console.log(); - process.exit(1); -} - -if (process.argv.length < 3) { - exitWithErrorMessage('ShellJS: missing argument (script name)'); -} - -var args, - scriptName = process.argv[2]; -env['NODE_PATH'] = __dirname + '/../..'; - -if (!scriptName.match(/\.js/) && !scriptName.match(/\.coffee/)) { - if (test('-f', scriptName + '.js')) - scriptName += '.js'; - if (test('-f', scriptName + '.coffee')) - scriptName += '.coffee'; -} - -if (!test('-f', scriptName)) { - exitWithErrorMessage('ShellJS: script not found ('+scriptName+')'); -} - -args = process.argv.slice(3); -process.argv = [process.argv[0], process.argv[1], ...args]; - -var path = require('path'); -var extensions = require('interpret').extensions; -var rechoir = require('rechoir'); -rechoir.prepare(extensions, scriptName); -require(require.resolve(path.resolve(process.cwd(), scriptName))); +require('../index'); diff --git a/index.js b/index.js new file mode 100644 index 0000000..f255be3 --- /dev/null +++ b/index.js @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +/* globals test, env */ + +const path = require('path'); + +require('shelljs/global'); + +function exitWithErrorMessage(msg) { + console.log(msg); + console.log(); + process.exit(1); +} + +if (process.argv.length < 3) { + exitWithErrorMessage('ShellJS: missing argument (script name)'); +} + +var args; + var scriptName = process.argv[2]; +env.NODE_PATH = path.join(__dirname, '..', '..'); + +if (!scriptName.match(/\.js/) && !scriptName.match(/\.coffee/)) { + if (test('-f', scriptName + '.js')) { + scriptName += '.js'; + } + if (test('-f', scriptName + '.coffee')) { + scriptName += '.coffee'; + } +} + +if (!test('-f', scriptName)) { + exitWithErrorMessage('ShellJS: script not found (' + scriptName + ')'); +} + +args = process.argv.slice(3); +process.argv = [process.argv[0], process.argv[1], ...args]; + +var extensions = require('interpret').extensions; +var rechoir = require('rechoir'); + +rechoir.prepare(extensions, scriptName); +// eslint-disable-next-line import/no-dynamic-require +require(require.resolve(path.resolve(process.cwd(), scriptName))); diff --git a/package.json b/package.json index 95a838b..4aa6d40 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "url": "https://github.com/shelljs/shjs/issues" }, "files": [ + "index.js", "bin" ], "homepage": "https://github.com/shelljs/shjs#readme",