主要内容
-
&&
: 串行,用&&
符号把多条 npm script 按先后顺序 -
&
: 并行,连接多条命令的&&
符号替换成&
-
& wait
:等待某条异步命令执行结果 -
npm-run-all
的使用
代码检查库
- eslint 【js 代码检查】
- stylelint 【样式文件检查】
- jsonlint 【json 文件语法检查】
- markdownlint-cli 【Markdown 文件最佳实践检查】
单元测试
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"