Skip to content

Commit

Permalink
Breaking: use NPM_EXECPATH to run tasks
Browse files Browse the repository at this point in the history
And add --npm-path option.
  • Loading branch information
mysticatea committed Dec 31, 2016
1 parent 07bfec5 commit 12b2b87
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 9 deletions.
9 changes: 7 additions & 2 deletions bin/common/parse-cli-args.js
Expand Up @@ -77,17 +77,18 @@ class ArgumentSet {
* @param {object} options - A key-value map for the options.
*/
constructor(initialValues, options) {
this.config = {}
this.continueOnError = false
this.groups = []
this.maxParallel = 0
this.npmPath = null
this.packageConfig = createPackageConfig()
this.printLabel = false
this.printName = false
this.race = false
this.rest = []
this.silent = process.env.npm_config_loglevel === "silent"
this.singleMode = Boolean(options && options.singleMode)
this.packageConfig = createPackageConfig()
this.config = {}

addGroup(this.groups, initialValues)
}
Expand Down Expand Up @@ -181,6 +182,10 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
addGroup(set.groups, {parallel: true})
break

case "--npm-path":
set.npmPath = args[++i] || null
break

default: {
let matched = null
if ((matched = OVERWRITE_OPTION.exec(arg))) {
Expand Down
5 changes: 5 additions & 0 deletions bin/npm-run-all/help.js
Expand Up @@ -33,6 +33,11 @@ Options:
non-zero code if one or more tasks threw error(s)
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
1 change: 1 addition & 0 deletions bin/npm-run-all/main.js
Expand Up @@ -51,6 +51,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
silent: argv.silent,
arguments: argv.rest,
race: argv.race,
npmPath: argv.npmPath,
}
))
},
Expand Down
5 changes: 5 additions & 0 deletions bin/run-p/help.js
Expand Up @@ -33,6 +33,11 @@ Options:
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
1 change: 1 addition & 0 deletions bin/run-p/main.js
Expand Up @@ -51,6 +51,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
silent: argv.silent,
arguments: argv.rest,
race: argv.race,
npmPath: argv.npmPath,
}
)

Expand Down
5 changes: 5 additions & 0 deletions bin/run-s/help.js
Expand Up @@ -31,6 +31,11 @@ Options:
tasks even if a task threw an error. 'run-s'
itself will exit with non-zero code if one or
more tasks threw error(s).
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
1 change: 1 addition & 0 deletions bin/run-s/main.js
Expand Up @@ -49,6 +49,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
packageConfig: argv.packageConfig,
silent: argv.silent,
arguments: argv.rest,
npmPath: argv.npmPath,
}
)

Expand Down
3 changes: 3 additions & 0 deletions docs/node-api.md
Expand Up @@ -48,6 +48,9 @@ Run npm-scripts.
- **options.maxParallel** `number` --
The maximum number of parallelism.
Default is `Number.POSITIVE_INFINITY`.
- **options.npmPath** `string` --
The path to npm.
Default is `process.env.NPM_EXECPATH` or `"npm"`.
- **options.packageConfig** `object|null` --
The map-like object to overwrite package configs.
Keys are package names.
Expand Down
5 changes: 5 additions & 0 deletions docs/npm-run-all.md
Expand Up @@ -38,6 +38,11 @@ Options:
non-zero code if one or more tasks threw error(s)
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
5 changes: 5 additions & 0 deletions docs/run-p.md
Expand Up @@ -37,6 +37,11 @@ Options:
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
5 changes: 5 additions & 0 deletions docs/run-s.md
Expand Up @@ -35,6 +35,11 @@ Options:
tasks even if a task threw an error. 'run-s'
itself will exit with non-zero code if one or
more tasks threw error(s).
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable NPM_EXECPATH.
If the variable is not defined, then it's "npm."
In this case, the "npm" command must be found in
environment variable PATH.
-l, --print-label - - - - Set the flag to print the task name as a prefix
on each line of output. Tools in tasks may stop
coloring their output if this option was given.
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Expand Up @@ -199,6 +199,12 @@ function maxLength(length, name) {
* @param {boolean} options.printName -
* The flag to print task names before running each task.
* Default is `false`.
* @param {number} options.maxParallel -
* The maximum number of parallelism.
* Default is unlimited.
* @param {string} options.npmPath -
* The path to npm.
* Default is `process.env.NPM_EXECPATH`.
* @returns {Promise}
* A promise object which becomes fullfilled when all npm-scripts are completed.
*/
Expand All @@ -217,6 +223,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
const printName = Boolean(options && options.printName)
const race = Boolean(options && options.race)
const maxParallel = parallel ? ((options && options.maxParallel) || 0) : 1
const npmPath = options && options.npmPath
try {
const patterns = parsePatterns(patternOrPatterns, args)
if (patterns.length === 0) {
Expand Down Expand Up @@ -265,6 +272,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
packageInfo: x.packageInfo,
race,
maxParallel,
npmPath,
})
})
}
Expand Down
31 changes: 25 additions & 6 deletions lib/run-task.js
Expand Up @@ -10,6 +10,7 @@
// Requirements
//------------------------------------------------------------------------------

const path = require("path")
const chalk = require("chalk")
const parseArgs = require("shell-quote").parse
const padEnd = require("string.prototype.padend")
Expand Down Expand Up @@ -123,6 +124,7 @@ module.exports = function runTask(task, options) {
const stdinKind = detectStreamKind(stdin, process.stdin)
const stdoutKind = detectStreamKind(stdout, process.stdout)
const stderrKind = detectStreamKind(stderr, process.stderr)
const spawnOptions = {stdio: [stdinKind, stdoutKind, stderrKind]}

// Print task name.
if (options.printName && stdout != null) {
Expand All @@ -133,12 +135,29 @@ module.exports = function runTask(task, options) {
))
}

// Execute.
cp = spawn(
"npm",
["run"].concat(options.prefixOptions, parseArgs(task)),
{stdio: [stdinKind, stdoutKind, stderrKind]}
)
if (path.extname(options.npmPath || "a.js") === ".js") {
const npmPath = options.npmPath || process.env.NPM_EXECPATH //eslint-disable-line no-process-env
const execPath = npmPath ? process.execPath : "npm"
const spawnArgs = [].concat(
npmPath ? [npmPath, "run"] : ["run"],
options.prefixOptions,
parseArgs(task)
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
}
else {
const execPath = options.npmPath
const spawnArgs = [].concat(
["run"],
options.prefixOptions,
parseArgs(task)
)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
}

// Piping stdio.
if (stdinKind === "pipe") {
Expand Down
5 changes: 4 additions & 1 deletion test-workspace/package.json
Expand Up @@ -33,7 +33,10 @@
"test-task:issue14:win32": "..\\node_modules\\.bin\\rimraf build && mkdir %npm_package_config_DEST% && cd build",
"test-task:issue14:posix": "../node_modules/.bin/rimraf build && mkdir $npm_package_config_DEST && cd build",
"test-task:echo": "node tasks/echo.js",
"test-task:dump": "node tasks/dump.js"
"test-task:dump": "node tasks/dump.js",
"test-task:nest-append:npm-run-all": "node ../bin/npm-run-all/index.js test-task:append",
"test-task:nest-append:run-s": "node ../bin/run-s/index.js test-task:append",
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append"
},
"repository": {
"type": "git",
Expand Down
18 changes: 18 additions & 0 deletions test/lib/util.js
Expand Up @@ -22,6 +22,7 @@ const FILE_NAME = "test.txt"
const NPM_RUN_ALL = path.resolve(__dirname, "../../bin/npm-run-all/index.js")
const RUN_P = path.resolve(__dirname, "../../bin/run-p/index.js")
const RUN_S = path.resolve(__dirname, "../../bin/run-s/index.js")
const YARN = path.resolve(__dirname, "../../node_modules/yarn/bin/yarn.js")

/**
* Spawns the given script with the given arguments.
Expand All @@ -40,6 +41,7 @@ function spawn(filePath, args, stdout, stderr) {
[filePath].concat(args),
{stdio: "pipe"}
)
const out = new BufferStream()
const error = new BufferStream()

if (stdout != null) {
Expand All @@ -51,6 +53,7 @@ function spawn(filePath, args, stdout, stderr) {
else {
child.stderr.pipe(error)
}
child.stdout.pipe(out)
child.on("close", (exitCode) => {
if (exitCode) {
reject(new Error(error.value || "Exited with non-zero code."))
Expand Down Expand Up @@ -161,3 +164,18 @@ module.exports.runPar = function runPar(args, stdout, stderr) {
module.exports.runSeq = function runSeq(args, stdout, stderr) {
return spawn(RUN_S, args, stdout, stderr)
}

/**
* Executes `yarn run` with the given arguments.
*
* @param {string[]} args - The arguments to execute.
* @param {Writable} [stdout] - The writable stream to receive stdout.
* @param {Writable} [stderr] - The writable stream to receive stderr.
* @returns {Promise<void>} The promise which becomes fulfilled if the child
* process finished.
*/
module.exports.runWithYarn = function runWithYarn(args, stdout, stderr) {
return spawn(YARN, ["run"].concat(args), stdout, stderr)
}

module.exports.YARN_PATH = YARN

0 comments on commit 12b2b87

Please sign in to comment.