提交学习笔记专用
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.

198 lines
7.2 KiB

  1. | [index](../README.md) | npm-run-all | [run-s](run-s.md) | [run-p](run-p.md) | [Node API](node-api.md) |
  2. |-----------------------|-------------|-------------------|-------------------|-------------------------|
  3. # `npm-run-all` command
  4. ```
  5. Usage:
  6. $ npm-run-all [--help | -h | --version | -v]
  7. $ npm-run-all [tasks] [OPTIONS]
  8. Run given npm-scripts in parallel or sequential.
  9. <tasks> : A list of npm-scripts' names and Glob-like patterns.
  10. Options:
  11. --aggregate-output - - - Avoid interleaving output by delaying printing of
  12. each command's output until it has finished.
  13. -c, --continue-on-error - Set the flag to continue executing
  14. other/subsequent tasks even if a task threw an
  15. error. 'npm-run-all' itself will exit with
  16. non-zero code if one or more tasks threw error(s)
  17. --max-parallel <number> - Set the maximum number of parallelism. Default is
  18. unlimited.
  19. --npm-path <string> - - - Set the path to npm. Default is the value of
  20. environment variable npm_execpath.
  21. If the variable is not defined, then it's "npm."
  22. In this case, the "npm" command must be found in
  23. environment variable PATH.
  24. -l, --print-label - - - - Set the flag to print the task name as a prefix
  25. on each line of output. Tools in tasks may stop
  26. coloring their output if this option was given.
  27. -n, --print-name - - - - Set the flag to print the task name before
  28. running each task.
  29. -p, --parallel <tasks> - Run a group of tasks in parallel.
  30. e.g. 'npm-run-all -p foo bar' is similar to
  31. 'npm run foo & npm run bar'.
  32. -r, --race - - - - - - - Set the flag to kill all tasks when a task
  33. finished with zero. This option is valid only
  34. with 'parallel' option.
  35. -s, --sequential <tasks> - Run a group of tasks sequentially.
  36. --serial <tasks> e.g. 'npm-run-all -s foo bar' is similar to
  37. 'npm run foo && npm run bar'.
  38. '--serial' is a synonym of '--sequential'.
  39. --silent - - - - - - - - Set 'silent' to the log level of npm.
  40. Examples:
  41. $ npm-run-all --serial clean lint build:**
  42. $ npm-run-all --parallel watch:**
  43. $ npm-run-all clean lint --parallel "build:** -- --watch"
  44. $ npm-run-all -l -p start-server start-browser start-electron
  45. ```
  46. ### npm-scripts
  47. It's `"scripts"` field of `package.json`.
  48. For example:
  49. ```json
  50. {
  51. "scripts": {
  52. "clean": "rm -rf dist",
  53. "lint": "eslint src",
  54. "build": "babel src -o lib"
  55. }
  56. }
  57. ```
  58. We can run a script with `npm run` command.
  59. On the other hand, this `npm-run-all` command runs multiple scripts in parallel or sequential.
  60. ### Run scripts sequentially
  61. ```
  62. $ npm-run-all clean lint build
  63. ```
  64. This is same as `npm run clean && npm run lint && npm run build`.
  65. **Note:** If a script exited with non zero code, the following scripts are not run.
  66. If `--continue-on-error` option is given, this behavior will be disabled.
  67. ### Run scripts in parallel
  68. ```
  69. $ npm-run-all --parallel lint build
  70. ```
  71. This is similar to `npm run lint & npm run build`.
  72. **Note1:** If a script exited with a non-zero code, the other scripts and those descendant processes are killed with `SIGTERM` (On Windows, with `taskkill.exe /F /T`).
  73. If `--continue-on-error` option is given, this behavior will be disabled.
  74. **Note2:** `&` operator does not work on Windows' `cmd.exe`. But `npm-run-all --parallel` works fine there.
  75. ### Run a mix of sequential and parallel scripts
  76. ```
  77. $ npm-run-all clean lint --parallel watch:html watch:js
  78. ```
  79. 1. First, this runs `clean` and `lint` sequentially / serially.
  80. 2. Next, runs `watch:html` and `watch:js` in parallel.
  81. ```
  82. $ npm-run-all a b --parallel c d --sequential e f --parallel g h i
  83. ```
  84. or
  85. ```
  86. $ npm-run-all a b --parallel c d --serial e f --parallel g h i
  87. ```
  88. 1. First, runs `a` and `b` sequentially / serially.
  89. 2. Second, runs `c` and `d` in parallel.
  90. 3. Third, runs `e` and `f` sequentially / serially.
  91. 4. Lastly, runs `g`, `h`, and `i` in parallel.
  92. ### Glob-like pattern matching for script names
  93. We can use [glob]-like patterns to specify npm-scripts.
  94. The difference is one -- the separator is `:` instead of `/`.
  95. ```
  96. $ npm-run-all --parallel watch:*
  97. ```
  98. In this case, runs sub scripts of `watch`. For example: `watch:html`, `watch:js`.
  99. But, doesn't run sub-sub scripts. For example: `watch:js:index`.
  100. ```
  101. $ npm-run-all --parallel watch:**
  102. ```
  103. If we use a globstar `**`, runs both sub scripts and sub-sub scripts.
  104. `npm-run-all` reads the actual npm-script list from `package.json` in the current directory, then filters the scripts by glob-like patterns, then runs those.
  105. ### Run with arguments
  106. We can enclose a script name or a pattern in quotes to use arguments.
  107. The following 2 commands are similar.
  108. ```
  109. $ npm-run-all --parallel "build:* -- --watch"
  110. $ npm run build:aaa -- --watch & npm run build:bbb -- --watch
  111. ```
  112. When we use a pattern, arguments are forwarded to every matched script.
  113. ### Argument placeholders
  114. We can use placeholders to give the arguments preceded by `--` to scripts.
  115. ```
  116. $ npm-run-all build "start-server -- --port {1}" -- 8080
  117. ```
  118. This is useful to pass through arguments from `npm run` command.
  119. ```json
  120. {
  121. "scripts": {
  122. "start": "npm-run-all build \"start-server -- --port {1}\" --"
  123. }
  124. }
  125. ```
  126. ```
  127. $ npm run start 8080
  128. > example@0.0.0 start /path/to/package.json
  129. > npm-run-all build "start-server -- --port {1}" -- "8080"
  130. ```
  131. There are the following placeholders:
  132. - `{1}`, `{2}`, ... -- An argument. `{1}` is the 1st argument. `{2}` is the 2nd.
  133. - `{@}` -- All arguments.
  134. - `{*}` -- All arguments as combined.
  135. - `{%}` -- Repeats the command for every argument. (There's no equivalent shell parameter and does not support suffixes)
  136. Support for following suffixes:
  137. - `{1-=foo}` -- defaults to `'foo'` here when the 1st argument is missing
  138. - `{1:=foo}` -- defaults to `'foo'` here and in all following `{1}` when the 1st argument is missing
  139. Those are similar to [Shell Parameters](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameters). But please note arguments are enclosed by double quotes automatically (similar to npm).
  140. ### Known Limitations
  141. - If `--print-label` option is given, some tools in scripts might stop coloring their output.
  142. Because some coloring library (e.g. [chalk]) will stop coloring if `process.stdout` is not a TTY.
  143. `npm-run-all` changes the `process.stdout` of child processes to a pipe in order to add labels to the head of each line if `--print-label` option is given.<br>
  144. For example, [eslint] stops coloring under `npm-run-all --print-label`. But [eslint] has `--color` option to force coloring, we can use it. For anything [chalk] based you can set the environment variable `FORCE_COLOR=1` to produce colored output anyway.
  145. [glob]: https://www.npmjs.com/package/glob#glob-primer
  146. [chalk]: https://www.npmjs.com/package/chalk
  147. [eslint]: https://www.npmjs.com/package/eslint