@@ -8,6 +8,11 @@ import {promisify} from './promise.js';
8
8
9
9
const child = require ( 'child_process' ) ;
10
10
11
+ const logTransformer = require ( 'strong-log-transformer' ) ;
12
+ const chalk = require ( 'chalk' ) ;
13
+ const colors = [ 'red' , 'yellow' , 'green' , 'cyan' , 'blue' , 'magenta' , 'white' , 'gray' ] ;
14
+ const workspaceColors = { } ;
15
+
11
16
export const queue = new BlockingQueue ( 'child' , constants . CHILD_CONCURRENCY ) ;
12
17
13
18
// TODO: this uid check is kinda whack
@@ -61,7 +66,7 @@ type ProcessFn = (
61
66
export function spawn (
62
67
program : string ,
63
68
args : Array < string > ,
64
- opts ?: child_process$spawnOpts & { detached ?: boolean , process ?: ProcessFn } = { } ,
69
+ opts ?: child_process$spawnOpts & { detached ?: boolean , process ?: ProcessFn , workspaceName ?: string } = { } ,
65
70
onData ?: ( chunk : Buffer | string ) => void ,
66
71
) : Promise < string > {
67
72
const key = opts . cwd || String ( ++ uid ) ;
@@ -116,7 +121,19 @@ export function spawn(
116
121
}
117
122
118
123
if ( proc . stdout ) {
119
- proc . stdout . on ( 'data' , updateStdout ) ;
124
+ // Prefix output with workspace name, if we have it
125
+ if ( opts . workspaceName ) {
126
+ // Stay consistent by using the key that is mapped to proc
127
+ if ( ! workspaceColors [ key ] ) {
128
+ const randInt = Math . floor ( Math . random ( ) * colors . length ) ;
129
+ workspaceColors [ key ] = colors [ randInt ] ;
130
+ }
131
+ const wsColor = chalk [ workspaceColors [ key ] ] ;
132
+ const tag = `${ wsColor . bold ( opts . workspaceName ) } :` ;
133
+ proc . stdout . pipe ( logTransformer ( { tag} ) ) . pipe ( process . stdout ) ;
134
+ } else {
135
+ proc . stdout . on ( 'data' , updateStdout ) ;
136
+ }
120
137
}
121
138
122
139
processingDone = true ;
0 commit comments