-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathformatLighthouseReport.js
88 lines (79 loc) · 2.35 KB
/
formatLighthouseReport.js
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
82
83
84
85
86
87
88
// @ts-check
/**
* @typedef {Record<'performance' | 'accessibility' | 'best-practices' | 'seo',
* number>} LighthouseSummary
*/
/** @type {Record<keyof LighthouseSummary, string>} */
const summaryKeys = {
performance: "Performance",
accessibility: "Accessibility",
"best-practices": "Best Practices",
seo: "SEO",
};
/** @param {number} rawScore */
const scoreEntry = (rawScore) => {
const score = Math.round(rawScore * 100);
const scoreIcon = score >= 90 ? "🟢" : score >= 50 ? "🟡" : "🔴";
return `${scoreIcon} ${score}`;
};
/**
* @param {string} url
* @returns {module:url.URL}
*/
function createURL(url) {
try {
return new URL(url);
} catch (e) {
throw new Error(`Can't create URL for string=${url}`, { cause: e });
}
}
/**
* @param {Object} param0
* @param {string} param0.url
* @param {LighthouseSummary} param0.summary
* @param {string} param0.reportUrl
*/
const createMarkdownTableRow = ({ url, summary, reportUrl }) =>
[
`| [${createURL(url).pathname}](${url})`,
.../** @type {(keyof LighthouseSummary)[]} */ (
Object.keys(summaryKeys)
).map((k) => scoreEntry(summary[k])),
`[📄](${reportUrl}) |`,
].join(" | ");
const createMarkdownTableHeader = () => [
["| URL 🌐", ...Object.values(summaryKeys), "📊 |"].join(" | "),
["|---", ...Array(Object.keys(summaryKeys).length).fill("---"), "---|"].join(
"|",
),
];
/**
* @param {Object} param0
* @param {Record<string, string>} param0.links
* @param {{url: string, summary: LighthouseSummary}[]} param0.results
*/
const createLighthouseReport = ({ results, links }) => {
const tableHeader = createMarkdownTableHeader();
const tableBody = results.map((result) => {
const testUrl = /** @type {string} */ (
Object.keys(links).find((key) => key === result.url)
);
const reportPublicUrl = /** @type {string} */ (links[testUrl]);
return createMarkdownTableRow({
url: testUrl,
summary: result.summary,
reportUrl: reportPublicUrl,
});
});
const comment = [
"### ⚡️ Lighthouse Report for the Deploy Preview of this PR 🚀",
"",
"🔗 Site: [CodeHarborHub](https://github.com/CodeHarborHub/codeharborhub.github.io) | [Live Site](https://codeharborhub.github.io/)",
"",
...tableHeader,
...tableBody,
"",
];
return comment.join("\n");
};
export default createLighthouseReport;