From 76ae44c953b6bde6d976840a5cb21d9c771a8a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 3 Jun 2017 16:40:14 -0700 Subject: [PATCH] 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. --- README.md | 9 +++++---- index.js | 25 ++++++++++++++++++++----- package-lock.json | 11 ++++++++--- package.json | 4 +++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a28a5aa..c2ae25b 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ If a full specifier is included, or if `--package` is used, npx will always use * `--userconfig ` - path to the user configuration file to pass to npm. Defaults to whatever npm's current default is. -* `-c ` - Execute `` inside a shell. For unix, this will be `/bin/sh -c `. For Windows, it will be `cmd.exe /d /s /c `. Only the first item in `` will be automatically used as ``. Any others _must_ use `-p`. +* `-c ` - Execute `` inside an `npm run-script`-like shell environment, with all the usual environment variables available. Only the first item in `` will be automatically used as ``. Any others _must_ use `-p`. -* `--shell ` - The shell to invoke the command with, if any. Defaults to `false`. +* `--shell ` - The shell to invoke the command with, if any. * `--shell-auto-fallback []` - 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. @@ -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)\_______ diff --git a/index.js b/index.js index d2a84c0..55ce929 100755 --- a/index.js +++ b/index.js @@ -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') @@ -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) }) @@ -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 => { diff --git a/package-lock.json b/package-lock.json index f347da5..a65dd69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -674,6 +674,11 @@ "integrity": "sha1-G3CK8JSknJoOfbyteQq6U52sEXc=", "dev": true }, + "dotenv": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-4.0.0.tgz", + "integrity": "sha1-hk7xN5rO1Vzm+V3r7NzhefegzR0=" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", @@ -4642,9 +4647,9 @@ "dev": true }, "safe-buffer": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz", - "integrity": "sha1-0mPKVGls2KMGtcplUekt5XkY++c=" + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.0.tgz", + "integrity": "sha512-aSLEDudu6OoRr/2rU609gRmnYboRLxgDG1z9o2Q0os7236FwvcqIOO8r8U5JUEwivZOhDaKlFO4SbPTJYyBEyQ==" }, "semver": { "version": "5.3.0", diff --git a/package.json b/package.json index d799583..20d9f6f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -50,7 +51,8 @@ "rimraf", "update-notifier", "which", - "yargs" + "yargs", + "dotenv" ], "devDependencies": { "marked-man": "^0.2.1",