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

Commit

Permalink
feat(call): -c now loads same env as run-script
Browse files Browse the repository at this point in the history
Fixes: #3

BREAKING CHANGE: scripts invoked with -c will now have a bunch of
variables added to them that were not there before.
  • Loading branch information
zkat committed Jun 3, 2017
1 parent e5d5634 commit 76ae44c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ If a full specifier is included, or if `--package` is used, npx will always use

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

* `-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`.
* `-c <string>` - Execute `<string>` inside an `npm run-script`-like shell environment, with all the usual environment variables available. 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 <string>` - The shell to invoke the command with, if any.

* `--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 @@ -68,10 +68,11 @@ $ npx git+ssh://my.hosted.git:cowsay.git#semver:^1
### Execute a full shell command using one npx call w/ multiple packages

```
$ npx -p lolcatjs -p cowsay -c 'echo "foo" | cowsay | lolcatjs'
$ npx -p lolcatjs -p cowsay -c \
'echo "$npm_package_name@$npm_package_version" | cowsay | lolcatjs'
...
_____
< foo >
< your-cool-package@1.2.3 >
-----
\ ^__^
\ (oo)\_______
Expand Down
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const BB = require('bluebird')

const child = require('./child')
const dotenv = require('dotenv')
const getPrefix = require('./get-prefix.js')
const parseArgs = require('./parse-args.js')
const path = require('path')
Expand Down Expand Up @@ -40,11 +41,14 @@ function main (argv) {

return localBinPath(process.cwd()).then(local => {
process.env.PATH = `${local}${PATH_SEP}${process.env.PATH}`
return getCmdPath(
argv.command, argv.package, argv
).then(cmdPath => {
return child.runCommand(cmdPath, argv.cmdOpts, argv)
}).catch(err => {
return BB.join(
getCmdPath(argv.command, argv.package, argv),
getEnv(argv),
(cmdPath, env) => {
process.env = env
return child.runCommand(cmdPath, argv.cmdOpts, argv)
}
).catch(err => {
console.error(err.message)
process.exit(err.exitCode || 1)
})
Expand All @@ -58,6 +62,17 @@ function localBinPath (cwd) {
})
}

module.exports._getEnv = getEnv
function getEnv (opts) {
if (opts.call) {
return child.exec(opts.npm, ['run', 'env']).then(env => {
return dotenv.parse(env)
})
} else {
return process.env
}
}

module.exports._getCmdPath = getCmdPath
function getCmdPath (command, specs, npmOpts) {
return getExistingPath(command, npmOpts).then(cmdPath => {
Expand Down
11 changes: 8 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"license": "CC0-1.0",
"dependencies": {
"bluebird": "^3.5.0",
"dotenv": "^4.0.0",
"npm": "^5.0.2",
"npm-package-arg": "^5.0.1",
"rimraf": "^2.6.1",
Expand All @@ -50,7 +51,8 @@
"rimraf",
"update-notifier",
"which",
"yargs"
"yargs",
"dotenv"
],
"devDependencies": {
"marked-man": "^0.2.1",
Expand Down

0 comments on commit 76ae44c

Please sign in to comment.