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

Commit

Permalink
feat(commands): -p and @Version now trigger installs
Browse files Browse the repository at this point in the history
Fixes: #9

BREAKING CHANGE: If a command has an explicit --package option, or if the command has an @Version part, any version of the command in $PATH will be ignored and a regular install will be executed.
  • Loading branch information
zkat committed May 31, 2017
1 parent 0e0d311 commit 9668c83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Executes `<command>` either from a local `node_modules/.bin`, or from a central

By default, `<command>` will be installed prior to execution. An optional `@version` may be appended to specify the package version required.

If a version specifier is included, or if `--package` is used, npx will ignore the version of the package in the current path, if it exists.

* `-p, --package <package>` - define the package to be installed. This defaults to the value of `<command>`. This is only needed for packages with multiple binaries if you want to call one of the other executables, or where the binary name does not match the package name. If this option is provided `<command>` will be executed as-is, without interpreting `@version` if it's there.

* `--cache <path>` - set the location of the npm cache. Defaults to npm's own cache settings.
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ function getCmdPath (command, spec, npmOpts) {
}

function getExistingPath (command, opts) {
if (opts.saveProd || opts.saveDev || opts.saveOptional || opts.global) {
if (
opts.saveProd ||
opts.saveDev ||
opts.saveOptional ||
opts.saveBundle ||
opts.saveExact ||
opts.global ||
opts.prefix ||
opts.cmdHadVersion ||
opts.packageRequested
) {
return BB.resolve(false)
} else {
return which(command).catch({code: 'ENOENT'}, () => false)
Expand Down Expand Up @@ -91,6 +101,7 @@ function buildArgs (spec, prefix, opts) {
}
if (opts.cache) args.push('--cache', opts.cache)
if (opts.userconfig) args.push('--userconfig', opts.userconfig)
args.push('--loglevel', 'error')

return args
}
Expand Down
15 changes: 12 additions & 3 deletions parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,38 @@ function parseArgs () {
}
if (cmdIndex) {
const parsed = parser.parse(process.argv.slice(0, cmdIndex))
const parsedCmd = npa(process.argv[cmdIndex])
parsed.command = parsed.package
? process.argv[cmdIndex]
: npa(process.argv[cmdIndex]).name
: parsedCmd.name
parsed.cmdOpts = process.argv.slice(cmdIndex + 1)
parsed.packageRequested = !!parsed.package
parsed.cmdHadVersion = parsedCmd.name !== parsedCmd.raw
const pkg = parsed.package || process.argv[cmdIndex]
parsed.p = parsed.package = npa(pkg).toString()
return parsed
} else {
const parsed = parser.argv
if (parsed.call) {
const splitCmd = parsed.call.trim().split(/\s+/)
const parsedCmd = npa(splitCmd[0])
parsed.command = parsed.package
? splitCmd[0]
: npa(splitCmd[0]).name
: parsedCmd.name
parsed.cmdOpts = splitCmd.slice(1)
parsed.packageRequested = !!parsed.package
parsed.cmdHadVersion = parsedCmd.name !== parsedCmd.raw
const pkg = parsed.package || splitCmd[0]
parsed.p = parsed.package = npa(pkg).toString()
} else if (hasDashDash) {
const splitCmd = parsed._
const parsedCmd = npa(splitCmd[0])
parsed.command = parsed.package
? splitCmd[0]
: npa(splitCmd[0]).name
: parsedCmd.name
parsed.cmdOpts = splitCmd.slice(1)
parsed.packageRequested = !!parsed.package
parsed.cmdHadVersion = parsedCmd.name !== parsedCmd.raw
}
return parsed
}
Expand Down

0 comments on commit 9668c83

Please sign in to comment.