Skip to content

Commit b1aba2f

Browse files
committed
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.
1 parent 6424cf3 commit b1aba2f

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

bin/shjs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ if (!test('-f', scriptName)) {
3434
}
3535

3636
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-
}
37+
process.argv = [process.argv[0], process.argv[1], ...args];
4338

4439
var path = require('path');
4540
var extensions = require('interpret').extensions;

test/resources/shjs/echo-args.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(...process.argv);

test/shjs.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ const shell = require('shelljs');
66

77
const binPath = path.resolve(__dirname, '../bin/shjs');
88

9-
function runWithShjs(name) {
9+
function runWithShjs(name, ...args) {
1010
// prefix with 'node ' for Windows, don't prefix for unix
1111
const execPath = process.platform === 'win32'
1212
? `${JSON.stringify(shell.config.execPath)} `
1313
: '';
1414
const script = path.resolve(__dirname, 'resources', 'shjs', name);
15-
return shell.exec(`${execPath}${binPath} ${script}`, { silent: true });
15+
let argString = args.map(arg => JSON.stringify(arg)).join(' ');
16+
if (argString) {
17+
argString = ' ' + argString;
18+
}
19+
return shell.exec(`${execPath}${binPath} ${script}${argString}`, {
20+
silent: true
21+
});
1622
}
1723

1824
//
@@ -61,6 +67,13 @@ test('CoffeeScript extension inference', t => {
6167
t.falsy(result.stderr);
6268
});
6369

70+
test('Multiple arguments', t => {
71+
const result = runWithShjs('echo-args.js', 'hello', 'there');
72+
t.is(result.code, 0);
73+
t.regex(result.stdout, /hello there\n$/);
74+
t.falsy(result.stderr);
75+
});
76+
6477
//
6578
// Invalids
6679
//

0 commit comments

Comments
 (0)