From 219a95d380f3088a5dd82e0e8902b384a3ce5c82 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Fri, 9 May 2025 00:29:40 -0700 Subject: [PATCH] refactor: enable code linting This refactors to a '.js' source file, which enables code linting. This also fixes the lint errors to use an updated style. This also enables test coverage for index.js (which now holds all the business logic). --- bin/shjs | 38 +------------------------------------- index.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 index.js 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",