npm一次性执行多条命令

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:* 这样精致小巧的模样。再者大家也知道 shell 的 & 语法实际上是不支持 cmd 的,为了跨平台也最好使用这样的第三方库来提供支持。preact 中就是用来对数量繁多的子模块进行并行构建和顺序测试。

安装

# 安装

npm install npm-run-all --save-dev

# 查看帮助信息

npm-run-all -h

参数

  • --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 clean lint build

并行执行

npm-run-all --parallel lint build

混合执行

npm-run-all clean lint --parallel watch:html watch:js

这段命令首先按顺序执行 clean lint 两个脚本,然后同时执行 watch:html 和 watch:js 的任务。

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

这段命令通过以下顺序执行:

  1. 顺序运行 a 和 b;
  2. 然后同时运行 c 和 d;
  3. 再依次运行 e 和 f;
  4. 最后同时执行 g, h, i。

示例

"scripts": {
    "install": "npm-run-all --serial install:*",
    "install:main": "cd main && npm i",
    "install:sub-vue": "cd sub-vue && npm i",
    "install:sub-react": "cd sub-react && npm i",
    "install:sub-html": "cd sub-html && npm i",
    "start": "npm-run-all --parallel start:*",
    "start:sub-react": "cd sub-react && npm start",
    "start:sub-vue": "cd sub-vue && npm start",
    "start:main": "cd main && npm start",
    "start:sub-html": "cd sub-html && npm start",
    "build": "npm-run-all build:* && bash ./scripts/bundle.sh",
    "build:sub-react": "cd sub-react && npm run build",
    "build:sub-vue": "cd sub-vue && npm run build",
    "build:sub-html": "cd sub-html && npm run build",
    "build:main": "cd main && npm run build",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

执行npm start 会并行执行下面的4个start命令

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值