-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-lighthouse-badges.js
90 lines (78 loc) · 3.11 KB
/
build-lighthouse-badges.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
#!/usr/bin/env node
const https = require('https');
const fs = require('fs-extra');
const glob = require("glob");
const lighthousePath = `src/main/resources/gh-pages/audit`;
const lighthouseAssetsPath = `${lighthousePath}/assets`;
const lighthouseCiPath = './.lighthouseci';
const createScoreList = report => {
const lhrScoreList = [];
Object.keys(report.categories).forEach(key => {
var category = report.categories[key];
lhrScoreList.push({'id': key, 'title': category.title, 'score': (category.score*100)});
});
return lhrScoreList;
}
const createReport = htmlReportPath => {
fs.ensureDir(lighthousePath);
fs.copySync( htmlReportPath , `./${lighthousePath}/index.html`);
}
const createBadges = resultList => {
fs.ensureDirSync(lighthouseAssetsPath);
resultList.forEach(r => {
const badgeColor = (r.score >= 0 && r.score <= 49) ? 'red' :
(r.score >= 50 && r.score <= 89) ? 'important' :
'success' ;
https.get(`https://img.shields.io/badge/${r.title}-${r.score}-${badgeColor}`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
fs.writeFileSync(`./${lighthouseAssetsPath}/${r.id}.svg`, data );
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
});
}
const logScoreList = scoreList => {
console.log('Finished generating report');
console.log('Results:');
scoreList.forEach(s => {
console.info(s.title, s.score);
});
}
const createGhPagesDir = () => {
const targetPath = 'target';
const ghPagesPath = `src/main/resources/gh-pages`;
const targetGhPagesPath = `${targetPath}/gh-pages`;
fs.ensureDir(ghPagesPath);
fs.ensureDir(targetPath);
fs.copy(`${targetPath}/generated-docs/`, `${targetGhPagesPath}/api-docs/`, err => {
if (err) return console.error(' \n Unable to copy generated-docs to : ',`${targetGhPagesPath}/api-docs/` , '\n', err);
console.log(`Copied generated-docs to ${targetGhPagesPath}/api-docs/ !`);
});
fs.copy(`./${ghPagesPath}/index.html`, `${targetGhPagesPath}/index.html`, err => {
if (err) return console.error(' \n Unable to copy src index file to : ',`${targetGhPagesPath}/index.html` , '\n', err);
console.log(`Copied index file to ${targetGhPagesPath}/index.html !`);
});
fs.copy(`./${ghPagesPath}/audit/`, `${targetGhPagesPath}/audit/`, err => {
if (err) return console.error(' \n Unable to copy src audit to : ',`${targetGhPagesPath}/audit/` , '\n', err);
console.log(`Copied audit to ${targetGhPagesPath}/audit/ !`);
});
};
if(fs.existsSync(lighthouseCiPath)) {
fs.ensureDir(`${lighthousePath}`);
const htmlReportPath = glob.sync(`${lighthouseCiPath}/lhr-*.html`, {})[0];
const jsonReportPath = glob.sync(`${lighthouseCiPath}/lhr-*.json`, {})[0];
const jsonReportRaw = fs.readFileSync(jsonReportPath);
const jsonReport = JSON.parse(jsonReportRaw);
const scoreList = createScoreList(jsonReport);
createReport(htmlReportPath);
createBadges(scoreList);
logScoreList(scoreList);
createGhPagesDir();
} else {
console.warn(`${lighthouseCiPath} does not exist. Please run 'npm run test:audit'`);
}