Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
feat(npm): allow configuration of npm binary
Browse files Browse the repository at this point in the history
Fixes: #24
  • Loading branch information
zkat committed Jun 3, 2017
1 parent a2cae9d commit e5d5634
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
30 changes: 16 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ let rimraf
const updateNotifier = require('update-notifier')
const which = BB.promisify(require('which'))

const INSTALLER_PATH = path.resolve(__dirname, 'node_modules/.bin/npm')
const PATH_SEP = process.platform === 'win32' ? ';' : ':'

updateNotifier({pkg}).notify()
Expand Down Expand Up @@ -99,12 +98,13 @@ function getExistingPath (command, opts) {

module.exports._getNpmCache = getNpmCache
function getNpmCache (opts) {
const args = ['config', 'get', 'cache']
if (opts.userconfig) {
args.push('--userconfig', opts.userconfig)
}
return child.exec(INSTALLER_PATH, ['config', 'get', 'cache'])
.then(cache => cache.trim())
return which(opts.npm).then(npmPath => {
const args = ['config', 'get', 'cache']
if (opts.userconfig) {
args.push('--userconfig', opts.userconfig)
}
return child.exec(npmPath, ['config', 'get', 'cache'])
}).then(cache => cache.trim())
}

module.exports._buildArgs = buildArgs
Expand All @@ -121,12 +121,14 @@ function buildArgs (specs, prefix, opts) {
module.exports._installPackages = installPackages
function installPackages (specs, prefix, npmOpts) {
const args = buildArgs(specs, prefix, npmOpts)
return child.spawn(INSTALLER_PATH, args, {
stdio: [0, 'ignore', 2]
}).catch(err => {
if (err.exitCode) {
err.message = `Install for ${specs} failed with code ${err.exitCode}`
}
throw err
return which(npmOpts.npm).then(npmPath => {
return child.spawn(npmPath, args, {
stdio: [0, 'ignore', 2]
}).catch(err => {
if (err.exitCode) {
err.message = `Install for ${specs} failed with code ${err.exitCode}`
}
throw err
})
})
}
5 changes: 5 additions & 0 deletions parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ function parseArgs (argv) {
describe: 'Ignores existing binaries in $PATH, or in the local project. This forces npx to do a temporary install and use the latest version.',
type: 'boolean'
})
.option('npm', {
describe: 'npm binary to use for internal operations.',
type: 'string',
default: path.resolve(__dirname, 'node_modules', '.bin', 'npm')
})
.version()
.alias('version', 'v')
.help()
Expand Down
8 changes: 8 additions & 0 deletions test/parse-args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const path = require('path')
const test = require('tap').test

const parseArgs = require('../parse-args.js')
Expand All @@ -10,6 +11,7 @@ test('parses basic command', t => {
t.deepEqual(parsed.package, ['foo@latest'])
t.equal(parsed.packageRequested, false)
t.equal(parsed.cmdHadVersion, false)
t.equal(parsed.npm, path.resolve(__dirname, '..', 'node_modules', '.bin', 'npm'))
t.deepEqual(parsed.cmdOpts, [])
t.done()
})
Expand Down Expand Up @@ -132,3 +134,9 @@ test('-- still respects -p', t => {
t.deepEqual(parsed.cmdOpts, ['a', 'b'])
t.done()
})

test('allows configuration of npm binary', t => {
const parsed = parseArgs(['/node', '/npx', '--npm', './mynpm', 'foo'])
t.equal(parsed.npm, './mynpm')
t.done()
})

0 comments on commit e5d5634

Please sign in to comment.