-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlighthouseReporter.js
71 lines (58 loc) · 2.07 KB
/
lighthouseReporter.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
const fs = require('fs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const fetch = require('node-fetch');
var xml2js = require('xml2js');
let reportObjectCondensed = {
testRunTime: new Date().toISOString(),
items: [],
}
let reportObjectFull = []
let urlList = [];
function condensedReport(lhr) {
let { performance, accessibility, seo, 'best-practices': bestPractices } = lhr.categories;
let temp = {
url: lhr.finalUrl,
performance: performance.score,
accessibility: accessibility.score,
bestPractices: bestPractices.score,
seo: seo.score,
}
reportObjectCondensed.items.push(temp)
}
async function fetchSitemap() {
const x = await fetch('https://lybekk.tech/sitemap.xml');
const sitemapResult = await x.text();
var parser = new xml2js.Parser({ async: true });
parser.parseStringPromise(sitemapResult).then(async function (result) {
for await (let x of result.urlset.url) {
urlList.push(x.loc[0])
}
})
.catch(function (err) {
console.log(err)
});
}
async function runLighthouse() {
const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
const options = { logLevel: 'info', output: 'html', port: chrome.port };
async function testURL(url) {
const runnerResult = await lighthouse(url, options);
const reportHtml = runnerResult.report;
let filename = url.replace(/[/\\?%*:|"<>]/g, '-');
fs.writeFileSync(`./reports/lhreport_${filename}.html`, reportHtml);
reportObjectFull.push(runnerResult.lhr);
condensedReport(runnerResult.lhr);
}
for await (let item of urlList) {
await testURL(item);
}
await chrome.kill();
};
async function procedure() {
await fetchSitemap();
await runLighthouse();
fs.writeFileSync('./reports/lhreport_full.json', JSON.stringify(reportObjectFull, null, 2));
fs.writeFileSync('./reports/lhreport_condensed.json', JSON.stringify(reportObjectCondensed, null, 2));
}
procedure()