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

Commit

Permalink
feat(node): add ability to inspect binaries when detected to be node …
Browse files Browse the repository at this point in the history
…scripts

Fixes: #68
  • Loading branch information
zkat committed Jul 12, 2017
1 parent 370f395 commit 765750a
Show file tree
Hide file tree
Showing 4 changed files with 35 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
20 changes: 17 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 @@ -246,7 +246,21 @@ function execCommand (_existing, argv) {
].concat(argv.cmdOpts) // options for the cmd itself
Module.runMain() // ✨MAGIC✨. Sorry-not-sorry
} 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."
}
8 changes: 7 additions & 1 deletion parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function parseArgs (argv, defaultNpm) {
// --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 +215,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 765750a

Please sign in to comment.