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

153 lines
5.1 KiB

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