Skip to content

Commit da14d3b

Browse files
nlfisaacs
authored andcommitted
use puka for parsing and quoting run scripts
Closes: #3 Closes: #14 PR-URL: #17 Credit: @nlf Close: #17 Reviewed-by: @isaacs
1 parent a49e736 commit da14d3b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

lib/make-spawn-args.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ const isWindows = require('./is-windows.js')
33
const setPATH = require('./set-path.js')
44
const {resolve} = require('path')
55
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
6+
const { quoteForShell, ShellString, ShellStringText, ShellStringUnquoted } = require('puka')
7+
8+
const escapeCmd = cmd => {
9+
const result = []
10+
const parsed = ShellString.sh([cmd])
11+
for (const child of parsed.children) {
12+
if (child instanceof ShellStringText) {
13+
const children = child.contents.filter(segment => segment !== null).map(segment => quoteForShell(segment, false, isWindows && 'win32'))
14+
result.push(...children)
15+
} else if (child instanceof ShellStringUnquoted) {
16+
result.push(child.value)
17+
} else {
18+
result.push(isWindows ? '&' : ';')
19+
}
20+
}
21+
22+
return result.join('')
23+
}
624

725
const makeSpawnArgs = options => {
826
const {
@@ -16,7 +34,7 @@ const makeSpawnArgs = options => {
1634
} = options
1735

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

2139
const spawnOpts = {
2240
env: setPATH(path, {

test/make-spawn-args.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ if (isWindows) {
2121
t.match(makeSpawnArgs({
2222
event: 'event',
2323
path: 'path',
24-
cmd: 'script',
24+
cmd: 'script "quoted parameter"; second command',
2525
}), [
2626
'cmd',
27-
[ '/d', '/s', '/c', '"script"' ],
27+
[ '/d', '/s', '/c', `script "quoted parameter"& second command` ],
2828
{
2929
env: {
3030
npm_package_json: /package\.json$/,
@@ -43,10 +43,10 @@ if (isWindows) {
4343
t.match(makeSpawnArgs({
4444
event: 'event',
4545
path: 'path',
46-
cmd: 'script',
46+
cmd: 'script "quoted parameter"; second command',
4747
}), [
4848
'blrorp',
49-
[ '-c', 'script' ],
49+
[ '-c', `script "quoted parameter"& second command` ],
5050
{
5151
env: {
5252
npm_package_json: /package\.json$/,
@@ -62,11 +62,11 @@ if (isWindows) {
6262
t.match(makeSpawnArgs({
6363
event: 'event',
6464
path: 'path',
65-
cmd: 'script',
65+
cmd: 'script "quoted parameter"; second command',
6666
scriptShell: 'cmd.exe',
6767
}), [
6868
'cmd.exe',
69-
[ '/d', '/s', '/c', '"script"' ],
69+
[ '/d', '/s', '/c', `script "quoted parameter"& second command` ],
7070
{
7171
env: {
7272
npm_package_json: /package\.json$/,
@@ -88,10 +88,10 @@ if (isWindows) {
8888
t.match(makeSpawnArgs({
8989
event: 'event',
9090
path: 'path',
91-
cmd: 'script',
91+
cmd: 'script "quoted parameter"; second command',
9292
}), [
9393
'sh',
94-
[ '-c', 'script' ],
94+
[ '-c', `script 'quoted parameter'; second command` ],
9595
{
9696
env: {
9797
npm_package_json: /package\.json$/,
@@ -109,11 +109,11 @@ if (isWindows) {
109109
t.match(makeSpawnArgs({
110110
event: 'event',
111111
path: 'path',
112-
cmd: 'script',
112+
cmd: 'script "quoted parameter"; second command',
113113
scriptShell: 'cmd.exe',
114114
}), [
115115
'cmd.exe',
116-
[ '/d', '/s', '/c', '"script"' ],
116+
[ '/d', '/s', '/c', `script 'quoted parameter'; second command` ],
117117
{
118118
env: {
119119
npm_package_json: /package\.json$/,

0 commit comments

Comments
 (0)