Skip to content

Commit

Permalink
Make it possible to use no colors at all through API
Browse files Browse the repository at this point in the history
Closes #466
  • Loading branch information
gustavohenke committed Sep 5, 2024
1 parent f4fb4f7 commit 7fc4803
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ export type ConcurrentlyOptions = {
* Available background colors:
* - `bgBlack`, `bgRed`, `bgGreen`, `bgYellow`, `bgBlue`, `bgMagenta`, `bgCyan`, `bgWhite`
*
* Set to `false` to disable colors.
*
* @see {@link https://www.npmjs.com/package/chalk} for more information.
*/
prefixColors?: string | string[];
prefixColors?: string | string[] | false;

/**
* Maximum number of commands to run at once.
Expand Down Expand Up @@ -169,7 +171,7 @@ export function concurrently(

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

const prefixColorSelector = new PrefixColorSelector(options.prefixColors);
const prefixColorSelector = new PrefixColorSelector(options.prefixColors || []);

const commandParsers: CommandParser[] = [
new StripQuotes(),
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export function concurrently(
timestampFormat: options.timestampFormat,
});

if (options.prefixColors === false) {
logger.toggleColors(false);
}

const abortController = new AbortController();

return createConcurrently(commands, {
Expand Down
28 changes: 28 additions & 0 deletions src/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,31 @@ describe('#logTable()', () => {
);
});
});

describe('#toggleColors()', () => {
it('uses supported color level when on', () => {
const { logger, spy } = createLogger({});
logger.toggleColors(true);

const command1 = new FakeCommand('foo', 'command', 0, { prefixColor: 'red' });
logger.logCommandText('bar', command1);
logger.logGlobalEvent('baz');

const texts = spy.getValues().map((value) => value.text);
expect(texts).toContain(chalk.red('[foo]') + ' ');
expect(texts).toContain(chalk.reset('-->') + ' ');
});

it('uses no colors when off', () => {
const { logger, spy } = createLogger({});
logger.toggleColors(false);

const command1 = new FakeCommand('foo', 'command', 0, { prefixColor: 'red' });
logger.logCommandText('bar', command1);
logger.logGlobalEvent('baz');

const texts = spy.getValues().map((value) => value.text);
expect(texts).toContain('[foo] ');
expect(texts).toContain('--> ');
});
});
24 changes: 18 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import chalk from 'chalk';
import chalk, { Chalk } from 'chalk';
import formatDate from 'date-fns/format';
import _ from 'lodash';
import * as Rx from 'rxjs';

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

const defaultChalk = chalk;
const noColorChalk = new chalk.Instance({ level: 0 });

export class Logger {
private readonly hide: CommandIdentifier[];
private readonly raw: boolean;
private readonly prefixFormat?: string;
private readonly commandLength: number;
private readonly timestampFormat: string;

private chalk: Chalk = defaultChalk;

/**
* How many characters should a prefix have.
* Prefixes shorter than this will be padded with spaces to the right.
Expand Down Expand Up @@ -73,6 +78,13 @@ export class Logger {
this.timestampFormat = timestampFormat || defaults.timestampFormat;
}

/**
* Toggles colors on/off globally.
*/
toggleColors(on: boolean) {
this.chalk = on ? defaultChalk : noColorChalk;
}

private shortenText(text: string) {
if (!text || text.length <= this.commandLength) {
return text;
Expand Down Expand Up @@ -142,10 +154,10 @@ export class Logger {
colorText(command: Command, text: string) {
let color: chalk.Chalk;
if (command.prefixColor && command.prefixColor.startsWith('#')) {
color = chalk.hex(command.prefixColor);
color = this.chalk.hex(command.prefixColor);
} else {
const defaultColor = _.get(chalk, defaults.prefixColors, chalk.reset);
color = _.get(chalk, command.prefixColor ?? '', defaultColor);
const defaultColor = _.get(this.chalk, defaults.prefixColors, this.chalk.reset);
color = _.get(this.chalk, command.prefixColor ?? '', defaultColor);
}
return color(text);
}
Expand All @@ -167,7 +179,7 @@ export class Logger {
if (this.lastWrite?.command === command && this.lastWrite.char !== '\n') {
prefix = '\n';
}
this.logCommandText(prefix + chalk.reset(text) + '\n', command);
this.logCommandText(prefix + this.chalk.reset(text) + '\n', command);
}

logCommandText(text: string, command: Command) {
Expand All @@ -189,7 +201,7 @@ export class Logger {
return;
}

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

/**
Expand Down

0 comments on commit 7fc4803

Please sign in to comment.