From f2a076e5827da1a1f462a72f9bc24a8e49a9052d Mon Sep 17 00:00:00 2001 From: Jochen Diekenbrock Date: Sat, 6 Jul 2019 16:19:55 +0200 Subject: [PATCH] add option --report-by-task --- bin/common/parse-cli-args.js | 4 ++++ bin/npm-run-all/main.js | 1 + lib/create-task-streams.js | 34 ++++++++++++++++++++++++++++++++++ lib/index.js | 2 ++ lib/run-task.js | 25 ++++++++++++++++++++++--- 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 lib/create-task-streams.js diff --git a/bin/common/parse-cli-args.js b/bin/common/parse-cli-args.js index 7f056fc..19305cf 100644 --- a/bin/common/parse-cli-args.js +++ b/bin/common/parse-cli-args.js @@ -135,6 +135,10 @@ function parseCLIArgsCore(set, args) { // eslint-disable-line complexity set.continueOnError = true break + case "--report-by-task": + set.reportByTask = true + break + case "-l": case "--print-label": set.printLabel = true diff --git a/bin/npm-run-all/main.js b/bin/npm-run-all/main.js index 2782468..0651759 100644 --- a/bin/npm-run-all/main.js +++ b/bin/npm-run-all/main.js @@ -44,6 +44,7 @@ module.exports = function npmRunAll(args, stdout, stderr) { parallel: group.parallel, maxParallel: group.parallel ? argv.maxParallel : 1, continueOnError: argv.continueOnError, + reportByTask: argv.reportByTask, printLabel: argv.printLabel, printName: argv.printName, config: argv.config, diff --git a/lib/create-task-streams.js b/lib/create-task-streams.js new file mode 100644 index 0000000..ad46e7a --- /dev/null +++ b/lib/create-task-streams.js @@ -0,0 +1,34 @@ +/** + * @module create-task-streams + * See LICENSE file in root directory for full license. + */ +"use strict" + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const createWriteStream = require("fs").createWriteStream + +//------------------------------------------------------------------------------ +// Public Interface +//------------------------------------------------------------------------------ + +/** + * Creates output and error stream for a task + * + * @param {string} fileName - fileName + * @returns {object} streams outStream errStream + */ +module.exports = function createTaskStreams(fileName) { + if (!fileName) { + return undefined + } + + const sanitizedFileName = fileName.replace(/\W/g, "_") + + const outStream = createWriteStream(`${sanitizedFileName}.out`) + const errStream = createWriteStream(`${sanitizedFileName}.err`) + + return { outStream, errStream } +} diff --git a/lib/index.js b/lib/index.js index e36a605..ac3845d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -219,6 +219,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) { //eslint-disab const parallel = Boolean(options && options.parallel) const silent = Boolean(options && options.silent) const continueOnError = Boolean(options && options.continueOnError) + const reportByTask = Boolean(options && options.reportByTask) const printLabel = Boolean(options && options.printLabel) const printName = Boolean(options && options.printName) const race = Boolean(options && options.race) @@ -266,6 +267,7 @@ module.exports = function npmRunAll(patternOrPatterns, options) { //eslint-disab stderr, prefixOptions, continueOnError, + reportByTask, labelState: { enabled: printLabel, width: labelWidth, diff --git a/lib/run-task.js b/lib/run-task.js index b875dcd..da146fa 100644 --- a/lib/run-task.js +++ b/lib/run-task.js @@ -15,6 +15,7 @@ const chalk = require("chalk") const parseArgs = require("shell-quote").parse const padEnd = require("string.prototype.padend") const createHeader = require("./create-header") +const createTaskStreams = require("./create-task-streams") const createPrefixTransform = require("./create-prefix-transform-stream") const spawn = require("./spawn") @@ -142,7 +143,19 @@ module.exports = function runTask(task, options) { const stdinKind = detectStreamKind(stdin, process.stdin) const stdoutKind = detectStreamKind(stdout, process.stdout) const stderrKind = detectStreamKind(stderr, process.stderr) - const spawnOptions = { stdio: [stdinKind, stdoutKind, stderrKind] } + let spawnOutKind = stdoutKind + let spawnErrKind = stderrKind + + let childStreams = undefined + if (options.reportByTask) { + childStreams = createTaskStreams(task) + if (childStreams) { + spawnOutKind = "pipe" + spawnErrKind = "pipe" + } + } + + const spawnOptions = { stdio: [stdinKind, spawnOutKind, spawnErrKind] } // Print task name. if (options.printName && stdout != null) { @@ -177,11 +190,17 @@ module.exports = function runTask(task, options) { if (stdinKind === "pipe") { stdin.pipe(cp.stdin) } - if (stdoutKind === "pipe") { + if (spawnOutKind === "pipe") { cp.stdout.pipe(stdout, { end: false }) + if (childStreams) { + cp.stdout.pipe(childStreams.outStream) + } } - if (stderrKind === "pipe") { + if (spawnErrKind === "pipe") { cp.stderr.pipe(stderr, { end: false }) + if (childStreams) { + cp.stderr.pipe(childStreams.errStream) + } } // Register