Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
fix(exec): escape binaries and args to cp.exec (#18)
Browse files Browse the repository at this point in the history
Fixes: #14
  • Loading branch information
zkat authored Jun 3, 2017
1 parent 0866a83 commit 55d6a11
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ function getExistingPath (command, opts) {
function getNpmCache (opts) {
return which('npm').then(npmPath => {
return BB.fromNode(cb => {
cp.exec(`${npmPath} config get cache${
opts.userconfig ? ` --userconfig ${opts.userconfig}` : ''
cp.exec(`${escapeArg(npmPath, true)} config get cache${
opts.userconfig
? ` --userconfig ${escapeArg(opts.userconfig)}`
: ''
}`, {}, cb)
}).then(cache => cache.trim())
})
Expand Down Expand Up @@ -162,3 +164,15 @@ function spawn (cmd, args, opts) {
})
})
}

function escapeArg (str, asPath) {
return process.platform === 'win32' && asPath
? path.normalize(str)
.split(/\\/)
.map(s => s.match(/\s+/) ? `"${s}"` : s)
: process.platform === 'win32'
? `"${path.normalize(str)}"`
: str.match(/[^-_.~/\w]/)
? `'${str.replace(/'/g, "'\"'\"'")}'`
: str
}

0 comments on commit 55d6a11

Please sign in to comment.