-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnormalize-flags.ts
More file actions
81 lines (73 loc) · 3.28 KB
/
Copy pathnormalize-flags.ts
File metadata and controls
81 lines (73 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import ansis from 'ansis';
import { logger } from '@code-pushup/utils';
import { DEFAULT_CLI_FLAGS } from './runner/constants.js';
import type { LighthouseCliFlags } from './runner/types.js';
import type { LighthouseOptions } from './types.js';
const { onlyCategories, ...originalDefaultCliFlags } = DEFAULT_CLI_FLAGS;
export const DEFAULT_LIGHTHOUSE_OPTIONS = {
...originalDefaultCliFlags,
onlyGroups: onlyCategories,
} satisfies LighthouseOptions;
// NOTE:
// This is an intermediate variable to get `UnsupportedCliFlags`. For unknown reasons `typescript@5.3.3` doesn't work otherwise.
const lighthouseUnsupportedCliFlags = [
'precomputedLanternDataPath', // Path to the file where precomputed lantern data should be read from.
'chromeIgnoreDefaultFlags', // ignore default flags from Lighthouse CLI
// No error reporting implemented as in the source Sentry was involved
// See: https://github.com/GoogleChrome/lighthouse/blob/d8ccf70692216b7fa047a4eaa2d1277b0b7fe947/cli/bin.js#L124
'enableErrorReporting', // enable error reporting
// lighthouse CLI specific debug logs
'list-all-audits', // Prints a list of all available audits and exits.
'list-locales', // Prints a list of all supported locales and exits.
'list-trace-categories', // Prints a list of all required trace categories and exits.
] as const;
type UnsupportedCliFlags = (typeof lighthouseUnsupportedCliFlags)[number];
const LIGHTHOUSE_UNSUPPORTED_CLI_FLAGS = new Set(lighthouseUnsupportedCliFlags);
const REFINED_STRING_OR_STRING_ARRAY = new Set([
'onlyAudits',
'onlyCategories',
'skipAudits',
'budgets',
'chromeFlags',
]);
export function normalizeFlags(flags?: LighthouseOptions): LighthouseCliFlags {
const prefilledFlags = { ...DEFAULT_LIGHTHOUSE_OPTIONS, ...flags };
logUnsupportedFlagsInUse(prefilledFlags);
return Object.fromEntries(
Object.entries(prefilledFlags)
.filter(
([flagName]) =>
!LIGHTHOUSE_UNSUPPORTED_CLI_FLAGS.has(
flagName as UnsupportedCliFlags,
),
)
// in code-pushup lighthouse categories are mapped as groups, therefor we had to rename "onlyCategories" to "onlyGroups" for the user of the plugin as it was confusing
.map(([key, v]) => [key === 'onlyGroups' ? 'onlyCategories' : key, v])
// onlyAudits and onlyCategories cannot be empty arrays, otherwise skipAudits is ignored by lighthouse
.filter(([_, v]) => !(Array.isArray(v) && v.length === 0))
// undefined | string | string[] => string[] (empty for undefined)
.map(([key, v]) => {
if (!REFINED_STRING_OR_STRING_ARRAY.has(key as never)) {
return [key, v];
}
return [key, Array.isArray(v) ? v : v == null ? [] : [v]];
}),
) as LighthouseCliFlags;
}
export function logUnsupportedFlagsInUse(
flags: LighthouseOptions,
displayCount = 3,
) {
const unsupportedFlagsInUse = Object.keys(flags).filter(flag =>
LIGHTHOUSE_UNSUPPORTED_CLI_FLAGS.has(flag as UnsupportedCliFlags),
);
if (unsupportedFlagsInUse.length > 0) {
const postFix = (count: number) =>
count > displayCount ? ` and ${count - displayCount} more.` : '';
logger.warn(
`Used unsupported flags: ${ansis.bold(
unsupportedFlagsInUse.slice(0, displayCount).join(', '),
)}${postFix(unsupportedFlagsInUse.length)}`,
);
}
}