diff --git a/.gitignore b/.gitignore index 3c3629e..07e6e47 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -node_modules +/node_modules diff --git a/index.js b/index.js index 6429c1b..a25cedb 100644 --- a/index.js +++ b/index.js @@ -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. diff --git a/lib/win-rebase.js b/lib/win-rebase.js index 3436c28..d4af5fe 100644 --- a/lib/win-rebase.js +++ b/lib/win-rebase.js @@ -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) diff --git a/test/fixtures/node_modules/npm/bin/npm-cli.js b/test/fixtures/node_modules/npm/bin/npm-cli.js new file mode 100644 index 0000000..b3c4007 --- /dev/null +++ b/test/fixtures/node_modules/npm/bin/npm-cli.js @@ -0,0 +1,4 @@ +'use strict'; +console.log('%j', process.execArgv) +console.log('%j', process.argv.slice(2)) +setTimeout(function () {}, 100) diff --git a/test/fixtures/npm.cmd b/test/fixtures/npm.cmd new file mode 100644 index 0000000..521ec9e --- /dev/null +++ b/test/fixtures/npm.cmd @@ -0,0 +1,2 @@ +@echo This code should never be executed. +exit /B 1 diff --git a/test/win-rebase.js b/test/win-rebase.js index 6d09849..b9d09fe 100644 --- a/test/win-rebase.js +++ b/test/win-rebase.js @@ -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() +})