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

Commit

Permalink
feat(shell): run -c strings inside a system shell (#22)
Browse files Browse the repository at this point in the history
Fixes: #21
  • Loading branch information
zkat committed Jun 3, 2017
1 parent 045a593 commit 17db461
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ If a version specifier is included, or if `--package` is used, npx will ignore t

* `--userconfig` - path to the user configuration file to pass to npm. Defaults to whatever npm's current default is.

* `-c <string>` - Execute `<string>` with delayed environment variable evaluation.
* `-c <string>` - Execute `<string>` inside a shell. For unix, this will be `/bin/sh -c <string>`. For Windows, it will be `cmd.exe /d /s /c <string>`. Only the first item in `<string>` will be automatically used as `<command>`. Any others _must_ use `-p`.

* `--shell <string>` - The shell to invoke the command with, if any. Defaults to `false`.

* `--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.

Expand Down Expand Up @@ -62,6 +64,12 @@ $ cat package.json
...webpack added to "devDependencies"
```

### Execute a full shell command using one npx call

```
$ npx -p cowsay -c 'echo "foo" | cowsay'
```

## SHELL AUTO FALLBACK

To install permanently, add the relevant line to your `~/.bashrc`, `~/.zshrc`, `~/.config/fish/config.fish`, or as needed. To install just for the shell session, simply run the line.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function main (argv) {
return getCmdPath(
argv.command, argv.package, argv
).then(cmdPath => {
return runCommand(cmdPath, argv.cmdOpts)
return runCommand(cmdPath, argv.cmdOpts, argv)
}).catch(err => {
console.error(err.message)
process.exit(err.exitCode || 1)
Expand Down Expand Up @@ -137,8 +137,9 @@ function installPackage (spec, prefix, npmOpts) {
})
}

function runCommand (cmdPath, cmdOpts) {
function runCommand (cmdPath, cmdOpts, opts) {
return spawn(cmdPath, cmdOpts, {
shell: opts.shell || !!opts.call,
stdio: 'inherit'
}).catch({code: 'ENOENT'}, () => {
throw new Error(`npx: command not found: ${path.basename(cmdPath)}`)
Expand Down
6 changes: 6 additions & 0 deletions parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ function parseArgs () {
type: 'string',
describe: 'execute string as if inside `npm run-script`'
})
.option('shell', {
alias: 's',
type: 'string',
describe: 'Shell to execute the command with, if any.',
default: false
})
.option('shell-auto-fallback', {
choices: ['', 'bash', 'fish', 'zsh'],
describe: 'generate shell code to use npx as the "command not found" fallback',
Expand Down

0 comments on commit 17db461

Please sign in to comment.