Skip to content

Commit 4b89dca

Browse files
committed
logger: don't throw when color doesn't exist
Fixes #580
1 parent 632fa05 commit 4b89dca

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/logger.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ describe('#logCommandText()', () => {
211211
expect(logger.log).toHaveBeenCalledWith(`${chalk.blue('[1]')} `, 'foo', cmd);
212212
});
213213

214+
it('logs prefix using default color if prefixColor from command is not a valid color', () => {
215+
const { logger } = createLogger({});
216+
const cmd = new FakeCommand('', undefined, 1, {
217+
prefixColor: 'fake.bold',
218+
});
219+
logger.logCommandText('foo', cmd);
220+
221+
expect(logger.log).toHaveBeenCalledWith(chalk.reset('[1]') + ' ', 'foo', cmd);
222+
});
223+
214224
it('logs prefix in gray dim if prefixColor from command does not exist', () => {
215225
const { logger } = createLogger({});
216226
const cmd = new FakeCommand('', undefined, 1, {

lib/logger.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const noColorChalk = new Chalk({ level: 0 });
1212
function getChalkPath(chalk: ChalkInstance, path: string): ChalkInstance | undefined {
1313
return path
1414
.split('.')
15-
.reduce((prev, key) => (prev as unknown as Record<string, ChalkInstance>)[key], chalk);
15+
.reduce(
16+
(prev, key) => prev && (prev as unknown as Record<string, ChalkInstance>)[key],
17+
chalk,
18+
);
1619
}
1720

1821
export class Logger {

0 commit comments

Comments
 (0)