npm-run-all

330 阅读1分钟

Before: npm run clean && npm run build:css && npm run build:js
After: npm-run-all clean build:*

并行处理:npm-run-all —parallel 的简略是 run-p

序列化处理:npm-run-all —sequential 的简略是 run-s

const runAll = require("npm-run-all");

runAll(["clean", "lint", "build:*"], {parallel: false}) //不并行处理
    .then(() => {
        console.log("done!");
    })
    .catch(err => {
        console.log("failed!");
    });

runAll(["build:* -- --watch"], {parallel: true})    //并行处理
    .then(() => {
        console.log("done!");
    })
    .catch(err => {
        console.log("failed!");
    });


let promise = runAll(patterns, options);    // 返回promise
-   **options.aggregateOutput** `boolean` -- The flag to avoid interleaving output by delaying printing of each command's output until it has finished. This option is valid only with `options.parallel` option. Default is `false`.
-   **options.arguments** `string[]` -- An argument list to replace argument placeholders (such as `{1}`, `{2}`). If pattern text has `{1}`, it's replaced by `options.arguments[0]`. Default is an empty array.
-   **options.continueOnError** `boolean` -- The flag to continue executing other/subsequent scripts even if a script threw an error. Returned `Promise` object will be rejected if one or more scripts threw error(s). Default is `false`.
-   **options.parallel** `boolean` -- The flag to run scripts in parallel. Default is `false`.
-   **options.maxParallel** `number` -- The maximum number of parallelism. This option is valid only with `options.parallel` option. Default is `Number.POSITIVE_INFINITY`.
-   **options.npmPath** `string` -- The path to npm. Default is `process.env.npm_execpath` or `"npm"`.
-   **options.packageConfig** `object|null` -- The map-like object to overwrite package configs. Keys are package names. Every value is a map-like object (Pairs of variable name and value). e.g`{"npm-run-all": {"test": 777, "test2": 333}}` Default is `null`.
-   **options.printLabel** `boolean` -- Set the flag to print the task name as a prefix on each line of output. Tools in scripts may stop coloring their output if this option is given. Default is `false`.
-   **options.printName** `boolean` -- Set the flag to print the task name before running each task. Default is `false`.
-   **options.race** `boolean` -- Set the flag to kill all npm-scripts when a npm-script finished with zero. This option is valid only with `options.parallel` option. Default is `false`.
-   **options.silent** `boolean` -- The flag to set `silent` to the log level of npm. Default is `false`.
-   **options.stdin** `stream.Readable|null` -- The readable stream to send to the stdin of npm-scripts. Default is nothing. Set `process.stdin` in order to send from stdin.
-   **options.stdout** `stream.Writable|null` -- The writable stream to receive from the stdout of npm-scripts. Default is nothing. Set `process.stdout` in order to print to stdout.
-   **options.stderr** `stream.Writable|null` -- The writable stream to receive from the stderr of npm-scripts Default is nothing. Set `process.stderr` in order to print to stderr.
-   options.taskList: `string[] | null` -- 执行script的数组. If this is `null`, it reads from `package.json` in the current directory. Default is `null`.

https://www.npmjs.com/package/npm-run-all