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

162 lines
5.7 KiB

  1. | [index](../README.md) | [npm-run-all](npm-run-all.md) | [run-s](run-s.md) | run-p | [Node API](node-api.md) |
  2. |-----------------------|-------------------------------|-------------------|-------|-------------------------|
  3. # `run-p` command
  4. A CLI command to run given npm-scripts in parallel.
  5. This command is the shorthand of `npm-run-all -p`.
  6. ```
  7. Usage:
  8. $ run-p [--help | -h | --version | -v]
  9. $ run-p [OPTIONS] <tasks>
  10. Run given npm-scripts in parallel.
  11. <tasks> : A list of npm-scripts' names and Glob-like patterns.
  12. Options:
  13. --aggregate-output - - - Avoid interleaving output by delaying printing of
  14. each command's output until it has finished.
  15. -c, --continue-on-error - Set the flag to continue executing other tasks
  16. even if a task threw an error. 'run-p' itself
  17. will exit with non-zero code if one or more tasks
  18. threw error(s).
  19. --max-parallel <number> - Set the maximum number of parallelism. Default is
  20. unlimited.
  21. --npm-path <string> - - - Set the path to npm. Default is the value of
  22. environment variable npm_execpath.
  23. If the variable is not defined, then it's "npm."
  24. In this case, the "npm" command must be found in
  25. environment variable PATH.
  26. -l, --print-label - - - - Set the flag to print the task name as a prefix
  27. on each line of output. Tools in tasks may stop
  28. coloring their output if this option was given.
  29. -n, --print-name - - - - Set the flag to print the task name before
  30. running each task.
  31. -r, --race - - - - - - - Set the flag to kill all tasks when a task
  32. finished with zero.
  33. -s, --silent - - - - - - Set 'silent' to the log level of npm.
  34. Shorthand aliases can be combined.
  35. For example, '-clns' equals to '-c -l -n -s'.
  36. Examples:
  37. $ run-p watch:**
  38. $ run-p --print-label "build:** -- --watch"
  39. $ run-p -l "build:** -- --watch"
  40. $ run-p start-server start-browser start-electron
  41. ```
  42. ### npm-scripts
  43. It's `"scripts"` field of `package.json`.
  44. For example:
  45. ```json
  46. {
  47. "scripts": {
  48. "clean": "rm -rf dist",
  49. "lint": "eslint src",
  50. "build": "babel src -o lib"
  51. }
  52. }
  53. ```
  54. We can run a script with `npm run` command.
  55. On the other hand, this `run-p` command runs multiple scripts in parallel.
  56. The following 2 commands are similar.
  57. The `run-p` command is shorter and **available on Windows**.
  58. ```
  59. $ run-p lint build
  60. $ npm run lint & npm run build
  61. ```
  62. **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`).
  63. If `--continue-on-error` option is given, this behavior will be disabled.
  64. **Note2:** `&` operator does not work on Windows' `cmd.exe`. But `run-p` works fine there.
  65. ### Glob-like pattern matching for script names
  66. We can use [glob]-like patterns to specify npm-scripts.
  67. The difference is one -- the separator is `:` instead of `/`.
  68. ```
  69. $ run-p watch:*
  70. ```
  71. In this case, runs sub scripts of `watch`. For example: `watch:html`, `watch:js`.
  72. But, doesn't run sub-sub scripts. For example: `watch:js:index`.
  73. ```
  74. $ run-p watch:**
  75. ```
  76. If we use a globstar `**`, runs both sub scripts and sub-sub scripts.
  77. `run-p` reads the actual npm-script list from `package.json` in the current directory, then filters the scripts by glob-like patterns, then runs those.
  78. ### Run with arguments
  79. We can enclose a script name or a pattern in quotes to use arguments.
  80. The following 2 commands are similar.
  81. ```
  82. $ run-p "build:* -- --watch"
  83. $ npm run build:aaa -- --watch & npm run build:bbb -- --watch
  84. ```
  85. When we use a pattern, arguments are forwarded to every matched script.
  86. ### Argument placeholders
  87. We can use placeholders to give the arguments preceded by `--` to scripts.
  88. ```
  89. $ run-p "start-server -- --port {1}" -- 8080
  90. ```
  91. This is useful to pass through arguments from `npm run` command.
  92. ```json
  93. {
  94. "scripts": {
  95. "start": "run-p \"start-server -- --port {1}\" --"
  96. }
  97. }
  98. ```
  99. ```
  100. $ npm run start 8080
  101. > example@0.0.0 start /path/to/package.json
  102. > run-p "start-server -- --port {1}" -- "8080"
  103. ```
  104. There are the following placeholders:
  105. - `{1}`, `{2}`, ... -- An argument. `{1}` is the 1st argument. `{2}` is the 2nd.
  106. - `{@}` -- All arguments.
  107. - `{*}` -- All arguments as combined.
  108. - `{%}` -- Repeats the command for every argument. (There's no equivalent shell parameter and does not support suffixes)
  109. Support for following suffixes:
  110. - `{1-=foo}` -- defaults to `'foo'` here when the 1st argument is missing
  111. - `{1:=foo}` -- defaults to `'foo'` here and in all following `{1}` when the 1st argument is missing
  112. 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).
  113. ### Known Limitations
  114. - If `--print-label` option is given, some tools in scripts might stop coloring their output.
  115. Because some coloring library (e.g. [chalk]) will stop coloring if `process.stdout` is not a TTY.
  116. `run-p` 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>
  117. For example, [eslint] stops coloring under `run-p --print-label`. But [eslint] has `--color` option to force coloring, we can use it.
  118. [glob]: https://www.npmjs.com/package/glob#glob-primer
  119. [chalk]: https://www.npmjs.com/package/chalk
  120. [eslint]: https://www.npmjs.com/package/eslint