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

Commit

Permalink
Merge 48a0847 into 8a0b3f6
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jul 12, 2017
2 parents 8a0b3f6 + 48a0847 commit 36d1157
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ If a full specifier is included, or if `--package` is used, npx will always use

* `-q, --quiet` - Suppressed any output from npx itself (progress bars, error messages, install reports). Subcommand output itself will not be silenced.

* `-n, --node-arg` - Extra node argument to supply to node when binary is a node script. You can supply this option multiple times to add more arguments.

* `-v, --version` - Show the current npx version.

## EXAMPLES
Expand Down Expand Up @@ -89,6 +91,13 @@ $ npx -p lolcatjs -p cowsay -c \
|| ||
```

### Run node binary with --inspect

```
$ npx --node-arg=--inspect cowsay
Debugger listening on ws://127.0.0.1:9229/....
```

## SHELL AUTO FALLBACK

You can configure `npx` to run as your default fallback command when you type something in the command line with an `@` but the command is not found. This includes installing packages that were not found in the local prefix either.
Expand Down
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ function installPackages (specs, prefix, opts) {
module.exports._execCommand = execCommand
function execCommand (_existing, argv) {
return findNodeScript(_existing, argv).then(existing => {
const Module = require('module')
if (existing && Module.runMain && !argv.shell && existing !== process.argv[1]) {
if (existing && !argv.nodeArg && !argv.shell && existing !== process.argv[1]) {
const Module = require('module')
// let it take over the process. This means we can skip node startup!
if (!argv.noYargs) {
// blow away built-up yargs crud
Expand All @@ -245,8 +245,24 @@ function execCommand (_existing, argv) {
existing // node script path. `runMain()` will set this as the new main
].concat(argv.cmdOpts) // options for the cmd itself
Module.runMain() // ✨MAGIC✨. Sorry-not-sorry
} else if (!existing && argv.nodeArg && argv.nodeArg.length) {
throw new Error(Y()`ERROR: --node-arg/-n can only be used on packages with node scripts.`)
} else {
return child.runCommand(existing, argv).catch(err => {
let cmd = existing
let opts = argv
if (existing && argv.nodeArg && argv.nodeArg.length) {
// If we know we're running a run script and we got a --node-arg,
// we need to fudge things a bit to get them working right.
let nargs = argv.nodeArg
if (typeof nargs === 'string') {
nargs = [nargs]
}
cmd = process.argv[0]
opts = Object.assign({}, argv, {
cmdOpts: nargs.concat([existing], argv.cmdOpts || [])
})
}
return child.runCommand(cmd, opts).catch(err => {
if (err.isOperational && err.exitCode) {
// At this point, we want to treat errors from the child as if
// we were just running the command. That means no extra msg logging
Expand Down
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"shell": "shell",
"package": "package",
"npx: installed %s in %ss": "npx: installed %s in %ss",
"Suppress output from npx itself. Subcommands will not be affected.": "Suppress output from npx itself. Subcommands will not be affected."
"Suppress output from npx itself. Subcommands will not be affected.": "Suppress output from npx itself. Subcommands will not be affected.",
"Extra node argument when calling a node binary.": "Extra node argument when calling a node binary."
}
11 changes: 10 additions & 1 deletion parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ function parseArgs (argv, defaultNpm) {
if (opt === '--') {
hasDashDash = true
break
} else if (opt === '--node-arg' || opt === '-n') {
argv[i] = `${opt}=${argv[i + 1]}`
argv.splice(i + 1, 1)
} else if (opt[0] === '-') {
if (
// --no-install needs to be special-cased because we're abusing
// yargs a bit in order to get the --help text right.
opt !== '--no-install' &&
!bools.has(opt.replace(/^--?(no-)?/i, ''))
!bools.has(opt.replace(/^--?(no-)?/i, '')) &&
opt.indexOf('=') === -1
) {
i++
}
Expand Down Expand Up @@ -214,6 +218,11 @@ function yargsParser (argv, defaultNpm) {
type: 'string',
default: defaultNpm || 'npm'
})
.option('node-arg', {
alias: 'n',
type: 'string',
describe: Y()`Extra node argument when calling a node binary.`
})
.version(() => require('./package.json').version)
.alias('version', 'v')
.help()
Expand Down

0 comments on commit 36d1157

Please sign in to comment.