Skip to content

Commit b868bc0

Browse files
committed
feat(plugin-lighthouse): log initializer steps (audit/group counts, expanded, skipped)
1 parent 964441c commit b868bc0

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { DEFAULT_PERSIST_OUTPUT_DIR } from '@code-pushup/models';
66
export const DEFAULT_CHROME_FLAGS = [...DEFAULT_FLAGS, '--headless'];
77

88
export const LIGHTHOUSE_PLUGIN_SLUG = 'lighthouse';
9+
export const LIGHTHOUSE_PLUGIN_TITLE = 'Lighthouse';
10+
911
export const LIGHTHOUSE_OUTPUT_PATH = path.join(
1012
DEFAULT_PERSIST_OUTPUT_DIR,
1113
LIGHTHOUSE_PLUGIN_SLUG,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { pluginMetaLogFormatter } from '@code-pushup/utils';
2+
import { LIGHTHOUSE_PLUGIN_TITLE } from './constants.js';
3+
4+
export const formatMetaLog = pluginMetaLogFormatter(LIGHTHOUSE_PLUGIN_TITLE);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { createRequire } from 'node:module';
22
import type { PluginConfig, PluginUrls } from '@code-pushup/models';
33
import { normalizeUrlInput } from '@code-pushup/utils';
4-
import { LIGHTHOUSE_PLUGIN_SLUG } from './constants.js';
4+
import {
5+
LIGHTHOUSE_PLUGIN_SLUG,
6+
LIGHTHOUSE_PLUGIN_TITLE,
7+
} from './constants.js';
58
import { normalizeFlags } from './normalize-flags.js';
69
import { processAuditsAndGroups } from './processing.js';
710
import { createRunnerFunction } from './runner/runner.js';
@@ -33,10 +36,10 @@ export function lighthousePlugin(
3336

3437
return {
3538
slug: LIGHTHOUSE_PLUGIN_SLUG,
39+
title: LIGHTHOUSE_PLUGIN_TITLE,
40+
icon: 'lighthouse',
3641
packageName: packageJson.name,
3742
version: packageJson.version,
38-
title: 'Lighthouse',
39-
icon: 'lighthouse',
4043
audits,
4144
groups,
4245
runner: createRunnerFunction(normalizedUrls, {
Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import type { Audit, Group } from '@code-pushup/models';
12
import {
23
addIndex,
34
expandAuditsForUrls,
45
expandGroupsForUrls,
6+
logger,
7+
objectFromEntries,
8+
objectToEntries,
9+
pluralizeToken,
510
shouldExpandForUrls,
611
} from '@code-pushup/utils';
12+
import { formatMetaLog } from './format.js';
713
import {
814
LIGHTHOUSE_GROUPS,
915
LIGHTHOUSE_NAVIGATION_AUDITS,
@@ -14,35 +20,82 @@ export function expandOptionsForUrls(
1420
options: FilterOptions,
1521
urlCount: number,
1622
): FilterOptions {
17-
return Object.fromEntries(
18-
Object.entries(options).map(([key, value]) => [
23+
return objectFromEntries(
24+
objectToEntries(options).map(([key, value = []]) => [
1925
key,
20-
Array.isArray(value)
21-
? value.flatMap(slug =>
22-
Array.from({ length: urlCount }, (_, i) => addIndex(slug, i)),
23-
)
24-
: value,
26+
value.flatMap(slug =>
27+
Array.from({ length: urlCount }, (_, i) => addIndex(slug, i)),
28+
),
2529
]),
2630
);
2731
}
2832

2933
export function processAuditsAndGroups(urls: string[], options: FilterOptions) {
34+
logTotal();
3035
if (!shouldExpandForUrls(urls.length)) {
31-
return markSkippedAuditsAndGroups(
36+
const marked = markSkippedAuditsAndGroups(
3237
LIGHTHOUSE_NAVIGATION_AUDITS,
3338
LIGHTHOUSE_GROUPS,
3439
options,
3540
);
41+
logSkipped(marked);
42+
return marked;
3643
}
3744
const expandedAudits = expandAuditsForUrls(
3845
LIGHTHOUSE_NAVIGATION_AUDITS,
3946
urls,
4047
);
4148
const expandedGroups = expandGroupsForUrls(LIGHTHOUSE_GROUPS, urls);
4249
const expandedOptions = expandOptionsForUrls(options, urls.length);
43-
return markSkippedAuditsAndGroups(
50+
logExpanded(expandedAudits, expandedGroups, urls);
51+
const marked = markSkippedAuditsAndGroups(
4452
expandedAudits,
4553
expandedGroups,
4654
expandedOptions,
4755
);
56+
logSkipped(marked);
57+
return marked;
58+
}
59+
60+
function logTotal(): void {
61+
logger.info(
62+
formatMetaLog(
63+
`Created ${pluralizeToken('group', LIGHTHOUSE_GROUPS.length)} and ${pluralizeToken('audit', LIGHTHOUSE_NAVIGATION_AUDITS.length)} from Lighthouse's categories and navigation audits`,
64+
),
65+
);
66+
}
67+
68+
function logExpanded(
69+
expandedAudits: Audit[],
70+
expandedGroups: Group[],
71+
urls: string[],
72+
): void {
73+
logger.info(
74+
formatMetaLog(
75+
`Expanded audits (${LIGHTHOUSE_NAVIGATION_AUDITS.length}${expandedAudits.length}) and groups (${LIGHTHOUSE_GROUPS.length}${expandedGroups.length}) for ${pluralizeToken('URL', urls.length)}`,
76+
),
77+
);
78+
}
79+
80+
function logSkipped(marked: { audits: Audit[]; groups: Group[] }): void {
81+
const { audits, groups } = marked;
82+
83+
const formattedCounts = [
84+
{ name: 'audit', items: audits },
85+
{ name: 'group', items: groups },
86+
]
87+
.map(({ name, items }) => {
88+
const skipped = items.filter(({ isSkipped }) => isSkipped);
89+
if (skipped.length === 0) {
90+
return '';
91+
}
92+
return `${skipped.length} out of ${pluralizeToken(name, items.length)}`;
93+
})
94+
.filter(Boolean)
95+
.join(' and ');
96+
97+
if (!formattedCounts) {
98+
return;
99+
}
100+
logger.info(formatMetaLog(`Skipping ${formattedCounts}`));
48101
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ export function lighthouseAuditRef(auditSlug: string, weight = 1): CategoryRef {
2525
};
2626
}
2727

28-
export class AuditsNotImplementedError extends Error {
29-
constructor(auditSlugs: string[]) {
30-
super(`audits: "${auditSlugs.join(', ')}" not implemented`);
28+
export class NotImplementedError extends Error {
29+
constructor(plural: string, slugs: string[]) {
30+
const formattedSlugs = slugs.map(slug => `"${slug}"`).join(', ');
31+
super(`${plural} not implemented: ${formattedSlugs}`);
3132
}
3233
}
3334

@@ -36,17 +37,11 @@ export function validateAudits(audits: Audit[], onlyAudits: string[]): boolean {
3637
slug => !audits.some(audit => audit.slug === slug),
3738
);
3839
if (missingAudtis.length > 0) {
39-
throw new AuditsNotImplementedError(missingAudtis);
40+
throw new NotImplementedError('Audits', missingAudtis);
4041
}
4142
return true;
4243
}
4344

44-
export class CategoriesNotImplementedError extends Error {
45-
constructor(categorySlugs: string[]) {
46-
super(`categories: "${categorySlugs.join(', ')}" not implemented`);
47-
}
48-
}
49-
5045
export function validateOnlyCategories(
5146
groups: Group[],
5247
onlyCategories: string | string[],
@@ -55,7 +50,7 @@ export function validateOnlyCategories(
5550
groups.every(group => group.slug !== slug),
5651
);
5752
if (missingCategories.length > 0) {
58-
throw new CategoriesNotImplementedError(missingCategories);
53+
throw new NotImplementedError('Categories', missingCategories);
5954
}
6055
return true;
6156
}

packages/plugin-lighthouse/src/lib/utils.unit.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
pluginConfigSchema,
88
} from '@code-pushup/models';
99
import {
10-
AuditsNotImplementedError,
11-
CategoriesNotImplementedError,
1210
lighthouseAuditRef,
1311
lighthouseGroupRef,
1412
markSkippedAuditsAndGroups,
@@ -70,7 +68,7 @@ describe('validateAudits', () => {
7068
],
7169
['missing-audit'],
7270
),
73-
).toThrow(new AuditsNotImplementedError(['missing-audit']));
71+
).toThrow('Audits not implemented: "missing-audit"');
7472
});
7573
});
7674

@@ -107,7 +105,7 @@ describe('validateOnlyCategories', () => {
107105
],
108106
'missing-category',
109107
),
110-
).toThrow(new CategoriesNotImplementedError(['missing-category']));
108+
).toThrow('Categories not implemented: "missing-category"');
111109
});
112110
});
113111

@@ -209,7 +207,7 @@ describe('markSkippedAuditsAndGroups to be used in plugin config', () => {
209207
] as Group[],
210208
{ skipAudits: ['missing-audit'] },
211209
),
212-
).toThrow(new AuditsNotImplementedError(['missing-audit']));
210+
).toThrow('Audits not implemented: "missing-audit"');
213211
});
214212

215213
it('should mark audits as not skipped when onlyAudits is set', () => {
@@ -258,7 +256,7 @@ describe('markSkippedAuditsAndGroups to be used in plugin config', () => {
258256
] as Group[],
259257
{ onlyAudits: ['missing-audit'] },
260258
),
261-
).toThrow(new AuditsNotImplementedError(['missing-audit']));
259+
).toThrow('Audits not implemented: "missing-audit"');
262260
});
263261

264262
it('should mark skipped audits and groups when onlyGroups is set', () => {
@@ -396,10 +394,10 @@ describe('markSkippedAuditsAndGroups to be used in plugin config', () => {
396394
onlyAudits: ['missing-audit'],
397395
},
398396
),
399-
).toThrow(new AuditsNotImplementedError(['missing-audit']));
397+
).toThrow('Audits not implemented: "missing-audit"');
400398
});
401399

402-
it('should throw if onlyGroups is set with a group slug that is not implemented', () => {
400+
it('should throw if onlyCategories is set with a group slug that is not implemented', () => {
403401
expect(() =>
404402
markSkippedAuditsAndGroups(
405403
[{ slug: 'speed-index' }] as Audit[],
@@ -413,6 +411,6 @@ describe('markSkippedAuditsAndGroups to be used in plugin config', () => {
413411
onlyCategories: ['missing-group'],
414412
},
415413
),
416-
).toThrow(new CategoriesNotImplementedError(['missing-group']));
414+
).toThrow('Categories not implemented: "missing-group"');
417415
});
418416
});

0 commit comments

Comments
 (0)