Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Mar 24, 2015
1 parent b77f1a5 commit 9555a9d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

A CLI tool to run multiple npm-scripts on sequential or parallel.


## Installation

```
npm install npm-run-all
```


## Usage

```
Usage: npm-run-all [OPTIONS] <task> [...tasks]
Run specified tasks on sequential.
Options:
-h, --help Print this text.
-p, --parallel <task> [...tasks] Run specified tasks on parallel.
-v, --version Print version number.
Run specified tasks.
Options:
-h, --help Print this text.
-p, --parallel Run specified tasks on parallel.
By default, run on sequential.
-v, --version Print version number.
```


## Examples

```json
Expand All @@ -30,7 +34,7 @@ Options:
"build:html": "cp src/client/*.{html,css} app/static/",
"build:js": "browserify src/client/index.js -o app/static/index.js",
"build:babel": "babel src/server -o app/server",

"start": "npm run build && node app/server/index.js",
"test": "mocha test --compilers js:babel/register",

Expand Down Expand Up @@ -58,3 +62,35 @@ npm-run-all --parallel testing:html testing:js testing:babel testing:server test

is same as `npm run testing:html & npm run testing:js & npm run testing:babel & npm run testing:server & npm run testing:mocha`.
Of course, be possible to run on Windows as well!


## Node API

```
var runAll = require("npm-run-all");
```

### runAll

```
var promise = runAll(tasks, options);
```

Run npm-scripts.

* *tasks* `string|string[]` -- Task names.
* *options* `object`
* *options.parallel* `boolean` -- A flag to run tasks on parallel. By default,
`false`.
* *options.stdin* `stream.Readable` -- A readable stream that sends to stdin
of tasks. By default, nothing. Set `process.stdin` in order to send from
key inputs.
* *options.stdout* `stream.Writable` -- A writable stream that receives stdout
of tasks. By default, nothing. Set `process.stdout` in order to print to
console.
* *options.stderr* `stream.Writable` -- A writable stream that receives stderr
of tasks. By default, nothing. Set `process.stderr` in order to print to
console.

`runAll` returns a promise that becomes *fulfilled* when done all tasks.
The promise will become *rejected* when any of tasks exited with non-zero code.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"testing": "run-all \"npm run testing:babel\" \"npm run testing:mocha\"",
"testing:babel": "babel src --out-dir lib --watch --source-maps-inline",
"testing:mocha": "mocha test/*.js --compilers js:espower-babel/guess --timeout 10000 --watch --colors",

"test-task:env-check": "node test/tasks/env-check.js",
"test-task:append-a": "node test/tasks/append.js a",
"test-task:append-b": "node test/tasks/append.js b",
Expand Down Expand Up @@ -54,5 +53,8 @@
"mocha": "^2.2.1",
"power-assert": "^0.10.2",
"run-all": "^1.0.1"
},
"dependencies": {
"minimist": "^1.1.1"
}
}
69 changes: 32 additions & 37 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {readFileSync} from "fs";
import {join as joinPath} from "path";
import minimist from "minimist";
import runAll from "./index";

if (require.main === module) {
Expand All @@ -10,14 +11,15 @@ if (require.main === module) {

function printHelp() {
console.log(`
Usage: npm-run-all [OPTIONS] <task> [...tasks]
Usage: npm-run-all [OPTIONS] [...tasks]
Run specified tasks on sequential.
Run specified tasks.
Options:
-h, --help Print this text.
-p, --parallel <task> [...tasks] Run specified tasks on parallel.
-v, --version Print version number.
-h, --help Print this text.
-p, --parallel Run specified tasks on parallel.
By default, run on sequential.
-v, --version Print version number.
See Also:
https://github.com/mysticatea/npm-run-all
Expand All @@ -35,42 +37,35 @@ function printVersion() {
console.log("v" + version);
}

function findParallelOptionIndex(args) {
for (let i = 0, end = args.length; i < end; ++i) {
const arg = args[i];
if (arg === "-p" || arg === "--parallel") {
return i;
}
}
return -1;
}

/*eslint no-process-exit:0*/
function main(args) {
switch (args[0]) {
case undefined:
case "-h":
case "--help":
printHelp();
process.exit(0);
break;
const options = minimist(args, {
boolean: ["help", "parallel", "version"],
alias: {"h": "help", "p": "parallel", "v": "version"},
unknown: arg => {
if (arg[0] === "-") {
console.error(`Unknown Option: ${arg}`);
process.exit(1);
}
}
});

case "-v":
case "--version":
printVersion();
process.exit(0);
break;
if (options._.length === 0 || options.help) {
printHelp();
process.exit(0);
}
if (options.version) {
printVersion();
process.exit(0);
}

const pIndex = findParallelOptionIndex(args);
const seqTasks = (pIndex < 0 ? args : args.slice(0, pIndex));
const parTasks = (pIndex < 0 ? [] : args.slice(1 + pIndex));
const seqOptions =
{stdout: process.stdout, stderr: process.stderr, parallel: false};
const parOptions =
{stdout: process.stdout, stderr: process.stderr, parallel: true};

runAll(seqTasks, seqOptions)
.then(() => runAll(parTasks, parOptions))
runAll(
options._,
{
stdout: process.stdout,
stderr: process.stderr,
parallel: options.parallel
}
)
.catch(() => process.exit(1));
}

0 comments on commit 9555a9d

Please sign in to comment.