Skip to content

Commit 7fc4803

Browse files
committed
Make it possible to use no colors at all through API
Closes #466
1 parent f4fb4f7 commit 7fc4803

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

src/concurrently.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ export type ConcurrentlyOptions = {
8282
* Available background colors:
8383
* - `bgBlack`, `bgRed`, `bgGreen`, `bgYellow`, `bgBlue`, `bgMagenta`, `bgCyan`, `bgWhite`
8484
*
85+
* Set to `false` to disable colors.
86+
*
8587
* @see {@link https://www.npmjs.com/package/chalk} for more information.
8688
*/
87-
prefixColors?: string | string[];
89+
prefixColors?: string | string[] | false;
8890

8991
/**
9092
* Maximum number of commands to run at once.
@@ -169,7 +171,7 @@ export function concurrently(
169171

170172
const options = _.defaults(baseOptions, defaults);
171173

172-
const prefixColorSelector = new PrefixColorSelector(options.prefixColors);
174+
const prefixColorSelector = new PrefixColorSelector(options.prefixColors || []);
173175

174176
const commandParsers: CommandParser[] = [
175177
new StripQuotes(),

src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export function concurrently(
121121
timestampFormat: options.timestampFormat,
122122
});
123123

124+
if (options.prefixColors === false) {
125+
logger.toggleColors(false);
126+
}
127+
124128
const abortController = new AbortController();
125129

126130
return createConcurrently(commands, {

src/logger.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,31 @@ describe('#logTable()', () => {
406406
);
407407
});
408408
});
409+
410+
describe('#toggleColors()', () => {
411+
it('uses supported color level when on', () => {
412+
const { logger, spy } = createLogger({});
413+
logger.toggleColors(true);
414+
415+
const command1 = new FakeCommand('foo', 'command', 0, { prefixColor: 'red' });
416+
logger.logCommandText('bar', command1);
417+
logger.logGlobalEvent('baz');
418+
419+
const texts = spy.getValues().map((value) => value.text);
420+
expect(texts).toContain(chalk.red('[foo]') + ' ');
421+
expect(texts).toContain(chalk.reset('-->') + ' ');
422+
});
423+
424+
it('uses no colors when off', () => {
425+
const { logger, spy } = createLogger({});
426+
logger.toggleColors(false);
427+
428+
const command1 = new FakeCommand('foo', 'command', 0, { prefixColor: 'red' });
429+
logger.logCommandText('bar', command1);
430+
logger.logGlobalEvent('baz');
431+
432+
const texts = spy.getValues().map((value) => value.text);
433+
expect(texts).toContain('[foo] ');
434+
expect(texts).toContain('--> ');
435+
});
436+
});

src/logger.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import chalk from 'chalk';
1+
import chalk, { Chalk } from 'chalk';
22
import formatDate from 'date-fns/format';
33
import _ from 'lodash';
44
import * as Rx from 'rxjs';
55

66
import { Command, CommandIdentifier } from './command';
77
import * as defaults from './defaults';
88

9+
const defaultChalk = chalk;
10+
const noColorChalk = new chalk.Instance({ level: 0 });
11+
912
export class Logger {
1013
private readonly hide: CommandIdentifier[];
1114
private readonly raw: boolean;
1215
private readonly prefixFormat?: string;
1316
private readonly commandLength: number;
1417
private readonly timestampFormat: string;
1518

19+
private chalk: Chalk = defaultChalk;
20+
1621
/**
1722
* How many characters should a prefix have.
1823
* Prefixes shorter than this will be padded with spaces to the right.
@@ -73,6 +78,13 @@ export class Logger {
7378
this.timestampFormat = timestampFormat || defaults.timestampFormat;
7479
}
7580

81+
/**
82+
* Toggles colors on/off globally.
83+
*/
84+
toggleColors(on: boolean) {
85+
this.chalk = on ? defaultChalk : noColorChalk;
86+
}
87+
7688
private shortenText(text: string) {
7789
if (!text || text.length <= this.commandLength) {
7890
return text;
@@ -142,10 +154,10 @@ export class Logger {
142154
colorText(command: Command, text: string) {
143155
let color: chalk.Chalk;
144156
if (command.prefixColor && command.prefixColor.startsWith('#')) {
145-
color = chalk.hex(command.prefixColor);
157+
color = this.chalk.hex(command.prefixColor);
146158
} else {
147-
const defaultColor = _.get(chalk, defaults.prefixColors, chalk.reset);
148-
color = _.get(chalk, command.prefixColor ?? '', defaultColor);
159+
const defaultColor = _.get(this.chalk, defaults.prefixColors, this.chalk.reset);
160+
color = _.get(this.chalk, command.prefixColor ?? '', defaultColor);
149161
}
150162
return color(text);
151163
}
@@ -167,7 +179,7 @@ export class Logger {
167179
if (this.lastWrite?.command === command && this.lastWrite.char !== '\n') {
168180
prefix = '\n';
169181
}
170-
this.logCommandText(prefix + chalk.reset(text) + '\n', command);
182+
this.logCommandText(prefix + this.chalk.reset(text) + '\n', command);
171183
}
172184

173185
logCommandText(text: string, command: Command) {
@@ -189,7 +201,7 @@ export class Logger {
189201
return;
190202
}
191203

192-
this.log(chalk.reset('-->') + ' ', chalk.reset(text) + '\n');
204+
this.log(this.chalk.reset('-->') + ' ', this.chalk.reset(text) + '\n');
193205
}
194206

195207
/**

0 commit comments

Comments
 (0)