diff --git a/src/concurrently.ts b/src/concurrently.ts index 8c2bfc15..e507343d 100644 --- a/src/concurrently.ts +++ b/src/concurrently.ts @@ -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. @@ -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(), diff --git a/src/index.ts b/src/index.ts index ccf1d517..e69b9e09 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,6 +121,10 @@ export function concurrently( timestampFormat: options.timestampFormat, }); + if (options.prefixColors === false) { + logger.toggleColors(false); + } + const abortController = new AbortController(); return createConcurrently(commands, { diff --git a/src/logger.spec.ts b/src/logger.spec.ts index 1b2cbf09..fe314a0e 100644 --- a/src/logger.spec.ts +++ b/src/logger.spec.ts @@ -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('--> '); + }); +}); diff --git a/src/logger.ts b/src/logger.ts index 3e7a3a41..00018a42 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,4 +1,4 @@ -import chalk from 'chalk'; +import chalk, { Chalk } from 'chalk'; import formatDate from 'date-fns/format'; import _ from 'lodash'; import * as Rx from 'rxjs'; @@ -6,6 +6,9 @@ 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; @@ -13,6 +16,8 @@ export class Logger { 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. @@ -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; @@ -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); } @@ -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) { @@ -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'); } /**