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

Commit

Permalink
fix(windows): fixed windows binary pathing issues
Browse files Browse the repository at this point in the history
Fixes: #62
  • Loading branch information
zkat committed Jul 14, 2017
1 parent d4b9226 commit 761dfe9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ function localBinPath (cwd) {
module.exports._getEnv = getEnv
function getEnv (opts) {
const args = ['run', 'env', '--parseable']
return which(opts.npm).catch(() => {
args.unshift(opts.npm)
return process.argv[0]
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
args.unshift(opts.npm)
return process.argv[0]
} else {
return opts.npm
}
}).then(npmPath => {
return child.exec(npmPath, args)
}).then(require('dotenv').parse)
Expand Down Expand Up @@ -188,9 +192,13 @@ function getNpmCache (opts) {
if (opts.userconfig) {
args.push('--userconfig', child.escapeArg(opts.userconfig, true))
}
return which(opts.npm).catch(() => {
args.unshift(opts.npm)
return process.argv[0]
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
args.unshift(opts.npm)
return process.argv[0]
} else {
return opts.npm
}
}).then(npmPath => {
return child.exec(npmPath, args)
}).then(cache => cache.trim())
Expand All @@ -210,9 +218,13 @@ function buildArgs (specs, prefix, opts) {
module.exports._installPackages = installPackages
function installPackages (specs, prefix, opts) {
const args = buildArgs(specs, prefix, opts)
return which(opts.npm).catch(() => {
args.unshift(opts.npm)
return process.argv[0]
return findNodeScript(opts.npm, {isLocal: true}).then(npmPath => {
if (npmPath) {
args.unshift(opts.npm)
return process.argv[0]
} else {
return opts.npm
}
}).then(npmPath => {
return child.escapeArg(npmPath, true)
}).then(npmPath => {
Expand Down
19 changes: 9 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ const test = require('tap').test
const main = require('../index.js')

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

test('npx --shell-auto-fallback', t => {
return child.spawn('node', [
Expand Down Expand Up @@ -53,7 +50,9 @@ test('npx existing subcommand', {
})
})

test('execCommand unit', t => {
test('execCommand unit', {
skip: process.platform === 'win32' && 'need a workaround for obnoxious auto-open of .md file on Windows'
}, t => {
let whichBin = path.resolve(
__dirname, '..', 'node_modules', '.bin', 'which'
)
Expand Down Expand Up @@ -89,9 +88,9 @@ test('installPackages unit', t => {
const installPkgs = requireInject('../index.js', {
'../child.js': {
spawn (npmPath, args) {
if (args[1] === 'fail') {
if (args[2] === 'fail') {
return Promise.reject(new Error('fail'))
} else if (args[1] === 'codefail') {
} else if (args[2] === 'codefail') {
const err = new Error('npm failed')
err.exitCode = 123
return Promise.reject(err)
Expand All @@ -109,8 +108,8 @@ test('installPackages unit', t => {
return installPkgs(['installme@latest', 'file:foo'], 'myprefix', {
npm: NPM_PATH
}).then(deets => {
t.equal(deets[0], NPM_PATH, 'spawn got the right path to npm')
t.deepEqual(deets[1], [
NPM_PATH,
'install', 'installme@latest', 'file:foo',
'--global',
'--prefix', 'myprefix',
Expand Down Expand Up @@ -161,12 +160,12 @@ test('getNpmCache', t => {
}
})._getNpmCache
return getCache({npm: NPM_PATH}).then(cache => {
t.equal(cache, `${NPM_PATH} config get cache --parseable`, 'requests cache from npm')
t.equal(cache, `${process.argv[0]} ${NPM_PATH} config get cache --parseable`, 'requests cache from npm')
return getCache({npm: NPM_PATH, userconfig})
}).then(cache => {
t.equal(
cache,
`${NPM_PATH} config get cache --parseable --userconfig ${
`${process.argv[0]} ${NPM_PATH} config get cache --parseable --userconfig ${
userconfig
}-escaped-as-path-true`,
'added userconfig if option present'
Expand Down

0 comments on commit 761dfe9

Please sign in to comment.