Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit 761dfe9

Browse files
committed
fix(windows): fixed windows binary pathing issues
Fixes: #62
1 parent d4b9226 commit 761dfe9

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ function localBinPath (cwd) {
126126
module.exports._getEnv = getEnv
127127
function getEnv (opts) {
128128
const args = ['run', 'env', '--parseable']
129-
return which(opts.npm).catch(() => {
130-
args.unshift(opts.npm)
131-
return process.argv[0]
129+
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
130+
if (npmPath) {
131+
args.unshift(opts.npm)
132+
return process.argv[0]
133+
} else {
134+
return opts.npm
135+
}
132136
}).then(npmPath => {
133137
return child.exec(npmPath, args)
134138
}).then(require('dotenv').parse)
@@ -188,9 +192,13 @@ function getNpmCache (opts) {
188192
if (opts.userconfig) {
189193
args.push('--userconfig', child.escapeArg(opts.userconfig, true))
190194
}
191-
return which(opts.npm).catch(() => {
192-
args.unshift(opts.npm)
193-
return process.argv[0]
195+
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
196+
if (npmPath) {
197+
args.unshift(opts.npm)
198+
return process.argv[0]
199+
} else {
200+
return opts.npm
201+
}
194202
}).then(npmPath => {
195203
return child.exec(npmPath, args)
196204
}).then(cache => cache.trim())
@@ -210,9 +218,13 @@ function buildArgs (specs, prefix, opts) {
210218
module.exports._installPackages = installPackages
211219
function installPackages (specs, prefix, opts) {
212220
const args = buildArgs(specs, prefix, opts)
213-
return which(opts.npm).catch(() => {
214-
args.unshift(opts.npm)
215-
return process.argv[0]
221+
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
222+
if (npmPath) {
223+
args.unshift(opts.npm)
224+
return process.argv[0]
225+
} else {
226+
return opts.npm
227+
}
216228
}).then(npmPath => {
217229
return child.escapeArg(npmPath, true)
218230
}).then(npmPath => {

test/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ const test = require('tap').test
1010
const main = require('../index.js')
1111

1212
const NPX_PATH = path.resolve(__dirname, 'util', 'npx-bin.js')
13-
let NPM_PATH = path.resolve(__dirname, '..', 'node_modules', '.bin', 'npm')
14-
if (process.platform === 'win32') {
15-
NPM_PATH += '.CMD'
16-
}
13+
let NPM_PATH = path.resolve(__dirname, '..', 'node_modules', 'npm', 'bin', 'npm-cli.js')
1714

1815
test('npx --shell-auto-fallback', t => {
1916
return child.spawn('node', [
@@ -53,7 +50,9 @@ test('npx existing subcommand', {
5350
})
5451
})
5552

56-
test('execCommand unit', t => {
53+
test('execCommand unit', {
54+
skip: process.platform === 'win32' && 'need a workaround for obnoxious auto-open of .md file on Windows'
55+
}, t => {
5756
let whichBin = path.resolve(
5857
__dirname, '..', 'node_modules', '.bin', 'which'
5958
)
@@ -89,9 +88,9 @@ test('installPackages unit', t => {
8988
const installPkgs = requireInject('../index.js', {
9089
'../child.js': {
9190
spawn (npmPath, args) {
92-
if (args[1] === 'fail') {
91+
if (args[2] === 'fail') {
9392
return Promise.reject(new Error('fail'))
94-
} else if (args[1] === 'codefail') {
93+
} else if (args[2] === 'codefail') {
9594
const err = new Error('npm failed')
9695
err.exitCode = 123
9796
return Promise.reject(err)
@@ -109,8 +108,8 @@ test('installPackages unit', t => {
109108
return installPkgs(['installme@latest', 'file:foo'], 'myprefix', {
110109
npm: NPM_PATH
111110
}).then(deets => {
112-
t.equal(deets[0], NPM_PATH, 'spawn got the right path to npm')
113111
t.deepEqual(deets[1], [
112+
NPM_PATH,
114113
'install', 'installme@latest', 'file:foo',
115114
'--global',
116115
'--prefix', 'myprefix',
@@ -161,12 +160,12 @@ test('getNpmCache', t => {
161160
}
162161
})._getNpmCache
163162
return getCache({npm: NPM_PATH}).then(cache => {
164-
t.equal(cache, `${NPM_PATH} config get cache --parseable`, 'requests cache from npm')
163+
t.equal(cache, `${process.argv[0]} ${NPM_PATH} config get cache --parseable`, 'requests cache from npm')
165164
return getCache({npm: NPM_PATH, userconfig})
166165
}).then(cache => {
167166
t.equal(
168167
cache,
169-
`${NPM_PATH} config get cache --parseable --userconfig ${
168+
`${process.argv[0]} ${NPM_PATH} config get cache --parseable --userconfig ${
170169
userconfig
171170
}-escaped-as-path-true`,
172171
'added userconfig if option present'

0 commit comments

Comments
 (0)