From 0866a838e464d0d6168c8465840fb96c4929d4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 2 Jun 2017 17:41:15 -0700 Subject: [PATCH] feat(ignore): add --ignore-existing option (#20) Fixes: #15 Fixes: #16 --- README.md | 10 +++++++--- index.js | 3 ++- parse-args.js | 6 +++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5c8f364..324b064 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## SYNOPSIS -`npx [--package|-p ] [--cache ] [--save-dev|-D] [--save-prod|-P] [--save-optional|-O] [--save-bundle|-B] [--save-exact|-E] [--global|-g] [--prefix|-C] [--userconfig ] [-c ] [--version|-v] [--] [@version] [command-arg]...` +`npx [--package|-p ] [--cache ] [--save-dev|-D] [--save-prod|-P] [--save-optional|-O] [--save-bundle|-B] [--save-exact|-E] [--global|-g] [--prefix|-C] [--userconfig ] [-c ] [--ignore-existing] [--version|-v] [--] [@version] [command-arg]...` ## INSTALL @@ -14,9 +14,11 @@ Executes `` either from a local `node_modules/.bin`, or from a central cache, installing any packages needed in order for `` to run. -By default, `` will be installed prior to execution. An optional `@version` may be appended to specify the package version required. +By default, `npx` will check whether `` exists in `$PATH`, or in the local project binaries, and execute that. If `` is not found, it will be installed prior to execution. -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. +An optional `@version` may be appended to specify the package version required, which defaults to `latest` only if `` is not in the path. If the command is already present and no explicit version specifier was requested, the existing command will be used. + +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. This can also be forced with the `--ignore-existing` flag. * `-p, --package ` - define the package to be installed. This defaults to the value of ``. 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 `` will be executed as-is, without interpreting `@version` if it's there. @@ -36,6 +38,8 @@ If a version specifier is included, or if `--package` is used, npx will ignore t * `--shell-auto-fallback [shell]` - Generates shell code to override your shell's "command not found" handler with one that calls `npx`. Tries to figure out your shell, or you can pass its name (either `bash`, `fish`, or `zsh`) as an option. See below for how to install. +* `--ignore-existing` - If this flag is set, npx will not look in `$PATH`, or in the current package's `node_modules/.bin` for an existing version before deciding whether to install. Binaries in those paths will still be available for execution, but will be shadowed by any packages requested by this install. + * `-v, --version` - Show the current npx version. ## EXAMPLES diff --git a/index.js b/index.js index 8cc5df1..2c07fe2 100755 --- a/index.js +++ b/index.js @@ -80,7 +80,8 @@ function getExistingPath (command, opts) { opts.global || opts.prefix || opts.cmdHadVersion || - opts.packageRequested + opts.packageRequested || + opts.ignoreExisting ) { return BB.resolve(false) } else { diff --git a/parse-args.js b/parse-args.js index a768366..95feb29 100644 --- a/parse-args.js +++ b/parse-args.js @@ -3,7 +3,7 @@ const npa = require('npm-package-arg') const yargs = require('yargs') -const usage = `$0 [--package|-p ] [--cache ] [--save-dev|-D] [--save-prod|-P] [--save-optional|-O] [--save-bundle|-B] [--save-exact|-E] [--global|-g] [--prefix|-C] [--userconfig ] [-c ] [--shell-auto-fallback [shell]] [--version|-v] [--] [@version] [command-arg]...` +const usage = `$0 [--package|-p ] [--cache ] [--save-dev|-D] [--save-prod|-P] [--save-optional|-O] [--save-bundle|-B] [--save-exact|-E] [--global|-g] [--prefix|-C] [--userconfig ] [-c ] [--shell-auto-fallback [shell]] [--ignore-existing] [--version|-v] [--] [@version] [command-arg]...` module.exports = parseArgs function parseArgs () { @@ -74,6 +74,10 @@ function parseArgs () { requireArg: false, type: 'string' }) + .option('ignore-existing', { + 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' + }) .version() .alias('version', 'v')