|
1 | 1 | import { bold } from 'ansis';
|
2 |
| -import type { Config } from 'lighthouse'; |
| 2 | +import type { Config, FormattedIcu } from 'lighthouse'; |
3 | 3 | import log from 'lighthouse-logger';
|
4 | 4 | import desktopConfig from 'lighthouse/core/config/desktop-config.js';
|
5 | 5 | import experimentalConfig from 'lighthouse/core/config/experimental-config.js';
|
6 | 6 | import perfConfig from 'lighthouse/core/config/perf-config.js';
|
| 7 | +import Details from 'lighthouse/types/lhr/audit-details'; |
7 | 8 | import { Result } from 'lighthouse/types/lhr/audit-result';
|
8 | 9 | import { AuditOutput, AuditOutputs } from '@code-pushup/models';
|
9 |
| -import { importModule, readJsonFile, ui } from '@code-pushup/utils'; |
| 10 | +import { |
| 11 | + formatReportScore, |
| 12 | + importModule, |
| 13 | + readJsonFile, |
| 14 | + ui, |
| 15 | +} from '@code-pushup/utils'; |
10 | 16 | import type { LighthouseOptions } from '../types';
|
11 | 17 | import { logUnsupportedDetails, toAuditDetails } from './details/details';
|
12 | 18 | import { LighthouseCliFlags } from './types';
|
@@ -38,43 +44,58 @@ export class LighthouseAuditParsingError extends Error {
|
38 | 44 | }
|
39 | 45 | }
|
40 | 46 |
|
| 47 | +function formatBaseAuditOutput(lhrAudit: Result): AuditOutput { |
| 48 | + const { |
| 49 | + id: slug, |
| 50 | + score, |
| 51 | + numericValue, |
| 52 | + displayValue, |
| 53 | + scoreDisplayMode, |
| 54 | + } = lhrAudit; |
| 55 | + return { |
| 56 | + slug, |
| 57 | + score: score ?? 1, |
| 58 | + value: numericValue ?? score ?? 0, |
| 59 | + displayValue: |
| 60 | + displayValue ?? |
| 61 | + (scoreDisplayMode === 'binary' |
| 62 | + ? score === 1 |
| 63 | + ? 'passed' |
| 64 | + : 'failed' |
| 65 | + : score |
| 66 | + ? `${formatReportScore(score)}%` |
| 67 | + : undefined), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function processAuditDetails( |
| 72 | + auditOutput: AuditOutput, |
| 73 | + details: FormattedIcu<Details>, |
| 74 | +): AuditOutput { |
| 75 | + try { |
| 76 | + const parsedDetails = toAuditDetails(details); |
| 77 | + return Object.keys(parsedDetails).length > 0 |
| 78 | + ? { ...auditOutput, details: parsedDetails } |
| 79 | + : auditOutput; |
| 80 | + } catch (error) { |
| 81 | + throw new LighthouseAuditParsingError(auditOutput.slug, error as Error); |
| 82 | + } |
| 83 | +} |
| 84 | + |
41 | 85 | export function toAuditOutputs(
|
42 | 86 | lhrAudits: Result[],
|
43 | 87 | { verbose = false }: { verbose?: boolean } = {},
|
44 | 88 | ): AuditOutputs {
|
45 | 89 | if (verbose) {
|
46 | 90 | logUnsupportedDetails(lhrAudits);
|
47 | 91 | }
|
| 92 | + return lhrAudits.map(audit => { |
| 93 | + const auditOutput = formatBaseAuditOutput(audit); |
48 | 94 |
|
49 |
| - return lhrAudits.map( |
50 |
| - ({ |
51 |
| - id: slug, |
52 |
| - score, |
53 |
| - numericValue: value = 0, // not every audit has a numericValue |
54 |
| - details, |
55 |
| - displayValue, |
56 |
| - }: Result) => { |
57 |
| - const auditOutput: AuditOutput = { |
58 |
| - slug, |
59 |
| - score: score ?? 1, // score can be null |
60 |
| - value, |
61 |
| - displayValue, |
62 |
| - }; |
63 |
| - |
64 |
| - if (details != null) { |
65 |
| - try { |
66 |
| - const parsedDetails = toAuditDetails(details); |
67 |
| - return Object.keys(parsedDetails).length > 0 |
68 |
| - ? { ...auditOutput, details: parsedDetails } |
69 |
| - : auditOutput; |
70 |
| - } catch (error) { |
71 |
| - throw new LighthouseAuditParsingError(slug, error as Error); |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - return auditOutput; |
76 |
| - }, |
77 |
| - ); |
| 95 | + return audit.details == null |
| 96 | + ? auditOutput |
| 97 | + : processAuditDetails(auditOutput, audit.details); |
| 98 | + }); |
78 | 99 | }
|
79 | 100 |
|
80 | 101 | export type LighthouseLogLevel =
|
|
0 commit comments