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

Add special handling for spawning npm #12

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var path = require('path')
var signalExit = require('signal-exit')
var homedir = require('os-homedir')() + '/.node-spawn-wrap-'
var winRebase = require('./lib/win-rebase')
var which = require('which')

var cmdname = path.basename(process.execPath, '.exe')

Expand Down Expand Up @@ -72,6 +73,13 @@ function wrap (argv, env, workingDir) {
exe === cmdname) {
c = c.replace(re, '$1 ' + workingDir + '/node')
options.args[cmdi + 1] = c
} else if (exe === 'npm' && !isWindows) {
var npmPath = whichOrUndefined('npm')

if (npmPath) {
c = c.replace(re, '$1 ' + workingDir + '/node ' + npmPath)
options.args[cmdi + 1] = c
}
}
}
}
Expand Down Expand Up @@ -133,6 +141,17 @@ function wrap (argv, env, workingDir) {
options.envPairs.push((isWindows ? 'Path=' : 'PATH=') + workingDir)
}

if (file === 'npm' && !isWindows) {
var npmPath = whichOrUndefined('npm')

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 @@ -141,6 +160,14 @@ function wrap (argv, env, workingDir) {
return unwrap
}

function whichOrUndefined (executable) {
var path
try {
path = which.sync(executable)
} catch (er) {}
return path
}

// by default Windows will reference the full
// path to the node.exe or iojs.exe as the bin,
// we should instead point spawn() at our .cmd shim.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"mkdirp": "^0.5.0",
"os-homedir": "^1.0.1",
"rimraf": "^2.3.3",
"signal-exit": "^2.0.0"
"signal-exit": "^2.0.0",
"which": "^1.2.4"
},
"scripts": {
"test": "tap test/*.js"
Expand Down
36 changes: 36 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var winNoSig = isWindows && 'no signals get through cmd'
var onExit = require('signal-exit')
var cp = require('child_process')
var fixture = require.resolve('./fixtures/script.js')
var npmFixture = require.resolve('./fixtures/npm')
var fs = require('fs')
var path = require('path')

Expand Down Expand Up @@ -293,6 +294,41 @@ t.test('exec shebang', { skip: winNoShebang }, function (t) {
})
})

// see: https://github.com/bcoe/nyc/issues/190
t.test('Node 5.8.x + npm 3.7.x - spawn', { skip: winNoShebang }, function (t) {
var npmdir = path.dirname(npmFixture)
process.env.PATH = npmdir + ':' + (process.env.PATH || '')
var child = cp.spawn('npm', ['xyz'])

var out = ''
child.stdout.on('data', function (c) {
out += c
})
child.on('close', function (code, signal) {
t.equal(code, 0)
t.equal(signal, null)
t.true(~out.indexOf('xyz'))
t.end()
})
})

t.test('Node 5.8.x + npm 3.7.x - shell', { skip: winNoShebang }, function (t) {
var npmdir = path.dirname(npmFixture)
process.env.PATH = npmdir + ':' + (process.env.PATH || '')
var child = cp.exec('npm xyz')

var out = ''
child.stdout.on('data', function (c) {
out += c
})
child.on('close', function (code, signal) {
t.equal(code, 0)
t.equal(signal, null)
t.true(~out.indexOf('xyz'))
t.end()
})
})

t.test('--harmony', function (t) {
var node = process.execPath
var child = cp.spawn(node, ['--harmony', fixture, 'xyz'])
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
// 2>/dev/null; exec "`dirname "$0"`/node" "$0" "$@"
console.log('%j', process.execArgv)
console.log('%j', process.argv.slice(2))