- Npm-run-all ,这个工具是为了解决官方的 npm run 命令无法同时运行多个脚本的问题,它可以把诸如 npm run clean && npm run build:css && npm run build:js && npm run build:html 的一长串的命令通过 glob 语法简化成 npm-run-all clean build:*
- 安装
npm install npm-run-all --save-dev
-
--parallel: 并行运行多个命令,例如:npm-run-all --parallel lint build
--serial: 多个命令按排列顺序执行,例如:npm-run-all --serial clean lint build:**
--continue-on-error: 是否忽略错误,添加此参数 npm-run-all 会自动退出出错的命令,继续运行正常的
--race: 添加此参数之后,只要有一个命令运行出错,那么 npm-run-all 就会结束掉全部的命令
-
这个包提供三个命令,分别是 npm-run-all run-s run-p,其中后两个都是 npm-run-all 带参数的简写,分别对应串行和并行。
npm-run-all a b --parallel c d --sequential e f --parallel g h i
# or
npm-run-all a b --parallel c d --serial e f --parallel g h i
这段命令通过以下顺序执行:
顺序运行 a 和 b;
然后同时运行 c 和 d;
再依次运行 e 和 f;
最后同时执行 g, h, i。
npm-run-all --parallel watch:*
不匹配分隔符,同时运行 watch:html watch:js 但不运行 watch:js:index。
npm-run-all --parallel watch:**
匹配分隔符,所有以 watch: 开头的脚本都会被运行。
cp -r 的作用是递归,可复制目录,如果复制目录必须加此选项。
- Npm 原理:npm run script执行脚本的时候都会创建一个shell,然后在shell中执行指定的脚本。所以在npm中可以执行shell脚本
简单案例:将微服务项目中的dist以及相关的dockerfile和nginx配置一起放到一个文件夹中然后压缩打包生成zip文件(注意:具体的使用需要根据自己的文件存放位置来调整路径, 我的项目执行文件的所在目录和其他项目文件夹同级)
"zip:main": "rm -rf web-main:1.0.0 && mkdir web-main:1.0.0 && cp -r dist nginx.conf Dockerfile web-main:1.0.0 && zip -o -r web-main:1.0.0.zip web-main:1.0.0",
"zip:web-standard": "cd ../web-standard && rm -rf web-standard:1.0.0 && mkdir web-standard:1.0.0 && cp -r dist nginx.conf Dockerfile web-standard:1.0.0 && zip -q -r ../web-main/web-standard:1.0.0.zip web-standard:1.0.0",
"zip:web-collect": "cd ../web-collect && rm -rf web-collect:1.1.0 && mkdir web-collect:1.1.0 && cp -r dist nginx.conf Dockerfile web-collect:1.1.0 && zip -q -r ../web-main/web-collect:1.1.0.zip web-collect:1.1.0",
"zip-all": "npm-run-all --parallel zip:*"
官方文档地址:npm-run-all