Skip to content

Commit 0b04869

Browse files
committed
New feature: mixied running.
1 parent c59e08e commit 0b04869

File tree

7 files changed

+121
-48
lines changed

7 files changed

+121
-48
lines changed

README.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ npm install npm-run-all
1616
## Usage
1717

1818
```
19-
Usage: npm-run-all [OPTIONS] <task> [...tasks]
19+
Usage: npm-run-all [OPTIONS] [...tasks]
2020
2121
Run specified tasks.
2222
2323
Options:
24-
-h, --help Print this text.
25-
-p, --parallel Run specified tasks on parallel.
26-
By default, run on sequential.
27-
-v, --version Print version number.
24+
-h, --help Print this text.
25+
-p, --parallel [...tasks] Grouping tasks to run on parallel.
26+
-s, --sequential [...tasks] Grouping tasks to run on sequential.
27+
-v, --version Print version number.
2828
```
2929

3030
### Run tasks on sequential
@@ -45,6 +45,24 @@ This is same as `npm run watch:html & npm run watch:js`.
4545

4646
Of course, this can be run on **Windows** as well!
4747

48+
### Run tasks on mixed sequential and parallel.
49+
50+
```
51+
npm-run-all clean lint --parallel watch:html watch:js
52+
```
53+
54+
1. First, this runs `clean` and `lint` sequentially.
55+
2. Next, runs `build:html` and `build:js` parallelly.
56+
57+
```
58+
npm-run-all a b --parallel c d --sequential e f --parallel g h i
59+
```
60+
61+
1. First, runs `a` and `b` sequentially.
62+
2. Second, runs `c` and `d` parallelly.
63+
3. Third, runs `e` and `f` sequentially.
64+
4. Lastly, runs `g`, `h`, and `i` parallelly.
65+
4866

4967
## Node API
5068

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"testing:build": "npm run build:babel -- --watch --source-maps-inline",
2424
"testing:mocha": "npm run test:mocha -- --watch --growl",
2525
"test-task:env-check": "node test/tasks/env-check.js",
26-
"test-task:append-a": "node test/tasks/append.js a",
27-
"test-task:append-b": "node test/tasks/append.js b",
26+
"test-task:append": "node test/tasks/append.js",
2827
"test-task:error": "node test/tasks/error.js",
2928
"test-task:stdio": "node test/tasks/stdio.js"
3029
},

src/command.js

+63-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/usr/bin/env node
22

3-
import {readFileSync} from "fs";
4-
import {join as joinPath} from "path";
5-
import minimist from "minimist";
63
import runAll from "./index";
74

85
if (require.main === module) {
@@ -16,56 +13,86 @@ Usage: npm-run-all [OPTIONS] [...tasks]
1613
Run specified tasks.
1714
1815
Options:
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.
16+
-h, --help Print this text.
17+
-p, --parallel [...tasks] Grouping tasks to run on parallel.
18+
-s, --sequential [...tasks] Grouping tasks to run on sequential.
19+
-v, --version Print version number.
2320
2421
See Also:
2522
https://github.com/mysticatea/npm-run-all
2623
`);
2724
}
2825

2926
function printVersion() {
30-
const version = JSON.parse(
31-
readFileSync(
32-
joinPath(__dirname, "../package.json"),
33-
{encoding: "utf8"}
34-
)
35-
).version;
27+
console.log("v" + require("../package.json").version);
28+
}
29+
30+
function createQueue(args) {
31+
return args.reduce((queue, arg) => {
32+
switch (arg) {
33+
case "-s":
34+
case "--sequential":
35+
queue.push({parallel: false, tasks: []});
36+
break;
37+
38+
case "-p":
39+
case "--parallel":
40+
queue.push({parallel: true, tasks: []});
41+
break;
3642

37-
console.log("v" + version);
43+
default:
44+
if (arg[0] === "-") {
45+
throw new Error("Invalid Option: " + arg);
46+
}
47+
queue[queue.length - 1].tasks.push(arg);
48+
break;
49+
}
50+
return queue;
51+
}, [{parallel: false, tasks: []}]);
3852
}
3953

4054
/*eslint no-process-exit:0*/
4155
function main(args) {
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-
});
56+
if (args.length === 0) {
57+
args.push("--help");
58+
}
59+
switch (args[0]) {
60+
case "-h":
61+
case "--help":
62+
printHelp();
63+
return;
64+
65+
case "-v":
66+
case "--version":
67+
printVersion();
68+
return;
69+
}
5270

53-
if (options._.length === 0 || options.help) {
54-
printHelp();
55-
process.exit(0);
71+
let queue;
72+
try {
73+
queue = createQueue(args);
5674
}
57-
if (options.version) {
58-
printVersion();
59-
process.exit(0);
75+
catch (err) {
76+
console.error(err.message);
77+
process.exit(1);
6078
}
6179

62-
runAll(
63-
options._,
80+
(function next() {
81+
const group = queue.shift();
82+
if (group == null) {
83+
return;
84+
}
85+
if (group.tasks.length === 0) {
86+
next();
87+
return;
88+
}
89+
runAll(
90+
group.tasks,
6491
{
6592
stdout: process.stdout,
6693
stderr: process.stderr,
67-
parallel: options.parallel
68-
}
69-
)
70-
.catch(() => process.exit(1));
94+
parallel: group.parallel
95+
})
96+
.then(next, () => process.exit(1));
97+
})();
7198
}

test/mixed.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {execSync} from "child_process";
2+
import assert from "power-assert";
3+
import {result, removeResult} from "./lib/util";
4+
5+
// Test targets.
6+
import runAll from "../lib/index";
7+
import "../lib/command";
8+
9+
describe("npm-run-all", () => {
10+
beforeEach(removeResult);
11+
after(removeResult);
12+
13+
it("should run tasks, mixed sequential and parallel 1:", () => {
14+
execSync("node lib/command.js \"test-task:append a\" -p \"test-task:append b\" \"test-task:append c\" -s \"test-task:append d\" \"test-task:append e\"");
15+
assert(result() === "aabcbcddee" ||
16+
result() === "aabccbddee" ||
17+
result() === "aacbbcddee" ||
18+
result() === "aacbcbddee");
19+
});
20+
21+
it("should run tasks, mixed sequential and parallel 2:", () => {
22+
execSync("node lib/command.js -p \"test-task:append b\" \"test-task:append c\" -s \"test-task:append d\" \"test-task:append e\"");
23+
assert(result() === "bcbcddee" ||
24+
result() === "bccbddee" ||
25+
result() === "cbbcddee" ||
26+
result() === "cbcbddee");
27+
});
28+
29+
});

test/parallel.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("npm-run-all", () => {
1212

1313
describe("should run tasks on parallel when was given --parallel option:", () => {
1414
it("lib version", () => {
15-
return runAll(["test-task:append-a", "test-task:append-b"], {parallel: true})
15+
return runAll(["test-task:append a", "test-task:append b"], {parallel: true})
1616
.then(() => {
1717
assert(result() === "abab" ||
1818
result() === "baba" ||
@@ -22,7 +22,7 @@ describe("npm-run-all", () => {
2222
});
2323

2424
it("command version", () => {
25-
execSync("node lib/command.js --parallel test-task:append-a test-task:append-b");
25+
execSync("node lib/command.js --parallel \"test-task:append a\" \"test-task:append b\"");
2626
assert(result() === "abab" ||
2727
result() === "baba" ||
2828
result() === "abba" ||

test/sequential.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ describe("npm-run-all", () => {
1212

1313
describe("should run tasks on sequential:", () => {
1414
it("lib version", () => {
15-
return runAll(["test-task:append-a", "test-task:append-b"], {parallel: false})
15+
return runAll(["test-task:append a", "test-task:append b"], {parallel: false})
1616
.then(() => {
1717
assert(result() === "aabb");
1818
});
1919
});
2020

2121
it("command version", () => {
22-
execSync("node lib/command.js test-task:append-a test-task:append-b");
22+
execSync("node lib/command.js \"test-task:append a\" \"test-task:append b\"");
2323
assert(result() === "aabb");
2424
});
2525
});

test/tasks/append.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ function append() {
55
}
66

77
append();
8-
setTimeout(append, 500);
8+
setTimeout(append, 100);

0 commit comments

Comments
 (0)