0
点赞
收藏
分享

微信扫一扫

npm script工作流(二)串行和并行

主要内容

  • && : 串行,用 && 符号把多条 npm script 按先后顺序
  • & : 并行,连接多条命令的 && 符号替换成 &
  • & wait :等待某条异步命令执行结果
  • npm-run-all 的使用

代码检查库

单元测试

  • mocha 【测试用例组织、测试用例运行和结果收集的框架】
  • chai 结合 sinon 【测试断言库】

package.json配置如下:

{
  "name": "hello-npm-script",
  "version": "0.1.0",
  "main": "index.js",
  "scripts": {
    "lint:js": "eslint *.js",
    "lint:css": "stylelint *.less",
    "lint:json": "jsonlint --quiet *.json",
    "lint:markdown": "markdownlint --config .markdownlint.json *.md",
    "test": "mocha tests/"
  },
  "devDependencies": {
    "chai": "^4.1.2",
    "eslint": "^4.11.0",
    "jsonlint": "^1.6.2",
    "markdownlint-cli": "^0.5.0",
    "mocha": "^4.0.1",
    "stylelint": "^8.2.0",
    "stylelint-config-standard": "^17.0.0"
  }
}

多个script 串行

  • && 符号把多条 npm script 按先后顺序串起来
{
"scripts":{
"test": "npm run lint:js && npm run lint:css && npm run lint:json && npm run lint:markdown && mocha tests/"
}
}
  • 执行:npm test
  • 顺序:eslint ==> stylelint ==> jsonlint ==> markdownlint ==> mocha

多个script 并行

  • 连接多条命令的 && 符号替换成 &
"test": "npm run lint:js & npm run lint:css & npm run lint:json & npm run lint:markdown & mocha tests/"
  • 异步的命令加:& wait
npm run lint:js & npm run lint:css & npm run lint:json & npm run lint:markdown & mocha tests/ & wait

script 管理最佳实践

npm-run-all

  • 安装
npm i npm-run-all -D
  • 串行 (支持通配符匹配)
"test": "npm-run-all lint:* mocha"
  • 并行 (不需要增加& wait,npm-run-all内部已处理)
"test": "npm-run-all --parallel lint:* mocha"
举报

相关推荐

0 条评论