UNPKG

4.76 kBMarkdownView Raw
1| [index](../README.md) | [npm-run-all](npm-run-all.md) | [run-s](run-s.md) | [run-p](run-p.md) | Node API |
2|-----------------------|-------------------------------|-------------------|-------------------|----------|
3
4# Node API
5
6A Node module to run given npm-scripts in parallel or sequential.
7
8```js
9const runAll = require("npm-run-all");
10
11runAll(["clean", "lint", "build:*"], {parallel: false})
12 .then(() => {
13 console.log("done!");
14 })
15 .catch(err => {
16 console.log("failed!");
17 });
18
19runAll(["build:* -- --watch"], {parallel: true})
20 .then(() => {
21 console.log("done!");
22 })
23 .catch(err => {
24 console.log("failed!");
25 });
26```
27
28## runAll
29
30```
31let promise = runAll(patterns, options);
32```
33
34Run npm-scripts.
35
36- **patterns** `string|string[]` -- Glob-like patterns for script names.
37- **options** `object`
38 - **options.arguments** `string[]` --
39 An argument list to replace argument placeholders (such as `{1}`, `{2}`). If pattern text has `{1}`, it's replaced by `options.arguments[0]`.
40 Default is an empty array.
41 - **options.continueOnError** `boolean` --
42 The flag to continue executing other/subsequent scripts even if a script threw an error.
43 Returned `Promise` object will be rejected if one or more scripts threw error(s).
44 Default is `false`.
45 - **options.parallel** `boolean` --
46 The flag to run scripts in parallel.
47 Default is `false`.
48 - **options.maxParallel** `number` --
49 The maximum number of parallelism.
50 Default is `Number.POSITIVE_INFINITY`.
51 - **options.npmPath** `string` --
52 The path to npm.
53 Default is `process.env.npm_execpath` or `"npm"`.
54 - **options.packageConfig** `object|null` --
55 The map-like object to overwrite package configs.
56 Keys are package names.
57 Every value is a map-like object (Pairs of variable name and value).
58 e.g. `{"npm-run-all": {"test": 777, "test2": 333}}`
59 Default is `null`.
60 - **options.printLabel** `boolean` --
61 Set the flag to print the task name as a prefix on each line of output.
62 Tools in scripts may stop coloring their output if this option is given.
63 Default is `false`.
64 - **options.printName** `boolean` --
65 Set the flag to print the task name before running each task.
66 Default is `false`.
67 - **options.race** `boolean` --
68 Set the flag to kill all npm-scripts when a npm-script finished with zero.
69 This option is valid only with `options.parallel` option.
70 Default is `false`.
71 - **options.silent** `boolean` --
72 The flag to set `silent` to the log level of npm.
73 Default is `false`.
74 - **options.stdin** `stream.Readable|null` --
75 The readable stream to send to the stdin of npm-scripts.
76 Default is nothing.
77 Set `process.stdin` in order to send from stdin.
78 - **options.stdout** `stream.Writable|null` --
79 The writable stream to receive from the stdout of npm-scripts.
80 Default is nothing.
81 Set `process.stdout` in order to print to stdout.
82 - **options.stderr** `stream.Writable|null` --
83 The writable stream to receive from the stderr of npm-scripts
84 Default is nothing.
85 Set `process.stderr` in order to print to stderr.
86 - **options.taskList** `string[]|null` --
87 The string array of all script names.
88 If this is `null`, it reads from `package.json` in the current directory.
89 Default is `null`.
90
91`runAll` returns a promise that will becomes *fulfilled* when all scripts are completed.
92The promise will become *rejected* when any of the scripts exit with a non-zero code.
93
94The promise gives `results` to the fulfilled handler.
95`results` is an array of objects which have 2 properties: `name` and `code`.
96The `name` property is the name of a npm-script.
97The `code` property is the exit code of the npm-script. If the npm-script was not executed, the `code` property is `undefined`.
98
99```js
100runAll(["clean", "lint", "build"])
101 .then(results => {
102 console.log(`${results[0].name}: ${results[0].code}`); // clean: 0
103 console.log(`${results[1].name}: ${results[1].code}`); // lint: 0
104 console.log(`${results[2].name}: ${results[2].code}`); // build: 0
105 });
106```
107
108## About MaxListenersExceededWarning
109
110- If you use `options.stdin`, `options.stdout`, or `options.stderr` in parallel mode, please configure max listeners by [emitter.setMaxListeners(n)](https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n) properly.
111- If you don't use those options and `process.stdXXX.isTTY` is `false`, please configure max listeners of the `process.stdXXX` properly. In that case, `npm-run-all` uses piping to connect to child processes.<br>
112 On the other hand, if `process.stdXXX.isTTY` is `true`, `npm-run-all` uses `inherit` option, so the configuration is unnecessary.