Skip to content

Commit 45f2301

Browse files
committed
feat: let @npmcli/promise-spawn do the escaping
1 parent b83ea82 commit 45f2301

File tree

4 files changed

+32
-411
lines changed

4 files changed

+32
-411
lines changed

Diff for: lib/escape.js

-68
This file was deleted.

Diff for: lib/make-spawn-args.js

+3-46
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
/* eslint camelcase: "off" */
2-
const isWindows = require('./is-windows.js')
32
const setPATH = require('./set-path.js')
43
const { resolve } = require('path')
5-
const which = require('which')
64
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
7-
const escape = require('./escape.js')
85

96
const makeSpawnArgs = options => {
107
const {
118
event,
129
path,
13-
scriptShell = isWindows ? process.env.ComSpec || 'cmd' : 'sh',
10+
scriptShell = true,
1411
binPaths,
1512
env = {},
1613
stdio,
@@ -29,55 +26,15 @@ const makeSpawnArgs = options => {
2926
npm_config_node_gyp,
3027
})
3128

32-
let doubleEscape = false
33-
const isCmd = /(?:^|\\)cmd(?:\.exe)?$/i.test(scriptShell)
34-
if (isCmd) {
35-
let initialCmd = ''
36-
let insideQuotes = false
37-
for (let i = 0; i < cmd.length; ++i) {
38-
const char = cmd.charAt(i)
39-
if (char === ' ' && !insideQuotes) {
40-
break
41-
}
42-
43-
initialCmd += char
44-
if (char === '"' || char === "'") {
45-
insideQuotes = !insideQuotes
46-
}
47-
}
48-
49-
let pathToInitial
50-
try {
51-
pathToInitial = which.sync(initialCmd, {
52-
path: spawnEnv.path,
53-
pathext: spawnEnv.pathext,
54-
}).toLowerCase()
55-
} catch (err) {
56-
pathToInitial = initialCmd.toLowerCase()
57-
}
58-
59-
doubleEscape = pathToInitial.endsWith('.cmd') || pathToInitial.endsWith('.bat')
60-
}
61-
62-
let script = cmd
63-
for (const arg of args) {
64-
script += isCmd
65-
? ` ${escape.cmd(arg, doubleEscape)}`
66-
: ` ${escape.sh(arg)}`
67-
}
68-
const spawnArgs = isCmd
69-
? ['/d', '/s', '/c', script]
70-
: ['-c', '--', script]
71-
7229
const spawnOpts = {
7330
env: spawnEnv,
7431
stdioString,
7532
stdio,
7633
cwd: path,
77-
...(isCmd ? { windowsVerbatimArguments: true } : {}),
34+
shell: scriptShell,
7835
}
7936

80-
return [scriptShell, spawnArgs, spawnOpts]
37+
return [cmd, args, spawnOpts]
8138
}
8239

8340
module.exports = makeSpawnArgs

Diff for: test/escape.js

-123
This file was deleted.

0 commit comments

Comments
 (0)