-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboard.js
71 lines (64 loc) · 2.05 KB
/
dashboard.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
'use strict';
const fs = require('fs');
const jp = require('jsonpath');
const table = require('table');
const pathToSummary = '/home/chrome/reports/';
class LighthousePuppeteerDashboard {
constructor(options = {}) {
this.defaultOptions = {
file: pathToSummary + '/summary.json',
rules: {
'Interactive value': "$.audits['consistently-interactive'].displayValue"
}
};
this.options = Object.assign({}, this.defaultOptions);
if (typeof (options.rules) !== 'undefined') {
this.options.rules = {};
for (let name in options.rules) {
let json = JSON.parse(options.rules[name]);
for (let key in json) {
this.options.rules[key] = json[key];
}
}
}
if (typeof (options.file) !== 'undefined') {
this.options.file = options.file;
}
this.json = JSON.parse(fs.readFileSync(this.options.file));
}
getReport() {
let reports = [];
this.json.forEach((el) => {
let report = {
url: el.url,
values: {},
};
let file = JSON.parse(fs.readFileSync(pathToSummary + '/' + el.file));
for (let name in this.options.rules) {
report.values[name] = jp.query(file, this.options.rules[name])[0];
}
reports.push(report);
});
return reports;
}
reportToTable(report) {
let array = [];
let line1 = [''];
for (let name in this.options.rules) {
line1.push(name);
}
array.push(line1);
report.forEach((entity) => {
let line = [entity.url];
for (let name in entity.values) {
line.push(entity.values[name]);
}
array.push(line);
});
return array;
}
getTable() {
return table.table(this.reportToTable(this.getReport()));
}
}
module.exports = LighthousePuppeteerDashboard;