diff --git a/README.md b/README.md index ddd144c9..c0ced688 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,9 @@ concurrently can be used programmatically by using the API documented below: - `prefix`: the prefix type to use when logging processes output. Possible values: `index`, `pid`, `time`, `command`, `name`, `none`, or a template (eg `[{time} process: {pid}]`). Default: the name of the process, or its index if no name is set. + - `prefixColors`: a list of colors as supported by [chalk](https://www.npmjs.com/package/chalk). + If concurrently would run more commands than there are colors, the last color is repeated. + Prefix colors specified per-command take precedence over this list. - `prefixLength`: how many characters to show when prefixing with `command`. Default: `10` - `raw`: whether raw mode should be used, meaning strictly process output will be logged, without any prefixes, colouring or extra stuff. diff --git a/bin/concurrently.js b/bin/concurrently.js index 8d86bd3f..7ec2fc76 100755 --- a/bin/concurrently.js +++ b/bin/concurrently.js @@ -144,19 +144,12 @@ const args = yargs .epilogue(fs.readFileSync(__dirname + '/epilogue.txt', { encoding: 'utf8' })) .argv; -const prefixColors = args.prefixColors.split(','); const names = (args.names || '').split(args.nameSeparator); -let lastColor; -concurrently(args._.map((command, index) => { - // Use documented behaviour of repeating last colour when specifying more commands than colours - lastColor = prefixColors[index] || lastColor; - return { - command, - prefixColor: lastColor, - name: names[index] - }; -}), { +concurrently(args._.map((command, index) => ({ + command, + name: names[index] +})), { handleInput: args.handleInput, defaultInputTarget: args.defaultInputTarget, killOthers: args.killOthers @@ -165,11 +158,12 @@ concurrently(args._.map((command, index) => { maxProcesses: args.maxProcesses, raw: args.raw, prefix: args.prefix, + prefixColors: args.prefixColors.split(','), prefixLength: args.prefixLength, restartDelay: args.restartAfter, restartTries: args.restartTries, successCondition: args.success, - timestampFormat: args.timestampFormat + timestampFormat: args.timestampFormat, }).then( () => process.exit(0), () => process.exit(1) diff --git a/index.js b/index.js index 3d389073..d21007ac 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,8 @@ module.exports = exports = (commands, options = {}) => { logger, conditions: options.killOthers }) - ] + ], + prefixColors: options.prefixColors || [] }); }; diff --git a/src/concurrently.js b/src/concurrently.js index bd2bc2b5..06b3574e 100644 --- a/src/concurrently.js +++ b/src/concurrently.js @@ -32,21 +32,27 @@ module.exports = (commands, options) => { new ExpandNpmWildcard() ]; + let lastColor = ''; commands = _(commands) .map(mapToCommandInfo) .flatMap(command => parseCommand(command, commandParsers)) - .map((command, index) => new Command( - Object.assign({ - index, - spawnOpts: getSpawnOpts({ - raw: options.raw, - env: command.env, - cwd: command.cwd || options.cwd, - }), - killProcess: options.kill, - spawn: options.spawn, - }, command) - )) + .map((command, index) => { + // Use documented behaviour of repeating last color when specifying more commands than colors + lastColor = options.prefixColors && options.prefixColors[index] || lastColor; + return new Command( + Object.assign({ + index, + spawnOpts: getSpawnOpts({ + raw: options.raw, + env: command.env, + cwd: command.cwd || options.cwd, + }), + prefixColor: lastColor, + killProcess: options.kill, + spawn: options.spawn, + }, command) + ); + }) .value(); const handleResult = options.controllers.reduce( @@ -75,13 +81,14 @@ module.exports = (commands, options) => { }; function mapToCommandInfo(command) { - return { + return Object.assign({ command: command.command || command, name: command.name || '', - prefixColor: command.prefixColor || '', env: command.env || {}, cwd: command.cwd || '', - }; + }, command.prefixColor ? { + prefixColor: command.prefixColor, + } : {}); } function parseCommand(command, parsers) { diff --git a/src/concurrently.spec.js b/src/concurrently.spec.js index 097a5755..7ab45dde 100644 --- a/src/concurrently.spec.js +++ b/src/concurrently.spec.js @@ -84,6 +84,19 @@ it('runs commands with a name or prefix color', () => { }); }); +it('runs commands with a list of colors', () => { + create(['echo', 'kill'], { + prefixColors: ['red'] + }); + + controllers.forEach(controller => { + expect(controller.handle).toHaveBeenCalledWith([ + expect.objectContaining({ command: 'echo', prefixColor: 'red' }), + expect.objectContaining({ command: 'kill', prefixColor: 'red' }), + ]); + }); +}); + it('passes commands wrapped from a controller to the next one', () => { const fakeCommand = createFakeCommand('banana', 'banana'); controllers[0].handle.mockReturnValue({ commands: [fakeCommand] });