Skip to content

Commit d3888a1

Browse files
committed
feat(plugin-lighthouse): replace @poppinss/cliui with new logger
1 parent 8311628 commit d3888a1

File tree

11 files changed

+82
-94
lines changed

11 files changed

+82
-94
lines changed

packages/plugin-lighthouse/src/lib/normalize-flags.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { bold, yellow } from 'ansis';
2-
import { ui } from '@code-pushup/utils';
3-
import { LIGHTHOUSE_PLUGIN_SLUG } from './constants.js';
1+
import ansis from 'ansis';
2+
import { logger } from '@code-pushup/utils';
43
import { DEFAULT_CLI_FLAGS } from './runner/constants.js';
54
import type { LighthouseCliFlags } from './runner/types.js';
65
import type { LighthouseOptions } from './types.js';
@@ -73,10 +72,8 @@ export function logUnsupportedFlagsInUse(
7372
if (unsupportedFlagsInUse.length > 0) {
7473
const postFix = (count: number) =>
7574
count > displayCount ? ` and ${count - displayCount} more.` : '';
76-
ui().logger.debug(
77-
`${yellow('⚠')} Plugin ${bold(
78-
LIGHTHOUSE_PLUGIN_SLUG,
79-
)} used unsupported flags: ${bold(
75+
logger.warn(
76+
`Used unsupported flags: ${ansis.bold(
8077
unsupportedFlagsInUse.slice(0, displayCount).join(', '),
8178
)}${postFix(unsupportedFlagsInUse.length)}`,
8279
);

packages/plugin-lighthouse/src/lib/normalize-flags.unit.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { bold, yellow } from 'ansis';
1+
import ansis from 'ansis';
22
import path from 'node:path';
33
import { describe, expect, it } from 'vitest';
4-
import { ui } from '@code-pushup/utils';
4+
import { logger } from '@code-pushup/utils';
55
import { DEFAULT_CHROME_FLAGS, LIGHTHOUSE_OUTPUT_PATH } from './constants.js';
66
import { logUnsupportedFlagsInUse, normalizeFlags } from './normalize-flags.js';
77
import { LIGHTHOUSE_REPORT_NAME } from './runner/constants.js';
@@ -10,12 +10,9 @@ import type { LighthouseOptions } from './types.js';
1010
describe('logUnsupportedFlagsInUse', () => {
1111
it('should log unsupported entries', () => {
1212
logUnsupportedFlagsInUse({ 'list-all-audits': true } as LighthouseOptions);
13-
expect(ui()).toHaveLoggedTimes(1);
14-
expect(ui()).toHaveLogged(
15-
'debug',
16-
`${yellow('⚠')} Plugin ${bold(
17-
'lighthouse',
18-
)} used unsupported flags: ${bold('list-all-audits')}`,
13+
expect(logger.warn).toHaveBeenCalledTimes(1);
14+
expect(logger.warn).toHaveBeenCalledWith(
15+
`Used unsupported flags: ${ansis.bold('list-all-audits')}`,
1916
);
2017
});
2118

@@ -32,12 +29,9 @@ describe('logUnsupportedFlagsInUse', () => {
3229
// unsupported
3330
...unsupportedFlags,
3431
} as unknown as LighthouseOptions);
35-
expect(ui()).toHaveLoggedTimes(1);
36-
expect(ui()).toHaveLogged(
37-
'debug',
38-
`${yellow('⚠')} Plugin ${bold(
39-
'lighthouse',
40-
)} used unsupported flags: ${bold(
32+
expect(logger.warn).toHaveBeenCalledTimes(1);
33+
expect(logger.warn).toHaveBeenCalledWith(
34+
`Used unsupported flags: ${ansis.bold(
4135
'list-all-audits, list-locales, list-trace-categories',
4236
)} and 3 more.`,
4337
);
@@ -119,7 +113,7 @@ describe('normalizeFlags', () => {
119113
...supportedFlags,
120114
} as unknown as LighthouseOptions),
121115
).toEqual(expect.not.objectContaining({ 'list-all-audits': true }));
122-
expect(ui()).toHaveLoggedTimes(1);
116+
expect(logger.warn).toHaveBeenCalledTimes(1);
123117
});
124118

125119
it('should remove any flag with an empty array as a value', () => {

packages/plugin-lighthouse/src/lib/runner/details/details.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { bold, yellow } from 'ansis';
1+
import ansis from 'ansis';
22
import type { FormattedIcu } from 'lighthouse';
33
import type Details from 'lighthouse/types/lhr/audit-details';
44
import type { Result } from 'lighthouse/types/lhr/audit-result';
55
import type { AuditDetails } from '@code-pushup/models';
6-
import { ui } from '@code-pushup/utils';
7-
import { PLUGIN_SLUG } from '../constants.js';
6+
import { logger } from '@code-pushup/utils';
87
import { parseCriticalRequestChainToAuditDetails } from './critical-request-chain.type.js';
98
import { parseOpportunityToAuditDetailsTable } from './opportunity.type.js';
109
import { parseTableToAuditDetailsTable } from './table.type.js';
@@ -58,10 +57,8 @@ export function logUnsupportedDetails(lhrAudits: Result[]) {
5857
),
5958
];
6059
if (slugsWithDetailParsingErrors.length > 0) {
61-
ui().logger.debug(
62-
`${yellow('⚠')} Plugin ${bold(
63-
PLUGIN_SLUG,
64-
)} skipped parsing of unsupported audit details: ${bold(
60+
logger.warn(
61+
`Skipped parsing of unsupported audit details: ${ansis.bold(
6562
slugsWithDetailParsingErrors.join(', '),
6663
)}.`,
6764
);

packages/plugin-lighthouse/src/lib/runner/details/details.unit.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import { bold, yellow } from 'ansis';
1+
import ansis from 'ansis';
22
import type { FormattedIcu } from 'lighthouse';
33
import type Details from 'lighthouse/types/lhr/audit-details';
44
import type { Result } from 'lighthouse/types/lhr/audit-result';
55
import { describe, expect, it } from 'vitest';
66
import type { AuditDetails } from '@code-pushup/models';
7-
import { ui } from '@code-pushup/utils';
7+
import { logger } from '@code-pushup/utils';
88
import { logUnsupportedDetails, toAuditDetails } from './details.js';
99

1010
describe('logUnsupportedDetails', () => {
1111
it('should log unsupported entries', () => {
1212
logUnsupportedDetails([{ details: { type: 'screenshot' } }] as Result[]);
13-
expect(ui()).toHaveLoggedTimes(1);
14-
expect(ui()).toHaveLogged(
15-
'debug',
16-
`${yellow('⚠')} Plugin ${bold(
17-
'lighthouse',
18-
)} skipped parsing of unsupported audit details: ${bold('screenshot')}.`,
13+
expect(logger.warn).toHaveBeenCalledTimes(1);
14+
expect(logger.warn).toHaveBeenCalledWith(
15+
`Skipped parsing of unsupported audit details: ${ansis.bold('screenshot')}.`,
1916
);
2017
});
2118

@@ -29,12 +26,9 @@ describe('logUnsupportedDetails', () => {
2926
{ details: { type: 'treemap-data' } },
3027
{ details: { type: 'criticalrequestchain' } },
3128
] as Result[]);
32-
expect(ui()).toHaveLoggedTimes(1);
33-
expect(ui()).toHaveLogged(
34-
'debug',
35-
`${yellow('⚠')} Plugin ${bold(
36-
'lighthouse',
37-
)} skipped parsing of unsupported audit details: ${bold(
29+
expect(logger.warn).toHaveBeenCalledTimes(1);
30+
expect(logger.warn).toHaveBeenCalledWith(
31+
`Skipped parsing of unsupported audit details: ${ansis.bold(
3832
'filmstrip, screenshot, debugdata',
3933
)}.`,
4034
);

packages/plugin-lighthouse/src/lib/runner/details/item-value.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { bold } from 'ansis';
1+
import ansis from 'ansis';
22
import type { IcuMessage } from 'lighthouse';
33
import type Details from 'lighthouse/types/lhr/audit-details';
44
import {
55
formatBytes,
66
formatDuration,
77
html,
8+
logger,
89
roundDecimals,
910
truncateText,
10-
ui,
1111
} from '@code-pushup/utils';
1212

1313
export type PrimitiveItemValue = string | number | boolean;
@@ -85,11 +85,11 @@ export function formatTableItemPropertyValue(
8585
return truncateText(String(parsedItemValue), 500);
8686
case 'multi': // @TODO
8787
// @TODO log verbose first, then implement data type
88-
ui().logger.info(`Format type ${bold('multi')} is not implemented`);
88+
logger.debug(`Format type ${ansis.bold('multi')} is not implemented`);
8989
return '';
9090
case 'thumbnail': // @TODO
9191
// @TODO log verbose first, then implement data type
92-
ui().logger.info(`Format type ${bold('thumbnail')} is not implemented`);
92+
logger.debug(`Format type ${ansis.bold('thumbnail')} is not implemented`);
9393
return '';
9494
}
9595
/* eslint-enable @typescript-eslint/no-magic-numbers */
@@ -145,13 +145,11 @@ export function parseTableItemPropertyValue(
145145
return String(url);
146146
case 'subitems':
147147
// @TODO log verbose first, then implement data type
148-
ui().logger.info(`Value type ${bold('subitems')} is not implemented`);
148+
logger.debug(`Value type ${ansis.bold('subitems')} is not implemented`);
149149
return '';
150150
case 'debugdata':
151151
// @TODO log verbose first, then implement data type
152-
ui().logger.info(`Value type ${bold('debugdata')} is not implemented`, {
153-
silent: true,
154-
});
152+
logger.debug(`Value type ${ansis.bold('debugdata')} is not implemented`);
155153
return '';
156154
}
157155
// IcuMessage

packages/plugin-lighthouse/src/lib/runner/details/item-value.unit.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { bold } from 'ansis';
1+
import ansis from 'ansis';
22
import type Details from 'lighthouse/types/lhr/audit-details';
33
import { beforeAll, describe, expect, it } from 'vitest';
4-
import { ui } from '@code-pushup/utils';
4+
import { logger, ui } from '@code-pushup/utils';
55
import {
66
type SimpleItemValue,
77
formatTableItemPropertyValue,
@@ -146,17 +146,15 @@ describe('parseTableItemPropertyValue', () => {
146146
}),
147147
).toBe('');
148148

149-
expect(ui()).toHaveLogged(
150-
'info',
151-
`Value type ${bold('subitems')} is not implemented`,
149+
expect(logger.debug).toHaveBeenCalledWith(
150+
`Value type ${ansis.bold('subitems')} is not implemented`,
152151
);
153152
});
154153

155154
it('should parse value item debugdata to empty string and log implemented', () => {
156155
expect(parseTableItemPropertyValue({ type: 'debugdata' })).toBe('');
157-
expect(ui()).toHaveLogged(
158-
'info',
159-
`Value type ${bold('debugdata')} is not implemented`,
156+
expect(logger.debug).toHaveBeenCalledWith(
157+
`Value type ${ansis.bold('debugdata')} is not implemented`,
160158
);
161159
});
162160

@@ -363,9 +361,8 @@ describe('formatTableItemPropertyValue', () => {
363361
),
364362
).toBe('');
365363

366-
expect(ui()).toHaveLogged(
367-
'info',
368-
`Format type ${bold('multi')} is not implemented`,
364+
expect(logger.debug).toHaveBeenCalledWith(
365+
`Format type ${ansis.bold('multi')} is not implemented`,
369366
);
370367
});
371368

@@ -376,9 +373,8 @@ describe('formatTableItemPropertyValue', () => {
376373
'thumbnail',
377374
),
378375
).toBe('');
379-
expect(ui()).toHaveLogged(
380-
'info',
381-
`Format type ${bold('thumbnail')} is not implemented`,
376+
expect(logger.debug).toHaveBeenCalledWith(
377+
`Format type ${ansis.bold('thumbnail')} is not implemented`,
382378
);
383379
});
384380

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bold } from 'ansis';
1+
import ansis from 'ansis';
22
import type Details from 'lighthouse/types/lhr/audit-details';
33
import { stringifyError } from '@code-pushup/utils';
44

@@ -9,9 +9,9 @@ export class LighthouseAuditDetailsParsingError extends Error {
99
error: unknown,
1010
) {
1111
super(
12-
`Parsing lighthouse report details ${bold(
12+
`Parsing lighthouse report details ${ansis.bold(
1313
type,
14-
)} failed: \nRaw data:\n ${JSON.stringify(rawTable, null, 2)}\n${stringifyError(error)}`,
14+
)} failed:\nRaw data:\n${JSON.stringify(rawTable, null, 2)}\n${stringifyError(error)}`,
1515
);
1616
}
1717
}

packages/plugin-lighthouse/src/lib/runner/runner.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { Config, RunnerResult } from 'lighthouse';
22
import { runLighthouse } from 'lighthouse/cli/run.js';
33
import path from 'node:path';
44
import type { AuditOutputs, RunnerFunction } from '@code-pushup/models';
5-
import { ensureDirectoryExists, stringifyError, ui } from '@code-pushup/utils';
5+
import {
6+
ensureDirectoryExists,
7+
link,
8+
logger,
9+
stringifyError,
10+
} from '@code-pushup/utils';
611
import { orderSlug, shouldExpandForUrls } from '../processing.js';
712
import type { LighthouseOptions } from '../types.js';
813
import { DEFAULT_CLI_FLAGS } from './constants.js';
@@ -46,7 +51,7 @@ export function createRunnerFunction(
4651

4752
return [...acc, ...processedOutputs];
4853
} catch (error) {
49-
ui().logger.warning(stringifyError(error));
54+
logger.warn(stringifyError(error));
5055
return acc;
5156
}
5257
}, Promise.resolve<AuditOutputs>([]));
@@ -74,7 +79,9 @@ async function runLighthouseForUrl(
7479
const runnerResult: unknown = await runLighthouse(url, flags, config);
7580

7681
if (runnerResult == null) {
77-
throw new Error(`Lighthouse did not produce a result for URL: ${url}`);
82+
throw new Error(
83+
`Lighthouse did not produce a result for URL: ${link(url)}`,
84+
);
7885
}
7986

8087
const { lhr } = runnerResult as RunnerResult;

0 commit comments

Comments
 (0)