Skip to content

Commit

Permalink
feat: let @npmcli/promise-spawn do the escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Nov 1, 2022
1 parent b83ea82 commit 45f2301
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 411 deletions.
68 changes: 0 additions & 68 deletions lib/escape.js

This file was deleted.

49 changes: 3 additions & 46 deletions lib/make-spawn-args.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* eslint camelcase: "off" */
const isWindows = require('./is-windows.js')
const setPATH = require('./set-path.js')
const { resolve } = require('path')
const which = require('which')
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
const escape = require('./escape.js')

const makeSpawnArgs = options => {
const {
event,
path,
scriptShell = isWindows ? process.env.ComSpec || 'cmd' : 'sh',
scriptShell = true,
binPaths,
env = {},
stdio,
Expand All @@ -29,55 +26,15 @@ const makeSpawnArgs = options => {
npm_config_node_gyp,
})

let doubleEscape = false
const isCmd = /(?:^|\\)cmd(?:\.exe)?$/i.test(scriptShell)
if (isCmd) {
let initialCmd = ''
let insideQuotes = false
for (let i = 0; i < cmd.length; ++i) {
const char = cmd.charAt(i)
if (char === ' ' && !insideQuotes) {
break
}

initialCmd += char
if (char === '"' || char === "'") {
insideQuotes = !insideQuotes
}
}

let pathToInitial
try {
pathToInitial = which.sync(initialCmd, {
path: spawnEnv.path,
pathext: spawnEnv.pathext,
}).toLowerCase()
} catch (err) {
pathToInitial = initialCmd.toLowerCase()
}

doubleEscape = pathToInitial.endsWith('.cmd') || pathToInitial.endsWith('.bat')
}

let script = cmd
for (const arg of args) {
script += isCmd
? ` ${escape.cmd(arg, doubleEscape)}`
: ` ${escape.sh(arg)}`
}
const spawnArgs = isCmd
? ['/d', '/s', '/c', script]
: ['-c', '--', script]

const spawnOpts = {
env: spawnEnv,
stdioString,
stdio,
cwd: path,
...(isCmd ? { windowsVerbatimArguments: true } : {}),
shell: scriptShell,
}

return [scriptShell, spawnArgs, spawnOpts]
return [cmd, args, spawnOpts]
}

module.exports = makeSpawnArgs
123 changes: 0 additions & 123 deletions test/escape.js

This file was deleted.

0 comments on commit 45f2301

Please sign in to comment.