Skip to content
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
20 changes: 19 additions & 1 deletion lib/make-spawn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ const isWindows = require('./is-windows.js')
const setPATH = require('./set-path.js')
const {resolve} = require('path')
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
const { quoteForShell, ShellString, ShellStringText, ShellStringUnquoted } = require('puka')

const escapeCmd = cmd => {
const result = []
const parsed = ShellString.sh([cmd])
for (const child of parsed.children) {
if (child instanceof ShellStringText) {
const children = child.contents.filter(segment => segment !== null).map(segment => quoteForShell(segment, false, isWindows && 'win32'))
result.push(...children)
} else if (child instanceof ShellStringUnquoted) {
result.push(child.value)
} else {
result.push(isWindows ? '&' : ';')
}
}

return result.join('')
}

const makeSpawnArgs = options => {
const {
Expand All @@ -16,7 +34,7 @@ const makeSpawnArgs = options => {
} = options

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

const spawnOpts = {
env: setPATH(path, {
Expand Down
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@npmcli/promise-spawn": "^1.3.0",
"infer-owner": "^1.0.4",
"node-gyp": "^7.1.0",
"puka": "^1.0.1",
"read-package-json-fast": "^1.1.3"
},
"files": [
Expand Down
20 changes: 10 additions & 10 deletions test/make-spawn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ if (isWindows) {
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
cmd: 'script "quoted parameter"; second command',
}), [
'cmd',
[ '/d', '/s', '/c', '"script"' ],
[ '/d', '/s', '/c', `script "quoted parameter"& second command` ],
{
env: {
npm_package_json: /package\.json$/,
Expand All @@ -43,10 +43,10 @@ if (isWindows) {
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
cmd: 'script "quoted parameter"; second command',
}), [
'blrorp',
[ '-c', 'script' ],
[ '-c', `script "quoted parameter"& second command` ],
{
env: {
npm_package_json: /package\.json$/,
Expand All @@ -62,11 +62,11 @@ if (isWindows) {
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
cmd: 'script "quoted parameter"; second command',
scriptShell: 'cmd.exe',
}), [
'cmd.exe',
[ '/d', '/s', '/c', '"script"' ],
[ '/d', '/s', '/c', `script "quoted parameter"& second command` ],
{
env: {
npm_package_json: /package\.json$/,
Expand All @@ -88,10 +88,10 @@ if (isWindows) {
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
cmd: 'script "quoted parameter"; second command',
}), [
'sh',
[ '-c', 'script' ],
[ '-c', `script 'quoted parameter'; second command` ],
{
env: {
npm_package_json: /package\.json$/,
Expand All @@ -109,11 +109,11 @@ if (isWindows) {
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
cmd: 'script "quoted parameter"; second command',
scriptShell: 'cmd.exe',
}), [
'cmd.exe',
[ '/d', '/s', '/c', '"script"' ],
[ '/d', '/s', '/c', `script 'quoted parameter'; second command` ],
{
env: {
npm_package_json: /package\.json$/,
Expand Down