Skip to content

Commit 99156df

Browse files
nlfisaacs
authored andcommitted
pass extra arguments directly to run-script as an array
fixes #2425 PR-URL: #2448 Credit: @nlf Close: #2448 Reviewed-by: @isaacs, @wraithgar, @darcyclarke
1 parent a390d74 commit 99156df

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/exec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const cmd = (args, cb) => exec(args).then(() => cb()).catch(cb)
6767

6868
const run = async ({ args, call, pathArr, shell }) => {
6969
// turn list of args into command string
70-
const script = call || args.join(' ').trim() || shell
70+
const script = call || args.shift() || shell
7171

7272
// do the fakey runScript dance
7373
// still should work if no package.json in cwd
@@ -98,6 +98,7 @@ const run = async ({ args, call, pathArr, shell }) => {
9898
path: process.cwd(),
9999
stdioString: true,
100100
event: 'npx',
101+
args,
101102
env: {
102103
PATH: pathArr.join(delimiter),
103104
},
@@ -144,7 +145,7 @@ const exec = async args => {
144145
if (binExists) {
145146
return await run({
146147
args,
147-
call: [args[0], ...args.slice(1)].join(' ').trim(),
148+
call,
148149
pathArr,
149150
shell,
150151
})

test/lib/exec.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ t.test('npx foo, bin already exists locally', async t => {
124124
PROGRESS_IGNORED = true
125125
npm.localBin = path
126126

127-
await exec(['foo'], er => {
127+
await exec(['foo', 'one arg', 'two arg'], er => {
128128
t.ifError(er, 'npm exec')
129129
})
130130
t.match(RUN_SCRIPTS, [{
131131
pkg: { scripts: { npx: 'foo' }},
132+
args: ['one arg', 'two arg'],
132133
banner: false,
133134
path: process.cwd(),
134135
stdioString: true,
@@ -148,11 +149,12 @@ t.test('npx foo, bin already exists globally', async t => {
148149
PROGRESS_IGNORED = true
149150
npm.globalBin = path
150151

151-
await exec(['foo'], er => {
152+
await exec(['foo', 'one arg', 'two arg'], er => {
152153
t.ifError(er, 'npm exec')
153154
})
154155
t.match(RUN_SCRIPTS, [{
155156
pkg: { scripts: { npx: 'foo' }},
157+
args: ['one arg', 'two arg'],
156158
banner: false,
157159
path: process.cwd(),
158160
stdioString: true,
@@ -178,7 +180,7 @@ t.test('npm exec foo, already present locally', async t => {
178180
},
179181
_from: 'foo@',
180182
}
181-
await exec(['foo'], er => {
183+
await exec(['foo', 'one arg', 'two arg'], er => {
182184
if (er)
183185
throw er
184186
})
@@ -188,6 +190,7 @@ t.test('npm exec foo, already present locally', async t => {
188190
t.equal(PROGRESS_ENABLED, true, 'progress re-enabled')
189191
t.match(RUN_SCRIPTS, [{
190192
pkg: { scripts: { npx: 'foo' } },
193+
args: ['one arg', 'two arg'],
191194
banner: false,
192195
path: process.cwd(),
193196
stdioString: true,
@@ -220,6 +223,7 @@ t.test('npm exec <noargs>, run interactive shell', async t => {
220223
if (doRun) {
221224
t.match(RUN_SCRIPTS, [{
222225
pkg: { scripts: { npx: 'shell-cmd' } },
226+
args: [],
223227
banner: false,
224228
path: process.cwd(),
225229
stdioString: true,
@@ -281,7 +285,7 @@ t.test('npm exec foo, not present locally or in central loc', async t => {
281285
},
282286
_from: 'foo@',
283287
}
284-
await exec(['foo'], er => {
288+
await exec(['foo', 'one arg', 'two arg'], er => {
285289
if (er)
286290
throw er
287291
})
@@ -292,6 +296,7 @@ t.test('npm exec foo, not present locally or in central loc', async t => {
292296
const PATH = `${resolve(installDir, 'node_modules', '.bin')}${delimiter}${process.env.PATH}`
293297
t.match(RUN_SCRIPTS, [{
294298
pkg: { scripts: { npx: 'foo' } },
299+
args: ['one arg', 'two arg'],
295300
banner: false,
296301
path: process.cwd(),
297302
stdioString: true,
@@ -319,7 +324,7 @@ t.test('npm exec foo, not present locally but in central loc', async t => {
319324
},
320325
_from: 'foo@',
321326
}
322-
await exec(['foo'], er => {
327+
await exec(['foo', 'one arg', 'two arg'], er => {
323328
if (er)
324329
throw er
325330
})
@@ -330,6 +335,7 @@ t.test('npm exec foo, not present locally but in central loc', async t => {
330335
const PATH = `${resolve(installDir, 'node_modules', '.bin')}${delimiter}${process.env.PATH}`
331336
t.match(RUN_SCRIPTS, [{
332337
pkg: { scripts: { npx: 'foo' } },
338+
args: ['one arg', 'two arg'],
333339
banner: false,
334340
path: process.cwd(),
335341
stdioString: true,
@@ -357,7 +363,7 @@ t.test('npm exec foo, present locally but wrong version', async t => {
357363
},
358364
_from: 'foo@2.x',
359365
}
360-
await exec(['foo@2.x'], er => {
366+
await exec(['foo@2.x', 'one arg', 'two arg'], er => {
361367
if (er)
362368
throw er
363369
})
@@ -368,6 +374,7 @@ t.test('npm exec foo, present locally but wrong version', async t => {
368374
const PATH = `${resolve(installDir, 'node_modules', '.bin')}${delimiter}${process.env.PATH}`
369375
t.match(RUN_SCRIPTS, [{
370376
pkg: { scripts: { npx: 'foo' } },
377+
args: ['one arg', 'two arg'],
371378
banner: false,
372379
path: process.cwd(),
373380
stdioString: true,
@@ -392,7 +399,7 @@ t.test('npm exec --package=foo bar', async t => {
392399
_from: 'foo@',
393400
}
394401
npm.flatOptions.package = ['foo']
395-
await exec(['bar'], er => {
402+
await exec(['bar', 'one arg', 'two arg'], er => {
396403
if (er)
397404
throw er
398405
})
@@ -402,6 +409,7 @@ t.test('npm exec --package=foo bar', async t => {
402409
t.equal(PROGRESS_ENABLED, true, 'progress re-enabled')
403410
t.match(RUN_SCRIPTS, [{
404411
pkg: { scripts: { npx: 'bar' } },
412+
args: ['one arg', 'two arg'],
405413
banner: false,
406414
path: process.cwd(),
407415
stdioString: true,
@@ -442,6 +450,7 @@ t.test('npm exec @foo/bar -- --some=arg, locally installed', async t => {
442450
t.equal(PROGRESS_ENABLED, true, 'progress re-enabled')
443451
t.match(RUN_SCRIPTS, [{
444452
pkg: { scripts: { npx: 'bar' } },
453+
args: ['--some=arg'],
445454
banner: false,
446455
path: process.cwd(),
447456
stdioString: true,
@@ -473,7 +482,7 @@ t.test('npm exec @foo/bar, with same bin alias and no unscoped named bin, locall
473482
children: new Map([['@foo/bar', { name: '@foo/bar', version: '1.2.3' }]]),
474483
}
475484
MANIFESTS['@foo/bar'] = foobarManifest
476-
await exec(['@foo/bar'], er => {
485+
await exec(['@foo/bar', 'one arg', 'two arg'], er => {
477486
if (er)
478487
throw er
479488
})
@@ -483,6 +492,7 @@ t.test('npm exec @foo/bar, with same bin alias and no unscoped named bin, locall
483492
t.equal(PROGRESS_ENABLED, true, 'progress re-enabled')
484493
t.match(RUN_SCRIPTS, [{
485494
pkg: { scripts: { npx: 'baz' } },
495+
args: ['one arg', 'two arg'],
486496
banner: false,
487497
path: process.cwd(),
488498
stdioString: true,
@@ -552,7 +562,7 @@ t.test('run command with 2 packages, need install, verify sort', t => {
552562
},
553563
_from: 'bar@',
554564
}
555-
await exec(['foobar'], er => {
565+
await exec(['foobar', 'one arg', 'two arg'], er => {
556566
if (er)
557567
throw er
558568
})
@@ -563,6 +573,7 @@ t.test('run command with 2 packages, need install, verify sort', t => {
563573
const PATH = `${resolve(installDir, 'node_modules', '.bin')}${delimiter}${process.env.PATH}`
564574
t.match(RUN_SCRIPTS, [{
565575
pkg: { scripts: { npx: 'foobar' } },
576+
args: ['one arg', 'two arg'],
566577
banner: false,
567578
path: process.cwd(),
568579
stdioString: true,

0 commit comments

Comments
 (0)