Skip to content

Commit

Permalink
Add special handling for spawning npm
Browse files Browse the repository at this point in the history
As laid out in istanbuljs/nyc#190, spawn-wrap’ing `npm` does currently
not work when using the `npm` binary that comes bundled with Node.js.

This adds special handler code for the case that `npm` is the
program that should be executed, looking for the `npm` executable
in PATH and invoking the shim with it as an argument.
  • Loading branch information
addaleax committed Mar 31, 2016
1 parent 0f5908a commit c335b3e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ function wrap (argv, env, workingDir) {
options.envPairs.push((isWindows ? 'Path=' : 'PATH=') + workingDir)
}

if (file === 'npm') {
var npmPath = lookupInPath('npm', pathEnv)

if (npmPath) {
options.args[0] = npmPath

options.file = workingDir + '/node'
options.args.unshift(workingDir + '/node')
}
}

if (isWindows) fixWindowsBins(workingDir, options)

return spawn.call(this, options)
Expand All @@ -153,6 +164,28 @@ function fixWindowsBins (workingDir, options) {
}
}

// imitate which(1)
function lookupInPath(executableName, pathEnv) {
var pathDirs = (pathEnv || '').split(colon)
var executablePath

for (var i = 0; i < pathDirs.length; i++) {
executablePath = path.resolve(pathDirs[i], executableName)
try {
fs.accessSync(executablePath, fs.R_OK | fs.X_OK)
} catch (err) {
// not found/not executable
executablePath = undefined
continue
}

// found it
break
}

return executablePath
}

function setup (argv, env) {
if (argv && typeof argv === 'object' && !env && !Array.isArray(argv)) {
env = argv
Expand Down

0 comments on commit c335b3e

Please sign in to comment.