Skip to content

Commit

Permalink
Fix colors option when using wildcard commands (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloraisi authored Oct 2, 2021
1 parent 105445c commit ed8d792
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 6 additions & 12 deletions bin/concurrently.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ module.exports = exports = (commands, options = {}) => {
logger,
conditions: options.killOthers
})
]
],
prefixColors: options.prefixColors || []
});
};

Expand Down
37 changes: 22 additions & 15 deletions src/concurrently.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/concurrently.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down

0 comments on commit ed8d792

Please sign in to comment.