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

Commit 76ae44c

Browse files
committed
feat(call): -c now loads same env as run-script
Fixes: #3 BREAKING CHANGE: scripts invoked with -c will now have a bunch of variables added to them that were not there before.
1 parent e5d5634 commit 76ae44c

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ If a full specifier is included, or if `--package` is used, npx will always use
2828

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

31-
* `-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`.
31+
* `-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`.
3232

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

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

@@ -68,10 +68,11 @@ $ npx git+ssh://my.hosted.git:cowsay.git#semver:^1
6868
### Execute a full shell command using one npx call w/ multiple packages
6969

7070
```
71-
$ npx -p lolcatjs -p cowsay -c 'echo "foo" | cowsay | lolcatjs'
71+
$ npx -p lolcatjs -p cowsay -c \
72+
'echo "$npm_package_name@$npm_package_version" | cowsay | lolcatjs'
7273
...
7374
_____
74-
< foo >
75+
< your-cool-package@1.2.3 >
7576
-----
7677
\ ^__^
7778
\ (oo)\_______

index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const BB = require('bluebird')
55

66
const child = require('./child')
7+
const dotenv = require('dotenv')
78
const getPrefix = require('./get-prefix.js')
89
const parseArgs = require('./parse-args.js')
910
const path = require('path')
@@ -40,11 +41,14 @@ function main (argv) {
4041

4142
return localBinPath(process.cwd()).then(local => {
4243
process.env.PATH = `${local}${PATH_SEP}${process.env.PATH}`
43-
return getCmdPath(
44-
argv.command, argv.package, argv
45-
).then(cmdPath => {
46-
return child.runCommand(cmdPath, argv.cmdOpts, argv)
47-
}).catch(err => {
44+
return BB.join(
45+
getCmdPath(argv.command, argv.package, argv),
46+
getEnv(argv),
47+
(cmdPath, env) => {
48+
process.env = env
49+
return child.runCommand(cmdPath, argv.cmdOpts, argv)
50+
}
51+
).catch(err => {
4852
console.error(err.message)
4953
process.exit(err.exitCode || 1)
5054
})
@@ -58,6 +62,17 @@ function localBinPath (cwd) {
5862
})
5963
}
6064

65+
module.exports._getEnv = getEnv
66+
function getEnv (opts) {
67+
if (opts.call) {
68+
return child.exec(opts.npm, ['run', 'env']).then(env => {
69+
return dotenv.parse(env)
70+
})
71+
} else {
72+
return process.env
73+
}
74+
}
75+
6176
module.exports._getCmdPath = getCmdPath
6277
function getCmdPath (command, specs, npmOpts) {
6378
return getExistingPath(command, npmOpts).then(cmdPath => {

package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"license": "CC0-1.0",
3737
"dependencies": {
3838
"bluebird": "^3.5.0",
39+
"dotenv": "^4.0.0",
3940
"npm": "^5.0.2",
4041
"npm-package-arg": "^5.0.1",
4142
"rimraf": "^2.6.1",
@@ -50,7 +51,8 @@
5051
"rimraf",
5152
"update-notifier",
5253
"which",
53-
"yargs"
54+
"yargs",
55+
"dotenv"
5456
],
5557
"devDependencies": {
5658
"marked-man": "^0.2.1",

0 commit comments

Comments
 (0)