Skip to content

Commit e07e782

Browse files
fatso83mysticatea
authored andcommitted
New: add --aggregate-output option (fixes #36)(#104)
1 parent dff6409 commit e07e782

File tree

10 files changed

+129
-2
lines changed

10 files changed

+129
-2
lines changed

bin/common/parse-cli-args.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
174174
addGroup(set.groups)
175175
break
176176

177+
case "--aggregate-output":
178+
set.aggregateOutput = true
179+
break
180+
177181
case "-p":
178182
case "--parallel":
179183
if (set.singleMode) {

bin/run-p/help.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Options:
3333
threw error(s).
3434
--max-parallel <number> - Set the maximum number of parallelism. Default is
3535
unlimited.
36+
--aggregate-output - Avoid interleaving output by delaying printing of
37+
each command's output until it has finished.
3638
--npm-path <string> - - - Set the path to npm. Default is the value of
3739
environment variable npm_execpath.
3840
If the variable is not defined, then it's "npm."

bin/run-p/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
5252
arguments: argv.rest,
5353
race: argv.race,
5454
npmPath: argv.npmPath,
55+
aggregateOutput: argv.aggregateOutput,
5556
}
5657
)
5758

docs/run-p.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Options:
3737
threw error(s).
3838
--max-parallel <number> - Set the maximum number of parallelism. Default is
3939
unlimited.
40+
--aggregate-output - Avoid interleaving output by delaying printing of
41+
each command's output until it has finished.
4042
--npm-path <string> - - - Set the path to npm. Default is the value of
4143
environment variable npm_execpath.
4244
If the variable is not defined, then it's "npm."

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
223223
const printName = Boolean(options && options.printName)
224224
const race = Boolean(options && options.race)
225225
const maxParallel = parallel ? ((options && options.maxParallel) || 0) : 1
226+
const aggregateOutput = Boolean(options && options.aggregateOutput)
226227
const npmPath = options && options.npmPath
227228
try {
228229
const patterns = parsePatterns(patternOrPatterns, args)
@@ -273,6 +274,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
273274
race,
274275
maxParallel,
275276
npmPath,
277+
aggregateOutput,
276278
})
277279
})
278280
}

lib/run-tasks.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Requirements
1111
//------------------------------------------------------------------------------
1212

13+
const streams = require("memory-streams")
1314
const NpmRunAllError = require("./npm-run-all-error")
1415
const runTask = require("./run-task")
1516

@@ -104,8 +105,17 @@ module.exports = function runTasks(tasks, options) {
104105
}
105106
return
106107
}
108+
109+
const originalOutputStream = options.stdout
110+
const optionsClone = Object.assign({}, options)
111+
const writer = new streams.WritableStream()
112+
113+
if (options.aggregateOutput) {
114+
optionsClone.stdout = writer
115+
}
116+
107117
const task = queue.shift()
108-
const promise = runTask(task.name, options)
118+
const promise = runTask(task.name, optionsClone)
109119

110120
promises.push(promise)
111121
promise.then(
@@ -115,6 +125,10 @@ module.exports = function runTasks(tasks, options) {
115125
return
116126
}
117127

128+
if (options.aggregateOutput) {
129+
originalOutputStream.write(writer.toString())
130+
}
131+
118132
// Save the result.
119133
results[task.index].code = result.code
120134

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"chalk": "^1.1.3",
3333
"cross-spawn": "^5.0.1",
34+
"memory-streams": "^0.1.2",
3435
"minimatch": "^3.0.2",
3536
"ps-tree": "^1.0.1",
3637
"read-pkg": "^2.0.0",

test-workspace/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"test-task:dump": "node tasks/dump.js",
3737
"test-task:nest-append:npm-run-all": "node ../bin/npm-run-all/index.js test-task:append",
3838
"test-task:nest-append:run-s": "node ../bin/run-s/index.js test-task:append",
39-
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append"
39+
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append",
40+
"test-task:delayed": "node tasks/output-with-delay.js"
4041
},
4142
"repository": {
4243
"type": "git",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict"
2+
3+
const text = process.argv[2]
4+
const timeout = process.argv[3]
5+
6+
process.stdout.write(`[${text}]`)
7+
setTimeout(() => process.stdout.write(`__[${text}]\n`), timeout)

test/aggregate-output.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
/**
3+
* @author Toru Nagashima
4+
* @copyright 2016 Toru Nagashima. All rights reserved.
5+
* See LICENSE file in root directory for full license.
6+
*/
7+
"use strict"
8+
9+
//------------------------------------------------------------------------------
10+
// Requirements
11+
//------------------------------------------------------------------------------
12+
13+
const assert = require("power-assert")
14+
const nodeApi = require("../lib")
15+
const BufferStream = require("./lib/buffer-stream")
16+
const util = require("./lib/util")
17+
const runAll = util.runAll
18+
const runPar = util.runPar
19+
const runSeq = util.runSeq
20+
21+
//------------------------------------------------------------------------------
22+
// Test
23+
//------------------------------------------------------------------------------
24+
25+
describe("[aggregated-output] npm-run-all", () => {
26+
before(() => process.chdir("test-workspace"))
27+
after(() => process.chdir(".."))
28+
29+
/**
30+
* create expected text
31+
* @param {string} term the term to use when creating a line
32+
* @returns {string} the complete line
33+
*/
34+
function createExpectedOutput(term) {
35+
return `[${term}]__[${term}]`
36+
}
37+
38+
describe("should not intermingle output of various commands", () => {
39+
const EXPECTED_SERIALIZED_TEXT = [
40+
createExpectedOutput("first"),
41+
createExpectedOutput("second"),
42+
`${createExpectedOutput("third")}\n`,
43+
].join("\n")
44+
45+
const EXPECTED_PARALLELIZED_TEXT = [
46+
createExpectedOutput("second"),
47+
createExpectedOutput("third"),
48+
`${createExpectedOutput("first")}\n`,
49+
].join("\n")
50+
51+
let stdout = null
52+
53+
beforeEach(() => {
54+
stdout = new BufferStream()
55+
})
56+
57+
it("Node API", () => nodeApi(
58+
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200"],
59+
{stdout, silent: true, aggregateOutput: true}
60+
)
61+
.then(() => {
62+
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
63+
}))
64+
65+
it("npm-run-all command", () => runAll(
66+
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200", "--silent", "--aggregate-output"],
67+
stdout
68+
)
69+
.then(() => {
70+
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
71+
}))
72+
73+
it("run-s command", () => runSeq(
74+
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200", "--silent", "--aggregate-output"],
75+
stdout
76+
)
77+
.then(() => {
78+
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
79+
}))
80+
81+
it("run-p command", () => runPar([
82+
"test-task:delayed first 3000",
83+
"test-task:delayed second 1000",
84+
"test-task:delayed third 2000",
85+
"--silent", "--aggregate-output"],
86+
stdout
87+
)
88+
.then(() => {
89+
assert.equal(stdout.value, EXPECTED_PARALLELIZED_TEXT)
90+
}))
91+
})
92+
})
93+

0 commit comments

Comments
 (0)