Skip to content

Commit

Permalink
Prefix piped streams with rotating colors (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 19, 2017
1 parent 64203e5 commit 20574b0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/ChildProcessUtilities.js
@@ -1,3 +1,4 @@
import chalk from "chalk";
import EventEmitter from "events";
import execa from "execa";
import logTransformer from "strong-log-transformer";
Expand All @@ -9,6 +10,17 @@ let children = 0;
// This is used to alert listeners when all children have exited.
const emitter = new EventEmitter();

// when streaming children are spawned, use this color for prefix
const colorWheel = [
"cyan",
"magenta",
"blue",
"yellow",
"green",
"red",
];
const NUM_COLORS = colorWheel.length;

export default class ChildProcessUtilities {
static exec(command, args, opts, callback) {
const options = Object.assign({}, opts);
Expand All @@ -32,10 +44,12 @@ export default class ChildProcessUtilities {
const options = Object.assign({}, opts);
options.stdio = ["ignore", "pipe", "pipe"];

const colorName = colorWheel[children % NUM_COLORS];
const color = chalk[colorName];
const spawned = _spawn(command, args, options, callback);

const prefixedStdout = logTransformer({ tag: `${prefix}:` });
const prefixedStderr = logTransformer({ tag: `${prefix} ERROR`, mergeMultiline: true });
const prefixedStdout = logTransformer({ tag: `${color.bold(prefix)}:` });
const prefixedStderr = logTransformer({ tag: `${color(prefix)}:`, mergeMultiline: true });

// Avoid "Possible EventEmitter memory leak detected" warning due to piped stdio
if (children > process.stdout.listenerCount("close")) {
Expand Down

0 comments on commit 20574b0

Please sign in to comment.