Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path construction #107

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function setup(argv, env) {
}

const key = process.pid + '-' + crypto.randomBytes(6).toString('hex')
let workingDir = homedir + key
let workingDir = path.resolve(homedir, `.node-spawn-wrap-${key}`)

const settings = JSON.stringify({
module: __filename,
Expand Down
5 changes: 1 addition & 4 deletions lib/homedir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

const os = require('os')

const home = process.env.SPAWN_WRAP_SHIM_ROOT || os.homedir()
const homedir = home + '/.node-spawn-wrap-'

module.exports = homedir
module.exports = process.env.SPAWN_WRAP_SHIM_ROOT || os.homedir()
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