The Node.js® Website
1'use strict';
2
3const stoplight = res => (res >= 90 ? '🟢' : res >= 75 ? '🟠' : '🔴');
4const normalizeScore = res => Math.round(res * 100);
5const formatScore = res => {
6 const normalizedScore = normalizeScore(res);
7 return `${stoplight(normalizedScore)} ${normalizedScore}`;
8};
9
10/**
11 * `core` is in scope from https://github.com/actions/github-script
12 */
13export const formatLighthouseResults = ({ core }) => {
14 // this will be the shape of https://github.com/treosh/lighthouse-ci-action#manifest
15 const results = JSON.parse(process.env.LIGHTHOUSE_RESULT);
16
17 // this will be the shape of https://github.com/treosh/lighthouse-ci-action#links
18 const links = JSON.parse(process.env.LIGHTHOUSE_LINKS);
19
20 // start creating our markdown table
21 const header = [
22 'Lighthouse Results',
23 'URL | Performance | Accessibility | Best Practices | SEO | Report',
24 '| - | - | - | - | - | - |',
25 ];
26
27 // map over each url result, formatting and linking to the output
28 const urlResults = results.map(({ url, summary }) => {
29 // make the tested link as a markdown link, without the long-generated host
30 const shortPreviewLink = `[${url.replace(
31 process.env.VERCEL_PREVIEW_URL,
32 ''
33 )}](${url})`;
34
35 // make each formatted score from our lighthouse properties
36 const performanceScore = formatScore(summary.performance);
37 const accessibilityScore = formatScore(summary.accessibility);
38 const bestPracticesScore = formatScore(summary['best-practices']);
39 const seoScore = formatScore(summary.seo);
40
41 // create the markdown table row
42 return `${shortPreviewLink} | ${performanceScore} | ${accessibilityScore} | ${bestPracticesScore} | ${seoScore} | [🔗](${links[url]})`;
43 });
44
45 // join the header and the rows together
46 const finalResults = [...header, ...urlResults].join('\n');
47
48 // return our output to the github action
49 core.setOutput('comment', finalResults);
50};