-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlocalLighthouse.js
276 lines (255 loc) · 6.35 KB
/
localLighthouse.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import lighthousePersist from '@foo-software/lighthouse-persist';
import fs from 'fs';
import get from 'lodash.get';
import { NAME } from './constants';
import { delay } from './helpers/utils';
import writeResults from './helpers/writeResults';
import { desktop, mobile, throttling } from './lighthouseConfig';
import options from './lighthouseOptions';
const defaultLighthouseConfigs = {
desktop,
mobile,
};
const getScoresFromFloat = (scores) =>
Object.keys(scores).reduce(
(accumulator, current) => ({
...accumulator,
...(typeof scores[current] === 'number' && {
[current]: Math.floor(scores[current] * 100),
}),
}),
{},
);
export const localLighthouse = async ({
awsAccessKeyId,
awsBucket,
awsRegion,
awsSecretAccessKey,
emulatedFormFactor,
extraHeaders,
locale,
maxWaitForLoad,
outputDirectory,
overrides,
throttling: throttlingParam,
throttlingMethod,
url,
}) => {
// if desktop device, and no throttling param specified, use the
// appropriate throttling
const throttlingOverride =
emulatedFormFactor === 'desktop' && !throttlingParam
? 'desktopDense4G'
: throttlingParam;
const lighthouseDefaultConfig = defaultLighthouseConfigs[emulatedFormFactor];
// the default config combined with overriding query params
const fullConfig = {
...lighthouseDefaultConfig,
settings: {
...lighthouseDefaultConfig.settings,
...(!maxWaitForLoad
? {}
: {
maxWaitForLoad,
}),
...(!throttlingMethod
? {}
: {
throttlingMethod,
}),
...(!throttlingOverride || !throttling[throttlingOverride]
? {}
: {
throttling: throttling[throttlingOverride],
}),
...(!emulatedFormFactor
? {}
: {
emulatedFormFactor,
}),
...(!extraHeaders
? {}
: {
extraHeaders,
}),
// if we wanted translations (holy!)
// locale: 'ja',
...(!locale
? {}
: {
locale,
}),
},
...(!overrides || !overrides.config
? {}
: {
...overrides.config,
}),
};
const { localReport, report, result } = await lighthousePersist({
awsAccessKeyId,
awsBucket,
awsRegion,
awsSecretAccessKey,
config: fullConfig,
options: {
...options,
...(!overrides || !overrides.options
? {}
: {
...overrides.options,
}),
},
outputDirectory,
url,
});
const scores = getScoresFromFloat({
accessibility: get(result, 'categories.accessibility.score'),
bestPractices: get(result, 'categories["best-practices"].score'),
performance: get(result, 'categories.performance.score'),
progressiveWebApp: get(result, 'categories.pwa.score'),
seo: get(result, 'categories.seo.score'),
});
return {
url,
localReport,
report,
emulatedFormFactor,
runtimeError: get(result, 'runtimeError.message'),
scores,
};
};
export const getLocalLighthouseResultsWithRetries = async ({
auditConfig,
localLighthousePromise = localLighthouse,
maxRetries = 0,
retries = 0,
verbose = false,
}) => {
let lighthouseAuditResult;
try {
lighthouseAuditResult = await localLighthousePromise(auditConfig);
if (lighthouseAuditResult.runtimeError) {
throw Error(lighthouseAuditResult.runtimeError);
}
if (maxRetries && retries) {
console.log(`Succeeded on retry #${retries}.`);
}
return lighthouseAuditResult;
} catch (error) {
if (retries >= maxRetries) {
if (maxRetries) {
console.log(`Max retries of ${maxRetries} exhausted... failing now.`);
}
throw error;
} else {
if (verbose) {
console.log(
`${NAME}: Error below caught on retry ${retries} of ${maxRetries}.`,
error,
'Trying again...',
);
} else {
console.log(
`Error caught on retry ${retries} of ${maxRetries}.`,
'Trying again...',
);
}
return getLocalLighthouseResultsWithRetries({
auditConfig,
localLighthousePromise,
maxRetries,
retries: retries + 1,
});
}
}
};
export default async ({
awsAccessKeyId,
awsBucket,
awsRegion,
awsSecretAccessKey,
emulatedFormFactor,
extraHeaders,
locale,
overridesJsonFile,
maxRetries = 0,
maxWaitForLoad,
outputDirectory,
throttling,
throttlingMethod,
urls,
verbose,
}) => {
// check for overrides config or options
let overrides;
if (overridesJsonFile) {
const overridesJsonString = fs.readFileSync(overridesJsonFile).toString();
const overridesJson = JSON.parse(overridesJsonString);
if (overridesJson.config || overridesJson.options) {
overrides = overridesJson;
}
}
// a list of audit configurations
const auditConfigs = [];
// collect all audit configs
for (const url of urls) {
const options = {
awsAccessKeyId,
awsBucket,
awsRegion,
awsSecretAccessKey,
emulatedFormFactor,
extraHeaders,
locale,
maxWaitForLoad,
outputDirectory,
overrides,
throttling,
throttlingMethod,
url,
verbose,
};
if (options.emulatedFormFactor !== 'all') {
auditConfigs.push(options);
} else {
// establish two audits for all device types
auditConfigs.push({
...options,
emulatedFormFactor: 'desktop',
});
auditConfigs.push({
...options,
emulatedFormFactor: 'mobile',
});
}
}
const auditResults = [];
let index = 1;
// for each audit config, run the audit
for (const auditConfig of auditConfigs) {
if (verbose) {
console.log(
`${NAME}: Auditing ${auditConfig.emulatedFormFactor} (${index}/${auditConfigs.length}): ${auditConfig.url}`,
);
}
// 1 second delay to allow time of previous Chrome process end
await delay(1000);
const lighthouseAuditResult = await getLocalLighthouseResultsWithRetries({
auditConfig,
maxRetries,
retries: 0,
verbose,
});
auditResults.push(lighthouseAuditResult);
index++;
}
// if outputDirectory is specified write the results to disk
if (outputDirectory) {
writeResults({
outputDirectory,
results: auditResults,
});
}
return auditResults;
};