-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
93 lines (84 loc) · 2.83 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
const pMap = require('p-map');
const getCombinations = require('./lib/getCombinations');
const lightHouseApi = require('./lib/lightHouseApi');
const defaultLogger = require('./lib/logger');
const sendEmail = require('./lib/sendEmail');
const sendMetrics = require('./lib/sendMetrics');
/**
* @see readme for parameters
* @returns {object}
*/
module.exports = async function run({
categories = ['PERFORMANCE', 'SEO', 'ACCESSIBILITY', 'BEST_PRACTICES'],
strategies = ['MOBILE', 'DESKTOP'],
email = {},
lightHouseApiKey,
pages = {},
statsdClient = {},
logger = defaultLogger
}) {
const strategiesCombinations = getCombinations({ strategies, categories, pages });
if (strategiesCombinations.flat().length === 0) {
logger.warn('No pages specified. Exit program.');
return;
}
logger.debug('Retrieve results from PageSpeed API.');
try {
const errors = [];
const results = await pMap(
strategiesCombinations,
async(strategyCombinations) => await pMap(
strategyCombinations,
async(combination) => {
try {
return await lightHouseApi(lightHouseApiKey, combination, { logger });
} catch (error) {
return undefined;
}
},
{ concurrency: 2 }
)
);
const dataFromApi = results.map((page) => page.filter(Boolean));
if (email.to) {
logger.debug('Send email to recipients.');
try {
const result = await sendEmail({ email, dataFromApi });
logger.info({
message: 'Email sent successfully',
response: JSON.stringify(result)
});
} catch (error) {
logger.error(error);
}
}
if (statsdClient.host) {
logger.debug('Send metrics to statsd.');
try {
const { length } = await sendMetrics.send({ statsdClient, dataFromApi });
logger.info({
message: `${length} metrics were sent successfully.`
});
} catch (error) {
logger.error(error);
}
}
errors.length && logger.error({
message: `Failed with ${errors.length} errors`,
description: JSON.stringify(
errors.map(
(error) => ({
message: error.message,
stack: error.stack,
name: error.constructor?.name,
...error
})
)
)
});
return dataFromApi;
} catch (error) {
logger.error(error);
return null;
}
};