Skip to content

Commit

Permalink
fix: Avoid path concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Oct 7, 2019
1 parent 5bcb288 commit 5626f2a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/mungers/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function mungeCmd(workingDir, options) {
if (m) {
originalNode = m[2]
// TODO: Remove `replace`: seems unused
replace = m[1] + workingDir + '/node.cmd' + m[3] + m[4]
replace = m[1] + path.join(workingDir, 'node.cmd') + m[3] + m[4]
newArgs[cmdi + 1] = m[1] + m[2] + m[3] +
' "' + workingDir + '\\node"' + m[4]
' "' + path.join(workingDir, 'node') + '"' + m[4]
} else {
// XXX probably not a good idea to rewrite to the first npm in the
// path if it's a full path to npm. And if it's not a full path to
Expand All @@ -46,8 +46,8 @@ function mungeCmd(workingDir, options) {
}

let npmPath = whichOrUndefined('npm') || 'npm'
npmPath = path.dirname(npmPath) + '\\node_modules\\npm\\bin\\npm-cli.js'
replace = m[1] + '"' + workingDir + '/node.cmd"' +
npmPath = path.join(path.dirname(npmPath), 'node_modules', 'npm', 'bin', 'npm-cli.js')
replace = m[1] + '"' + path.join(workingDir, 'node.cmd') + '"' +
' "' + npmPath + '"' +
m[3] + m[4]
newArgs[cmdi + 1] = command.replace(npmre, replace)
Expand Down
3 changes: 1 addition & 2 deletions lib/mungers/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const path = require("path")
const homedir = require("../homedir")

const pathRe = isWindows() ? /^PATH=/i : /^PATH=/;
const colon = isWindows() ? ';' : ':'

/**
* Updates the environment variables to intercept `node` commands and pass down options.
Expand All @@ -23,7 +22,7 @@ function mungeEnv(workingDir, options) {
// `5` corresponds to the length of `PATH=`
pathEnv = ep.substr(5)
const k = ep.substr(0, 5)
return k + workingDir + colon + pathEnv
return k + workingDir + path.delimiter + pathEnv
} else {
// Return as-is
return ep;
Expand Down
2 changes: 1 addition & 1 deletion lib/mungers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function mungeNode(workingDir, options) {
let newFile = options.file;

if (hasMain) {
const replace = workingDir + '/' + command
const replace = path.join(workingDir, command)
newArgs.splice(mainIndex, 0, replace)
}

Expand Down
5 changes: 3 additions & 2 deletions lib/mungers/sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ function mungeSh(workingDir, options) {
let newArgs = [...options.args];
// Remember the original Node command to use it in the shim
let originalNode;
const workingNode = path.join(workingDir, 'node')

if (isNode(exe)) {
originalNode = command
c = match[1] + match[2] + ' "' + workingDir + '/node" ' + match[3]
c = `${match[1]}${match[2]} "${workingNode}" ${match[3]}`
newArgs[cmdi + 1] = c
} else if (exe === 'npm' && !isWindows()) {
// XXX this will exhibit weird behavior when using /path/to/npm,
// if some other npm is first in the path.
const npmPath = whichOrUndefined('npm')

if (npmPath) {
c = c.replace(re, '$1 "' + workingDir + '/node" "' + npmPath + '" $3')
c = c.replace(re, `$1 "${workingNode}" "${npmPath}" $3`)
newArgs[cmdi + 1] = c
debug('npm munge!', c)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mungers/shebang.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function mungeShebang(workingDir, options) {

const originalNode = shebangbin
const file = shebangbin
const args = [shebangbin, workingDir + '/' + maybeNode]
const args = [shebangbin, path.join(workingDir, maybeNode)]
.concat(resolved)
.concat(match[1].split(' ').slice(1))
.concat(options.args.slice(1))
Expand Down

0 comments on commit 5626f2a

Please sign in to comment.