-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·104 lines (95 loc) · 3.26 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
const args = require('minimist')(process.argv.slice(2));
const fs = require('fs').promises;
const filePath = args._[0];
if (!filePath.endsWith('.json')) {
throw Error('Script can only process json files on LHR format');
}
// open json
const file = fs
.readFile(filePath, 'utf8')
.catch(error => {
// TODO: fail gracefully
throw error;
})
.then(text => {
const {categories} = JSON.parse(text);
const threshold = Math.min(1, parseFloat(args.threshold) || 0.8);
const fileOutputPath = args.o;
const results = {};
results.performance = categories.performance.score >= threshold;
results.accessibility = categories.accessibility.score >= threshold;
results.bestPractices = categories['best-practices'].score >= threshold;
results.seo = categories.seo.score >= threshold;
results.pwa = categories.pwa.score >= threshold;
// Count failures by summing falseful values
const failures = Object.values(results).reduce(
(acc, value) => (!value ? acc + 1 : acc),
0,
);
// get NOW date iso string
const date = new Date();
const dateString = date.toISOString();
// write report xml
let reportXml = `<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite id="0" name="lighthouse audit" errors="0" hostname="localhost" tests="5" failures="0" timestamp="${dateString}">
<properties>
</properties>
<testcase classname="Performance" name="score >= ${threshold}" time="0">${
results.performance
? ''
: `
<failure message="Performance score was less the threshold(${threshold})">Check full report for more info.</failure>`}
</testcase>
<testcase classname="Acessibility" name="score >= ${threshold}" time="0">${
results.accessibility
? ''
: `
<failure message="Acessibility score was less the threshold(${threshold})">Check full report for more info.</failure>`}
</testcase>
<testcase classname="Best Practices" name="score => ${threshold}" time="0">${
results.bestPractices
? ''
: ` <failure message="Best Practices score was less the threshold(${threshold})">Check full report for more info.</failure>`}
</testcase>
<testcase classname="SEO" name="score => ${threshold}" time="0">${
results.seo
? ''
: `
<failure message="SEO score was less the threshold(${threshold})">Check full report for more info.</failure>`}
</testcase>
<testcase classname="PWA" name="score >= ${threshold}" time="0">${
results.pwa
? ''
: `
<failure message="PWA score was less the threshold(${threshold})">Check full report for more info.</failure>`}
</testcase>
<system-out/>
<system-err/>
</testsuite>
</testsuites>`;
// write file on disc
if (fileOutputPath) {
fs.writeFile(fileOutputPath, reportXml)
.catch(error => {
// TODO: fail gracefully
throw error;
})
.then(() => {
// My job here is done!
if (failures) {
process.exit(1);
} else {
process.exit(0);
}
});
} else {
console.log(reportXml);
if (failures) {
process.exit(1);
} else {
process.exit(0);
}
}
});