Skip to content

Latest commit

 

History

History
121 lines (90 loc) · 4.54 KB

README.md

File metadata and controls

121 lines (90 loc) · 4.54 KB

npm-run-batch

npm license travis status Build status David David
NPM

npm run-script helper that allows running multiple run-scripts in series & parallel

npm-run-batch allows npm to be used as a build tool with a minimum of fuss.

It's not uncommon to see npm run-scripts that look like this:

"prebuild": "npm run build:clean && npm run test",
"build": "cross-env NODE_ENV=production webpack --config internals/webpack/webpack.prod.babel.js --color -p",
"build:clean": "npm run test:clean && rimraf ./build",
"build:dll": "node ./internals/scripts/dependencies.js",

As project complexity grows, these become harder to comprehend and debug.

To tackle this (and other things, but I never claim impartiality!), multiple build/automation tools have been craated gulp, grunt, brunch and that is not even the whole list.

However, this is not without debate.

A persistent source of complexity with using npm as a build tool are the pesky && to chain commands together. Further, one cannot run multiple commands in parallel. Here's a good example why that can be necessary.

npm-run-batch attempts to solve the problem of composing complex automation flows for npm-as-a-build-tool. It provides simple semantics, aids clarity and requires almost no extra installed weight.

Prove it!

"scripts": {
  "rimraf": "rimraf ./build",
  "webpack": "webpack --config internals/webpack/webpack.prod.bable.js --color -p",
  "build:dll": "node ./internals/scripts/dependencies.js",
  "test": "mocha",

  "build:clean": "npm-run-batch",
  "build": "cross-env NODE_ENV=production npm-run-batch"
},
"run-batch": {
  "build:clean": [
    "test:clean",
    "rimraf"
  ],
  "build": [
    "build:clean",
    "test",
    "webpack"
  ]
}

Installation

npm install npm-run-batch

Usage

The module exposes two binary aliases -

npm-run-batch
run-batch

These are meant to be invoked from npm-run scripts and used for the grouping of batch sequences as we shall see next.

In your package.json, define tasks as usual, but they do only one thing. The batch operations are the ones that are the meat of your run-script.

"scripts": {
  "task:1": "a simple task",
  "task:2": "a simple task",
  "task:3": "a simple task",
  "task:4": "a simple task",
  "batch:1": "run-batch",
  "batch:2": "run-batch"
}

Once the batch tasks have been tagged (either of the aliases will work),

  1. Add a "run-batch" segment to your package.json

  2. For each batch run-script, add a key to the "run-batch" section.

  3. Each "run-batch" segment is a list of run-scripts that you want to run.

  4. At each stage, run-scripts can be run in parallel if you add an array as shown for "batch:2" below.

    "run-batch": { "batch:1": [ "task:1", "task:2" ], "batch:2": [ "task:1", [ "task:2", "task:3" ], // Parallel tasks "task:4" ] }

Importantly, we allow for both aliases to be used interchangeably. If both aliases are present in package.json for batching, "run-batch" takes precedence over "npm-run-batch".

That's all there is to it!

Working Example

Please see package.json for a few working examples. Because we can't install the package within itself, instead of the alias run-batch, we use node ./index.js. With that caveat, this should work as described.

  • notice the use of cross-env to pass the environment forward.
  • for true cross-platform independence, we highly recommend using shelljs

Feedback of all kinds is welcome.

License

Apache-2.0