forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.mjs
More file actions
50 lines (41 loc) · 1.87 KB
/
index.mjs
File metadata and controls
50 lines (41 loc) · 1.87 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
'use strict';
const stoplight = res => (res >= 90 ? '🟢' : res >= 75 ? '🟠' : '🔴');
const normalizeScore = res => Math.round(res * 100);
const formatScore = res => {
const normalizedScore = normalizeScore(res);
return `${stoplight(normalizedScore)} ${normalizedScore}`;
};
/**
* `core` is in scope from https://github.com/actions/github-script
*/
export const formatLighthouseResults = ({ core }) => {
// this will be the shape of https://github.com/treosh/lighthouse-ci-action#manifest
const results = JSON.parse(process.env.LIGHTHOUSE_RESULT);
// this will be the shape of https://github.com/treosh/lighthouse-ci-action#links
const links = JSON.parse(process.env.LIGHTHOUSE_LINKS);
// start creating our markdown table
const header = [
'Lighthouse Results',
'URL | Performance | Accessibility | Best Practices | SEO | Report',
'| - | - | - | - | - | - |',
];
// map over each url result, formatting and linking to the output
const urlResults = results.map(({ url, summary }) => {
// make the tested link as a markdown link, without the long-generated host
const shortPreviewLink = `[${url.replace(
process.env.VERCEL_PREVIEW_URL,
''
)}](${url})`;
// make each formatted score from our lighthouse properties
const performanceScore = formatScore(summary.performance);
const accessibilityScore = formatScore(summary.accessibility);
const bestPracticesScore = formatScore(summary['best-practices']);
const seoScore = formatScore(summary.seo);
// create the markdown table row
return `${shortPreviewLink} | ${performanceScore} | ${accessibilityScore} | ${bestPracticesScore} | ${seoScore} | [🔗](${links[url]})`;
});
// join the header and the rows together
const finalResults = [...header, ...urlResults].join('\n');
// return our output to the github action
core.setOutput('comment', finalResults);
};