-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.js
202 lines (161 loc) · 6.02 KB
/
tool.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const lighthouse = require('lighthouse');
const { URL } = require('url');
const { priorities } = require('@koalati/result-builder');
class Tool {
constructor({ page }) {
this.page = page;
}
async run() {
const lighthouseResults = await this.runLighthouse();
this._results = this.standardizeWeights(lighthouseResults);
this._results = this.formatLightouseResults(lighthouseResults);
this.sortResults();
}
get results() {
return this._results;
}
async cleanup() {
}
standardizeWeights(lighthouseResults) {
const totalWeight = lighthouseResults.reduce((total, result) => total + result.weight);
for (let i = 0; i < lighthouseResults.length; i++) {
lighthouseResults[i].weight = lighthouseResults[i].weight / totalWeight;
}
return lighthouseResults;
}
formatLightouseResults(lighthouseResults) {
const results = [];
for (const audit of lighthouseResults) {
const result = {
'uniqueName': audit.id,
'title': audit.title,
'description': audit.description,
'weight': audit.weight,
'score': audit.score
};
if (audit.score < 1) {
result.recommendations = [
[
audit.title,
{
"_message": audit.id,
"_group": audit.group,
},
this._extractResultPriority(audit)
]
];
}
try {
if (audit.details.items.length) {
const table = [[]];
for (const column of audit.details.headings) {
table[0].push(column.text || (column.label || ''));
}
for (const rawItem of audit.details.items) {
const itemRow = [];
for (const column of audit.details.headings) {
const detailElements = this.formatSingleDetail(column, rawItem);
if (detailElements.length > 1 && audit.details.headings.length == 1) {
for (const element of detailElements) {
itemRow.push(element);
}
// Add table heading if it hasn't been added yet
if (table[0].length == 1) {
table[0].push("Explanation");
}
} else {
itemRow.push(detailElements.join('\n'));
}
}
table.push(itemRow);
}
result.table = table;
}
} catch (err) { }
results.push(result);
}
return results;
}
/**
*
* @param {object} column
* @param {object} item
* @returns {string[]}
*/
formatSingleDetail(column, item) {
switch (column.valueType) {
case 'thumbnail':
return '[![](%s)](%s)'.replace(/%s/g, item[column.key]);
case 'url':
return '[%s](%s)'.replace(/%s/g, item[column.key]);
}
let contentLines = [];
if (typeof item[column.key] == 'object') {
if (typeof item[column.key].snippet != 'undefined') {
contentLines.push('```\n' + item[column.key].snippet + '\n```');
} else if (typeof item[column.key].selector != 'undefined') {
contentLines.push('```\n' + item[column.key].selector + '\n```');
}
if (typeof item[column.key].explanation != 'undefined') {
contentLines.push(this.formatExplanation(item[column.key].explanation));
}
}
return contentLines;
}
/**
* @param {string} explanation
*/
formatExplanation(explanation) {
return explanation.replace(/^(\s\s){1,2}/gm, '- ');
}
formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
_extractResultPriority(audit) {
if (typeof audit.details != 'undefined' &&
typeof audit.details.debugData != 'undefined' &&
"serious" == audit.details.debugData.impact) {
return priorities.ISSUE;
}
if (audit.weight >= 5) {
return priorities.ISSUE;
} else if (audit.weight >= 2) {
return priorities.ESSENTIAL;
}
return priorities.OPTIMIZATION;
}
sortResults() {
this._results.sort((a, b) => {
if (a.weight == b.weight) {
return a.score > b.score ? 1 : -1;
}
return a.weight > b.weight ? -1 : 1;
});
}
async runLighthouse() {
const { lhr } = await lighthouse(this.page.url(), {
port: (new URL(this.page.browser().wsEndpoint())).port,
output: 'json',
onlyCategories: ['accessibility'],
});
const audits = [];
try {
for (const auditReference of lhr.categories.accessibility.auditRefs) {
try {
const auditData = lhr.audits[auditReference.id];
if (!auditData || auditData.score === null) {
continue;
}
const auditReferenceCopy = JSON.parse(JSON.stringify(auditReference));
audits.push(Object.assign(auditReferenceCopy, auditData));
} catch (err) { }
}
} catch (err) { }
return audits;
}
}
module.exports = Tool;