Skip to content

Commit 4d07995

Browse files
committed
feat(utils): add noLineBreak and noIndent options to logger methods
1 parent 7799197 commit 4d07995

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

packages/utils/src/lib/logger.int.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ ${ansis.red('Failed to load config')}
129129
);
130130
});
131131

132+
it('should skip line-break if requested', () => {
133+
const logger = new Logger();
134+
135+
logger.info('Code PushUp CLI', { noLineBreak: true });
136+
logger.debug(' - v1.2.3', { noLineBreak: true, force: true });
137+
logger.warn(' (Config file in CommonJS format)', { noLineBreak: true });
138+
logger.error(' => Failed to load config', { noLineBreak: true });
139+
140+
expect(stdout).toBe(
141+
`Code PushUp CLI${ansis.gray(' - v1.2.3')}${ansis.yellow(' (Config file in CommonJS format)')}${ansis.red(' => Failed to load config')}`,
142+
);
143+
});
144+
132145
it('should set verbose flag and environment variable', () => {
133146
vi.stubEnv('CP_VERBOSE', 'false');
134147
const logger = new Logger();
@@ -592,6 +605,40 @@ ${ansis.red('✖')} Uploading report to portal → ${ansis.red('GraphQL error: I
592605
`${ansis.green('✔')} ${ansis.blue('src')} ${ansis.green('$')} npx eslint . --format=json ${ansis.gray('(42 ms)')}\n`,
593606
);
594607
});
608+
609+
it('should skip indentation for inner logs in CI if requested', async () => {
610+
vi.stubEnv('CI', 'true');
611+
const logger = new Logger();
612+
613+
await logger.command('npx code-pushup', async () => {
614+
logger.newline();
615+
logger.info(
616+
'::group::npx nx lint\nAll files pass linting.\n::endgroup\n',
617+
{ noIndent: true },
618+
);
619+
logger.info(
620+
'Collected report:\n- .code-pushup/report.json\n- .code-pushup/report.md',
621+
{ noIndent: true },
622+
);
623+
logger.newline();
624+
});
625+
626+
expect(ansis.strip(stdout)).toBe(
627+
`
628+
- $ npx code-pushup
629+
630+
::group::npx nx lint
631+
All files pass linting.
632+
::endgroup
633+
634+
Collected report:
635+
- .code-pushup/report.json
636+
- .code-pushup/report.md
637+
638+
✔ $ npx code-pushup (42 ms)
639+
`.trimStart(),
640+
);
641+
});
595642
});
596643

597644
describe('spinners + groups', () => {

packages/utils/src/lib/logger.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import { settlePromise } from './promises.js';
1212
type GroupColor = Extract<AnsiColors, 'cyan' | 'magenta'>;
1313
type CiPlatform = 'GitHub Actions' | 'GitLab CI/CD';
1414

15+
/** Additional options for log methods */
16+
export type LogOptions = {
17+
/** Do not append line-feed to message (`process.stdout.write` instead of `console.log`) */
18+
noLineBreak?: boolean;
19+
/** Do not indent lines even if logged while a spinner is active */
20+
noIndent?: boolean;
21+
};
22+
23+
/** Additional options for {@link Logger.debug} method */
24+
export type DebugLogOptions = LogOptions & {
25+
/** Print debug message even if verbose flag is not set */
26+
force?: boolean;
27+
};
28+
1529
const HEX_RADIX = 16;
1630

1731
const SIGINT_CODE = 2;
@@ -79,9 +93,10 @@ export class Logger {
7993
* logger.error('Config file is invalid');
8094
*
8195
* @param message Error text
96+
* @param options Additional options
8297
*/
83-
error(message: string): void {
84-
this.#log(message, 'red');
98+
error(message: string, options?: LogOptions): void {
99+
this.#log(message, 'red', options);
85100
}
86101

87102
/**
@@ -93,9 +108,10 @@ export class Logger {
93108
* logger.warn('Skipping invalid audits');
94109
*
95110
* @param message Warning text
111+
* @param options Additional options
96112
*/
97-
warn(message: string): void {
98-
this.#log(message, 'yellow');
113+
warn(message: string, options?: LogOptions): void {
114+
this.#log(message, 'yellow', options);
99115
}
100116

101117
/**
@@ -107,9 +123,10 @@ export class Logger {
107123
* logger.info('Code PushUp CLI v0.80.2');
108124
*
109125
* @param message Info text
126+
* @param options Additional options
110127
*/
111-
info(message: string): void {
112-
this.#log(message);
128+
info(message: string, options?: LogOptions): void {
129+
this.#log(message, undefined, options);
113130
}
114131

115132
/**
@@ -122,11 +139,10 @@ export class Logger {
122139
*
123140
* @param message Debug text
124141
* @param options Additional options
125-
* @param options.force Print debug message even if verbose flag is not set
126142
*/
127-
debug(message: string, options?: { force?: boolean }): void {
143+
debug(message: string, options?: DebugLogOptions): void {
128144
if (this.#isVerbose || options?.force) {
129-
this.#log(message, 'gray');
145+
this.#log(message, 'gray', options);
130146
}
131147
}
132148

@@ -457,17 +473,24 @@ export class Logger {
457473
return result.value;
458474
}
459475

460-
#log(message: string, color?: AnsiColors): void {
476+
#log(message: string, color?: AnsiColors, options?: LogOptions): void {
477+
const print: (text: string) => void = options?.noLineBreak
478+
? text => process.stdout.write(text)
479+
: console.log;
480+
461481
if (this.#activeSpinner) {
462482
if (this.#activeSpinner.isSpinning) {
463483
this.#activeSpinnerLogs.push(this.#format(message, color));
464484
} else {
465-
console.log(this.#format(indentLines(message, 2), color));
485+
const indented =
486+
options?.noIndent || !message ? message : indentLines(message, 2);
487+
print(this.#format(indented, color));
466488
}
467489
} else {
468-
console.log(this.#format(message, color));
490+
print(this.#format(message, color));
469491
}
470-
this.#endsWithBlankLine = !message || message.endsWith('\n');
492+
this.#endsWithBlankLine =
493+
(!message || message.endsWith('\n')) && !options?.noIndent;
471494
}
472495

473496
#format(message: string, color: AnsiColors | undefined): string {

0 commit comments

Comments
 (0)