from ruanyifeng

npm script

whatis

in package.json script section
{
    // ...
    "scripts": {
        "build": "node build.js"
    }
}

how to use

npm run
    show all script defined
npm run commandname
    exec a command named build,then use npm run build
    npm run build = npm build ,run is't nessesary

原理

npm 脚本的原理非常简单。每当执行npm run,就会自动新建一个 Shell,在这个 Shell 里面执行指定的脚本命令。因此,只要是 Shell(一般是 Bash)可以运行的命令,就可以写在 npm 脚本里面。

通配符

由于 npm 脚本就是 Shell 脚本,因为可以使用 Shell 通配符。

"lint": "jshint *.js"
"lint": "jshint **/*.js"
上面代码中,*表示任意文件名,**表示任意一层子目录。
如果要将通配符传入原始命令,防止被 Shell 转义,要将星号转义。

"test": "tap test/\*.js"

传参

向 npm 脚本传入参数,要使用--标明。

"lint": "jshint **.js"
向上面的npm run lint命令传入参数,必须写成下面这样。

$ npm run lint --  --reporter checkstyle > checkstyle.xml
也可以在package.json里面再封装一个命令。

"lint": "jshint **.js",
"lint:checkstyle": "npm run lint -- --reporter checkstyle > checkstyle.xml"

执行顺序

如果 npm 脚本里面需要执行多个任务,那么需要明确它们的执行顺序。

如果是并行执行(即同时的平行执行),可以使用&符号。

$ npm run script1.js & npm run script2.js
如果是继发执行(即只有前一个任务成功,才执行下一个任务),可以使用&&符号。

$ npm run script1.js && npm run script2.js
这两个符号是 Bash 的功能。此外,还可以使用 node 的任务管理模块:script-runner、npm-run-all、redrun。

results matching ""

    No results matching ""