Skip to content

Commit

Permalink
New: --max-parallel option
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Dec 31, 2016
1 parent 58f9d4c commit 02e9767
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 176 deletions.
22 changes: 16 additions & 6 deletions bin/common/parse-cli-args.js
Expand Up @@ -79,6 +79,7 @@ class ArgumentSet {
constructor(initialValues, options) {
this.continueOnError = false
this.groups = []
this.maxParallel = 0
this.printLabel = false
this.printName = false
this.race = false
Expand Down Expand Up @@ -123,6 +124,11 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
set.rest = args.slice(1 + i)
break LOOP

case "--color":
case "--no-color":
// do nothing.
break

case "-c":
case "--continue-on-error":
set.continueOnError = true
Expand All @@ -147,9 +153,11 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
set.silent = true
break

case "--color":
case "--no-color":
// do nothing.
case "--max-parallel":
set.maxParallel = parseInt(args[++i], 10)
if (!Number.isFinite(set.maxParallel) || set.maxParallel <= 0) {
throw new Error(`Invalid Option: --max-parallel ${args[i]}`)
}
break

case "-s":
Expand Down Expand Up @@ -205,9 +213,11 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
}

if (!set.parallel && set.race) {
throw new Error(`Invalid Option: ${
args.indexOf("--race") !== -1 ? "race" : "r"
} (without parallel)`)
const race = args.indexOf("--race") !== -1 ? "race" : "r"
throw new Error(`Invalid Option: ${race} (without parallel)`)
}
if (!set.parallel && set.maxParallel !== 0) {
throw new Error("Invalid Option: --max-parallel (without parallel)")
}

return set
Expand Down
2 changes: 2 additions & 0 deletions bin/npm-run-all/help.js
Expand Up @@ -31,6 +31,8 @@ Options:
other/subsequent tasks even if a task threw an
error. 'npm-run-all' itself will exit with
non-zero code if one or more tasks threw error(s)
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
-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 @@ -42,6 +42,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
stderr,
stdin,
parallel: group.parallel,
maxParallel: argv.maxParallel,
continueOnError: argv.continueOnError,
printLabel: argv.printLabel,
printName: argv.printName,
Expand Down
2 changes: 2 additions & 0 deletions bin/run-p/help.js
Expand Up @@ -31,6 +31,8 @@ Options:
even if a task threw an error. 'run-p' itself
will exit with non-zero code if one or more tasks
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
-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 @@ -42,6 +42,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
stderr,
stdin,
parallel: group.parallel,
maxParallel: argv.maxParallel,
continueOnError: argv.continueOnError,
printLabel: argv.printLabel,
printName: argv.printName,
Expand Down
3 changes: 3 additions & 0 deletions docs/node-api.md
Expand Up @@ -45,6 +45,9 @@ Run npm-scripts.
- **options.parallel** `boolean` --
The flag to run scripts in parallel.
Default is `false`.
- **options.maxParallel** `number` --
The maximum number of parallelism.
Default is `Number.POSITIVE_INFINITY`.
- **options.packageConfig** `object|null` --
The map-like object to overwrite package configs.
Keys are package names.
Expand Down
8 changes: 5 additions & 3 deletions docs/npm-run-all.md
Expand Up @@ -36,6 +36,8 @@ Options:
other/subsequent tasks even if a task threw an
error. 'npm-run-all' itself will exit with
non-zero code if one or more tasks threw error(s)
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
-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 Expand Up @@ -109,7 +111,7 @@ npm-run-all clean lint --parallel watch:html watch:js
```

1. First, this runs `clean` and `lint` sequentially / serially.
2. Next, runs `watch:html` and `watch:js` in parallell.
2. Next, runs `watch:html` and `watch:js` in parallel.

```
npm-run-all a b --parallel c d --sequential e f --parallel g h i
Expand All @@ -121,9 +123,9 @@ npm-run-all a b --parallel c d --serial e f --parallel g h i
```

1. First, runs `a` and `b` sequentially / serially.
2. Second, runs `c` and `d` in parallell.
2. Second, runs `c` and `d` in parallel.
3. Third, runs `e` and `f` sequentially / serially.
4. Lastly, runs `g`, `h`, and `i` in parallell.
4. Lastly, runs `g`, `h`, and `i` in parallel.

### Glob-like pattern matching for script names

Expand Down
2 changes: 2 additions & 0 deletions docs/run-p.md
Expand Up @@ -35,6 +35,8 @@ Options:
even if a task threw an error. 'run-p' itself
will exit with non-zero code if one or more tasks
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
-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
15 changes: 10 additions & 5 deletions lib/index.js
Expand Up @@ -13,8 +13,7 @@
const shellQuote = require("shell-quote")
const matchTasks = require("./match-tasks")
const readPackageJson = require("./read-package-json")
const runTasksInParallel = require("./run-tasks-in-parallel")
const runTasksInSequencial = require("./run-tasks-in-sequencial")
const runTasks = require("./run-tasks")

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -204,28 +203,34 @@ function maxLength(length, name) {
* A promise object which becomes fullfilled when all npm-scripts are completed.
*/
module.exports = function npmRunAll(patternOrPatterns, options) {
const parallel = Boolean(options && options.parallel)
const stdin = (options && options.stdin) || null
const stdout = (options && options.stdout) || null
const stderr = (options && options.stderr) || null
const taskList = (options && options.taskList) || null
const config = (options && options.config) || null
const packageConfig = (options && options.packageConfig) || null
const args = (options && options.arguments) || []
const parallel = Boolean(options && options.parallel)
const silent = Boolean(options && options.silent)
const continueOnError = Boolean(options && options.continueOnError)
const printLabel = Boolean(options && options.printLabel)
const printName = Boolean(options && options.printName)
const race = Boolean(options && options.race)
const maxParallel = parallel ? ((options && options.maxParallel) || 0) : 1
try {
const patterns = parsePatterns(patternOrPatterns, args)
if (patterns.length === 0) {
return Promise.resolve(null)
}

if (taskList != null && Array.isArray(taskList) === false) {
throw new Error("Invalid options.taskList")
}
if (typeof maxParallel !== "number" || !(maxParallel >= 0)) {
throw new Error("Invalid options.maxParallel")
}
if (!parallel && race) {
throw new Error("Invalid options.race")
}

const prefixOptions = [].concat(
silent ? ["--silent"] : [],
Expand All @@ -243,7 +248,6 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
.then(x => {
const tasks = matchTasks(x.taskList, patterns)
const labelWidth = tasks.reduce(maxLength, 0)
const runTasks = parallel ? runTasksInParallel : runTasksInSequencial

return runTasks(tasks, {
stdin,
Expand All @@ -260,6 +264,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
printName,
packageInfo: x.packageInfo,
race,
maxParallel,
})
})
}
Expand Down
83 changes: 0 additions & 83 deletions lib/run-tasks-in-parallel.js

This file was deleted.

75 changes: 0 additions & 75 deletions lib/run-tasks-in-sequencial.js

This file was deleted.

0 comments on commit 02e9767

Please sign in to comment.