提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.9 KiB

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