From a4fba1ae0ed15899c1d33ea7f9ad7f9e590be032 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Fri, 9 May 2025 00:20:38 -0700 Subject: [PATCH] fix: support scripts with arguments This supports scripts with arguments. This was clearly working at some point in history (there is code which obviously intends to support this), but it's probably been broken since we added the "rechoir" module as a dependency. --- bin/shjs | 7 +------ test/resources/shjs/echo-args.js | 1 + test/shjs.js | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 test/resources/shjs/echo-args.js diff --git a/bin/shjs b/bin/shjs index 9503b0a..aa9259a 100755 --- a/bin/shjs +++ b/bin/shjs @@ -34,12 +34,7 @@ if (!test('-f', scriptName)) { } args = process.argv.slice(3); - -for (var i = 0, l = args.length; i < l; i++) { - if (args[i][0] !== "-"){ - args[i] = '"' + args[i] + '"'; // fixes arguments with multiple words - } -} +process.argv = [process.argv[0], process.argv[1], ...args]; var path = require('path'); var extensions = require('interpret').extensions; diff --git a/test/resources/shjs/echo-args.js b/test/resources/shjs/echo-args.js new file mode 100644 index 0000000..340f702 --- /dev/null +++ b/test/resources/shjs/echo-args.js @@ -0,0 +1 @@ +console.log(...process.argv); diff --git a/test/shjs.js b/test/shjs.js index c251eba..fd9897d 100644 --- a/test/shjs.js +++ b/test/shjs.js @@ -6,13 +6,19 @@ const shell = require('shelljs'); const binPath = path.resolve(__dirname, '../bin/shjs'); -function runWithShjs(name) { +function runWithShjs(name, ...args) { // prefix with 'node ' for Windows, don't prefix for unix const execPath = process.platform === 'win32' ? `${JSON.stringify(shell.config.execPath)} ` : ''; const script = path.resolve(__dirname, 'resources', 'shjs', name); - return shell.exec(`${execPath}${binPath} ${script}`, { silent: true }); + let argString = args.map(arg => JSON.stringify(arg)).join(' '); + if (argString) { + argString = ' ' + argString; + } + return shell.exec(`${execPath}${binPath} ${script}${argString}`, { + silent: true + }); } // @@ -61,6 +67,13 @@ test('CoffeeScript extension inference', t => { t.falsy(result.stderr); }); +test('Multiple arguments', t => { + const result = runWithShjs('echo-args.js', 'hello', 'there'); + t.is(result.code, 0); + t.regex(result.stdout, /hello there\n$/); + t.falsy(result.stderr); +}); + // // Invalids //