Skip to content

changed extra args to array passing without JSON parsing. #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/make-spawn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const makeSpawnArgs = options => {
env = {},
stdio,
cmd,
cmdargs = [],
stdioString = false,
} = options

const isCmd = /(?:^|\\)cmd(?:\.exe)?$/i.test(scriptShell)
const args = isCmd ? ['/d', '/s', '/c', cmd] : ['-c', cmd]
const args = isCmd ? ['/d', '/s', '/c', cmd, ...cmdargs] : ['-c', cmd, ...cmdargs]

const spawnOpts = {
env: setPATH(path, {
Expand Down
14 changes: 10 additions & 4 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const runScriptPkg = async options => {

const { scripts = {}, gypfile } = pkg
let cmd = null
let cmdargs = []
if (options.cmd) {
cmd = options.cmd
} else if (pkg.scripts && pkg.scripts[event]) {
cmd = pkg.scripts[event] + args.map(a => ` ${JSON.stringify(a)}`).join('')
cmd = pkg.scripts[event]
cmdargs = args
} else if (
// If there is no preinstall or install script, default to rebuilding node-gyp packages.
event === 'install' &&
Expand All @@ -42,16 +44,19 @@ const runScriptPkg = async options => {
) {
cmd = defaultGypInstallScript
} else if (event === 'start' && await isServerPackage(path)) {
cmd = 'node server.js' + args.map(a => ` ${JSON.stringify(a)}`).join('')
cmd = 'node server.js'
cmdargs = args
}

if (!cmd) {
return { code: 0, signal: null }
}

const script = cmd + cmdargs.map(a => ` ${JSON.stringify(a)}`).join('')

if (stdio === 'inherit' && banner !== false) {
// we're dumping to the parent's stdout, so print the banner
console.log(bruce(pkg._id, event, cmd))
console.log(bruce(pkg._id, event, script))
}

const p = promiseSpawn(...makeSpawnArgs({
Expand All @@ -61,10 +66,11 @@ const runScriptPkg = async options => {
env: packageEnvs(env, pkg),
stdio,
cmd,
cmdargs,
stdioString,
}), {
event,
script: cmd,
script,
pkgid: pkg._id,
path,
})
Expand Down
27 changes: 18 additions & 9 deletions test/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let SIGNAL = null
const EXIT_CODE = 0

const runScriptPkg = requireInject('../lib/run-script-pkg.js', {
'../lib/make-spawn-args.js': options => ['sh', ['-c', options.cmd], options],
'../lib/make-spawn-args.js': options => ['sh', ['-c', options.cmd, ...options.cmdargs], options],
'@npmcli/promise-spawn': (...args) => {
const p = SIGNAL || EXIT_CODE
? Promise.reject(Object.assign(new Error('test command failed'), {
Expand Down Expand Up @@ -64,6 +64,7 @@ t.test('pkg has server.js, start not specified', async t => {
},
stdio: 'pipe',
cmd: 'node server.js',
cmdargs: [],
}, {
event: 'start',
script: 'node server.js',
Expand All @@ -81,14 +82,14 @@ t.test('pkg has server.js, start not specified, with args', async t => {
env: {
environ: 'value',
},
args: ['a', 'b', 'c'],
args: ['a', 'b\\S+b', 'c'],
stdio: 'pipe',
pkg: {
_id: '[email protected]',
scripts: {},
},
})
t.strictSame(res, ['sh', ['-c', 'node server.js "a" "b" "c"'], {
t.strictSame(res, ['sh', ['-c', 'node server.js', 'a', 'b\\S+b', 'c'], {
stdioString: false,
event: 'start',
path,
Expand All @@ -97,10 +98,11 @@ t.test('pkg has server.js, start not specified, with args', async t => {
environ: 'value',
},
stdio: 'pipe',
cmd: 'node server.js "a" "b" "c"',
cmd: 'node server.js',
cmdargs: ['a', 'b\\S+b', 'c'],
}, {
event: 'start',
script: 'node server.js "a" "b" "c"',
script: 'node server.js "a" "b\\\\S+b" "c"',
pkgid: '[email protected]',
path,
}])
Expand Down Expand Up @@ -134,6 +136,7 @@ t.test('pkg has no foo script, but custom cmd provided', t => runScriptPkg({
},
stdio: 'pipe',
cmd: 'bar',
cmdargs: [],
}, {
event: 'foo',
script: 'bar',
Expand Down Expand Up @@ -169,6 +172,7 @@ t.test('do the banner when stdio is inherited, handle line breaks', t => {
},
stdio: 'inherit',
cmd: 'bar\nbaz\n',
cmdargs: [],
}, {
event: 'foo',
script: 'bar\nbaz\n',
Expand Down Expand Up @@ -206,6 +210,7 @@ t.test('do not show banner when stdio is inherited, if suppressed', t => {
},
stdio: 'inherit',
cmd: 'bar',
cmdargs: [],
}, {
event: 'foo',
script: 'bar',
Expand Down Expand Up @@ -241,6 +246,7 @@ t.test('do the banner with no pkgid', t => {
},
stdio: 'inherit',
cmd: 'bar',
cmdargs: [],
}, {
event: 'foo',
script: 'bar',
Expand Down Expand Up @@ -273,6 +279,7 @@ t.test('pkg has foo script', t => runScriptPkg({
},
stdio: 'pipe',
cmd: 'bar',
cmdargs: [],
}, {
event: 'foo',
script: 'bar',
Expand All @@ -294,8 +301,8 @@ t.test('pkg has foo script, with args', t => runScriptPkg({
foo: 'bar',
},
},
args: ['a', 'b', 'c'],
}).then(res => t.strictSame(res, ['sh', ['-c', 'bar "a" "b" "c"'], {
args: ['a', 'b', 'c\\sc'],
}).then(res => t.strictSame(res, ['sh', ['-c', 'bar', 'a', 'b', 'c\\sc'], {
stdioString: false,
event: 'foo',
path: 'path',
Expand All @@ -304,10 +311,11 @@ t.test('pkg has foo script, with args', t => runScriptPkg({
environ: 'value',
},
stdio: 'pipe',
cmd: 'bar "a" "b" "c"',
cmd: 'bar',
cmdargs: ['a', 'b', 'c\\sc'],
}, {
event: 'foo',
script: 'bar "a" "b" "c"',
script: 'bar "a" "b" "c\\\\sc"',
pkgid: '[email protected]',
path: 'path',
}])))
Expand Down Expand Up @@ -340,6 +348,7 @@ t.test('pkg has no install or preinstall script, but node-gyp files are present'
env: { environ: 'value' },
stdio: 'pipe',
cmd: 'node-gyp rebuild',
cmdargs: [],
stdioString: false,
},
{
Expand Down