Skip to content

Commit

Permalink
New: add --aggregate-output option (fixes #36)(#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 authored and mysticatea committed Aug 26, 2017
1 parent dff6409 commit e07e782
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bin/common/parse-cli-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
addGroup(set.groups)
break

case "--aggregate-output":
set.aggregateOutput = true
break

case "-p":
case "--parallel":
if (set.singleMode) {
Expand Down
2 changes: 2 additions & 0 deletions bin/run-p/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Options:
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--aggregate-output - Avoid interleaving output by delaying printing of
each command's output until it has finished.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable npm_execpath.
If the variable is not defined, then it's "npm."
Expand Down
1 change: 1 addition & 0 deletions bin/run-p/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
arguments: argv.rest,
race: argv.race,
npmPath: argv.npmPath,
aggregateOutput: argv.aggregateOutput,
}
)

Expand Down
2 changes: 2 additions & 0 deletions docs/run-p.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Options:
threw error(s).
--max-parallel <number> - Set the maximum number of parallelism. Default is
unlimited.
--aggregate-output - Avoid interleaving output by delaying printing of
each command's output until it has finished.
--npm-path <string> - - - Set the path to npm. Default is the value of
environment variable npm_execpath.
If the variable is not defined, then it's "npm."
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
const printName = Boolean(options && options.printName)
const race = Boolean(options && options.race)
const maxParallel = parallel ? ((options && options.maxParallel) || 0) : 1
const aggregateOutput = Boolean(options && options.aggregateOutput)
const npmPath = options && options.npmPath
try {
const patterns = parsePatterns(patternOrPatterns, args)
Expand Down Expand Up @@ -273,6 +274,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) {
race,
maxParallel,
npmPath,
aggregateOutput,
})
})
}
Expand Down
16 changes: 15 additions & 1 deletion lib/run-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Requirements
//------------------------------------------------------------------------------

const streams = require("memory-streams")
const NpmRunAllError = require("./npm-run-all-error")
const runTask = require("./run-task")

Expand Down Expand Up @@ -104,8 +105,17 @@ module.exports = function runTasks(tasks, options) {
}
return
}

const originalOutputStream = options.stdout
const optionsClone = Object.assign({}, options)
const writer = new streams.WritableStream()

if (options.aggregateOutput) {
optionsClone.stdout = writer
}

const task = queue.shift()
const promise = runTask(task.name, options)
const promise = runTask(task.name, optionsClone)

promises.push(promise)
promise.then(
Expand All @@ -115,6 +125,10 @@ module.exports = function runTasks(tasks, options) {
return
}

if (options.aggregateOutput) {
originalOutputStream.write(writer.toString())
}

// Save the result.
results[task.index].code = result.code

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"chalk": "^1.1.3",
"cross-spawn": "^5.0.1",
"memory-streams": "^0.1.2",
"minimatch": "^3.0.2",
"ps-tree": "^1.0.1",
"read-pkg": "^2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"test-task:dump": "node tasks/dump.js",
"test-task:nest-append:npm-run-all": "node ../bin/npm-run-all/index.js test-task:append",
"test-task:nest-append:run-s": "node ../bin/run-s/index.js test-task:append",
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append"
"test-task:nest-append:run-p": "node ../bin/run-p/index.js test-task:append",
"test-task:delayed": "node tasks/output-with-delay.js"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions test-workspace/tasks/output-with-delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict"

const text = process.argv[2]
const timeout = process.argv[3]

process.stdout.write(`[${text}]`)
setTimeout(() => process.stdout.write(`__[${text}]\n`), timeout)
93 changes: 93 additions & 0 deletions test/aggregate-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const assert = require("power-assert")
const nodeApi = require("../lib")
const BufferStream = require("./lib/buffer-stream")
const util = require("./lib/util")
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq

//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------

describe("[aggregated-output] npm-run-all", () => {
before(() => process.chdir("test-workspace"))
after(() => process.chdir(".."))

/**
* create expected text
* @param {string} term the term to use when creating a line
* @returns {string} the complete line
*/
function createExpectedOutput(term) {
return `[${term}]__[${term}]`
}

describe("should not intermingle output of various commands", () => {
const EXPECTED_SERIALIZED_TEXT = [
createExpectedOutput("first"),
createExpectedOutput("second"),
`${createExpectedOutput("third")}\n`,
].join("\n")

const EXPECTED_PARALLELIZED_TEXT = [
createExpectedOutput("second"),
createExpectedOutput("third"),
`${createExpectedOutput("first")}\n`,
].join("\n")

let stdout = null

beforeEach(() => {
stdout = new BufferStream()
})

it("Node API", () => nodeApi(
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200"],
{stdout, silent: true, aggregateOutput: true}
)
.then(() => {
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
}))

it("npm-run-all command", () => runAll(
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200", "--silent", "--aggregate-output"],
stdout
)
.then(() => {
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
}))

it("run-s command", () => runSeq(
["test-task:delayed first 300", "test-task:delayed second 100", "test-task:delayed third 200", "--silent", "--aggregate-output"],
stdout
)
.then(() => {
assert.equal(stdout.value, EXPECTED_SERIALIZED_TEXT)
}))

it("run-p command", () => runPar([
"test-task:delayed first 3000",
"test-task:delayed second 1000",
"test-task:delayed third 2000",
"--silent", "--aggregate-output"],
stdout
)
.then(() => {
assert.equal(stdout.value, EXPECTED_PARALLELIZED_TEXT)
}))
})
})

0 comments on commit e07e782

Please sign in to comment.