Skip to content

Commit 9555a9d

Browse files
committed
Refactor
1 parent b77f1a5 commit 9555a9d

File tree

3 files changed

+78
-45
lines changed

3 files changed

+78
-45
lines changed

README.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

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

5+
56
## Installation
67

78
```
89
npm install npm-run-all
910
```
1011

12+
1113
## Usage
1214

1315
```
1416
Usage: npm-run-all [OPTIONS] <task> [...tasks]
1517
16-
Run specified tasks on sequential.
17-
18-
Options:
19-
-h, --help Print this text.
20-
-p, --parallel <task> [...tasks] Run specified tasks on parallel.
21-
-v, --version Print version number.
18+
Run specified tasks.
19+
20+
Options:
21+
-h, --help Print this text.
22+
-p, --parallel Run specified tasks on parallel.
23+
By default, run on sequential.
24+
-v, --version Print version number.
2225
```
2326

27+
2428
## Examples
2529

2630
```json
@@ -30,7 +34,7 @@ Options:
3034
"build:html": "cp src/client/*.{html,css} app/static/",
3135
"build:js": "browserify src/client/index.js -o app/static/index.js",
3236
"build:babel": "babel src/server -o app/server",
33-
37+
3438
"start": "npm run build && node app/server/index.js",
3539
"test": "mocha test --compilers js:babel/register",
3640

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

5963
is same as `npm run testing:html & npm run testing:js & npm run testing:babel & npm run testing:server & npm run testing:mocha`.
6064
Of course, be possible to run on Windows as well!
65+
66+
67+
## Node API
68+
69+
```
70+
var runAll = require("npm-run-all");
71+
```
72+
73+
### runAll
74+
75+
```
76+
var promise = runAll(tasks, options);
77+
```
78+
79+
Run npm-scripts.
80+
81+
* *tasks* `string|string[]` -- Task names.
82+
* *options* `object`
83+
* *options.parallel* `boolean` -- A flag to run tasks on parallel. By default,
84+
`false`.
85+
* *options.stdin* `stream.Readable` -- A readable stream that sends to stdin
86+
of tasks. By default, nothing. Set `process.stdin` in order to send from
87+
key inputs.
88+
* *options.stdout* `stream.Writable` -- A writable stream that receives stdout
89+
of tasks. By default, nothing. Set `process.stdout` in order to print to
90+
console.
91+
* *options.stderr* `stream.Writable` -- A writable stream that receives stderr
92+
of tasks. By default, nothing. Set `process.stderr` in order to print to
93+
console.
94+
95+
`runAll` returns a promise that becomes *fulfilled* when done all tasks.
96+
The promise will become *rejected* when any of tasks exited with non-zero code.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"testing": "run-all \"npm run testing:babel\" \"npm run testing:mocha\"",
2020
"testing:babel": "babel src --out-dir lib --watch --source-maps-inline",
2121
"testing:mocha": "mocha test/*.js --compilers js:espower-babel/guess --timeout 10000 --watch --colors",
22-
2322
"test-task:env-check": "node test/tasks/env-check.js",
2423
"test-task:append-a": "node test/tasks/append.js a",
2524
"test-task:append-b": "node test/tasks/append.js b",
@@ -54,5 +53,8 @@
5453
"mocha": "^2.2.1",
5554
"power-assert": "^0.10.2",
5655
"run-all": "^1.0.1"
56+
},
57+
"dependencies": {
58+
"minimist": "^1.1.1"
5759
}
5860
}

src/command.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {readFileSync} from "fs";
44
import {join as joinPath} from "path";
5+
import minimist from "minimist";
56
import runAll from "./index";
67

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

1112
function printHelp() {
1213
console.log(`
13-
Usage: npm-run-all [OPTIONS] <task> [...tasks]
14+
Usage: npm-run-all [OPTIONS] [...tasks]
1415
15-
Run specified tasks on sequential.
16+
Run specified tasks.
1617
1718
Options:
18-
-h, --help Print this text.
19-
-p, --parallel <task> [...tasks] Run specified tasks on parallel.
20-
-v, --version Print version number.
19+
-h, --help Print this text.
20+
-p, --parallel Run specified tasks on parallel.
21+
By default, run on sequential.
22+
-v, --version Print version number.
2123
2224
See Also:
2325
https://github.com/mysticatea/npm-run-all
@@ -35,42 +37,35 @@ function printVersion() {
3537
console.log("v" + version);
3638
}
3739

38-
function findParallelOptionIndex(args) {
39-
for (let i = 0, end = args.length; i < end; ++i) {
40-
const arg = args[i];
41-
if (arg === "-p" || arg === "--parallel") {
42-
return i;
43-
}
44-
}
45-
return -1;
46-
}
47-
4840
/*eslint no-process-exit:0*/
4941
function main(args) {
50-
switch (args[0]) {
51-
case undefined:
52-
case "-h":
53-
case "--help":
54-
printHelp();
55-
process.exit(0);
56-
break;
42+
const options = minimist(args, {
43+
boolean: ["help", "parallel", "version"],
44+
alias: {"h": "help", "p": "parallel", "v": "version"},
45+
unknown: arg => {
46+
if (arg[0] === "-") {
47+
console.error(`Unknown Option: ${arg}`);
48+
process.exit(1);
49+
}
50+
}
51+
});
5752

58-
case "-v":
59-
case "--version":
60-
printVersion();
61-
process.exit(0);
62-
break;
53+
if (options._.length === 0 || options.help) {
54+
printHelp();
55+
process.exit(0);
56+
}
57+
if (options.version) {
58+
printVersion();
59+
process.exit(0);
6360
}
6461

65-
const pIndex = findParallelOptionIndex(args);
66-
const seqTasks = (pIndex < 0 ? args : args.slice(0, pIndex));
67-
const parTasks = (pIndex < 0 ? [] : args.slice(1 + pIndex));
68-
const seqOptions =
69-
{stdout: process.stdout, stderr: process.stderr, parallel: false};
70-
const parOptions =
71-
{stdout: process.stdout, stderr: process.stderr, parallel: true};
72-
73-
runAll(seqTasks, seqOptions)
74-
.then(() => runAll(parTasks, parOptions))
62+
runAll(
63+
options._,
64+
{
65+
stdout: process.stdout,
66+
stderr: process.stderr,
67+
parallel: options.parallel
68+
}
69+
)
7570
.catch(() => process.exit(1));
7671
}

0 commit comments

Comments
 (0)