Skip to content

Commit 29e8039

Browse files
committed
feat(utils): provide helper for consistent plugin meta logs formatting
1 parent 38ad415 commit 29e8039

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export {
5050
formatBytes,
5151
formatDuration,
5252
indentLines,
53+
pluginMetaLogFormatter,
5354
pluralize,
5455
pluralizeToken,
5556
roundDecimals,

packages/utils/src/lib/formatting.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ansis from 'ansis';
2+
import stringWidth from 'string-width';
13
import {
24
MAX_DESCRIPTION_LENGTH,
35
MAX_ISSUE_MESSAGE_LENGTH,
@@ -146,7 +148,7 @@ export function truncateMultilineText(
146148

147149
export function transformLines(
148150
text: string,
149-
fn: (line: string) => string,
151+
fn: (line: string, index: number) => string,
150152
): string {
151153
return text.split(/\r?\n/).map(fn).join('\n');
152154
}
@@ -164,3 +166,15 @@ export function serializeCommandWithArgs({
164166
}): string {
165167
return [command, ...(args ?? [])].join(' ');
166168
}
169+
170+
export function pluginMetaLogFormatter(
171+
title: string,
172+
): (message: string) => string {
173+
const prefix = ansis.blue(`[${title}]`);
174+
const padding = ' '.repeat(stringWidth(prefix));
175+
return message =>
176+
transformLines(
177+
message,
178+
(line, idx) => `${idx === 0 ? prefix : padding} ${line}`,
179+
);
180+
}

packages/utils/src/lib/formatting.unit.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
formatDate,
66
formatDuration,
77
indentLines,
8+
pluginMetaLogFormatter,
89
pluralize,
910
pluralizeToken,
1011
roundDecimals,
@@ -269,3 +270,28 @@ describe('serializeCommandWithArgs', () => {
269270
expect(serializeCommandWithArgs({ command: 'ls' })).toBe('ls');
270271
});
271272
});
273+
274+
describe('pluginMetaLogFormatter', () => {
275+
it('should prefix plugin title', () => {
276+
expect(pluginMetaLogFormatter('ESLint')('Found 42 rules in total')).toBe(
277+
`${ansis.blue('[ESLint]')} Found 42 rules in total`,
278+
);
279+
});
280+
281+
it('should align multiline message with prefix', () => {
282+
expect(
283+
ansis.strip(
284+
pluginMetaLogFormatter('Coverage')(
285+
'Created 3 groups:\n- Line coverage\n- Branch coverage\n- Function coverage',
286+
),
287+
),
288+
).toBe(
289+
`
290+
[Coverage] Created 3 groups:
291+
- Line coverage
292+
- Branch coverage
293+
- Function coverage
294+
`.trim(),
295+
);
296+
});
297+
});

0 commit comments

Comments
 (0)