Skip to content

Commit

Permalink
Special npm handling for Windows.
Browse files Browse the repository at this point in the history
Fixes a variant of istanbuljs/nyc#190 for
Windows when the `npm.cmd` shim is invoked from the shell
(i.e. `cmd /s`).

Fixes: istanbuljs/nyc#300
  • Loading branch information
addaleax committed Jul 9, 2016
1 parent cacf8b9 commit 26212a6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules
/node_modules
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ function wrappedSpawnFunction (fn, workingDir) {
)) {
cmdi = options.args.indexOf('/c')
if (cmdi !== -1) {
options.args[cmdi + 1] = winRebase(options.args[cmdi + 1], workingDir + '/node.cmd')
options.args[cmdi + 1] = winRebase(options.args[cmdi + 1],
workingDir + '/node.cmd',
whichOrUndefined)
}
} else if (file === 'node' || file === 'iojs' || cmdname === file) {
// make sure it has a main script.
Expand Down
13 changes: 11 additions & 2 deletions lib/win-rebase.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
var re = /^\s*("*)([^"]*?\b(?:node|iojs)(?:\.exe)?)("*)( |$)/
var npmre = /^\s*("*)([^"]*?\b(?:npm))("*)( |$)/
var path_ = require('path')
if (path_.win32) path_ = path_.win32

module.exports = function (path, rebase) {
module.exports = function (path, rebase, whichOrUndefined) {
var m = path.match(re)
if (!m) return path
if (!m) {
m = path.match(npmre)
if (!m) return path
var npmPath = whichOrUndefined('npm') || 'npm'
npmPath = path_.dirname(npmPath) + '\\node_modules\\npm\\bin\\npm-cli.js'
return path.replace(npmre, m[1] + rebase + ' "' + npmPath + '"' + m[3] + m[4])
}
// preserve the quotes
var replace = m[1] + rebase + m[3] + m[4]
return path.replace(re, replace)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/node_modules/npm/bin/npm-cli.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/fixtures/npm.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo This code should never be executed.
exit /B 1
8 changes: 8 additions & 0 deletions test/win-rebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ t.test('handles many quotes', function (t) {
t.equal(result, '""C:\\foo" "foo.js""')
t.end()
})

t.test('handles npm invocations', function (t) {
var result = winRebase('""npm" "install""',
'C:\\foo',
function() { return 'C:\\path-to-npm\\npm' })
t.equal(result, '""C:\\foo "C:\\path-to-npm\\node_modules\\npm\\bin\\npm-cli.js"" "install""')
t.end()
})

0 comments on commit 26212a6

Please sign in to comment.