-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (110 loc) · 2.99 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
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
import findConfig from 'find-config'
import { mapValues, keyBy, camelCase } from 'lodash'
import chalk from 'chalk'
import lighthouse from 'lighthouse'
import * as chromeLauncher from 'chrome-launcher'
const log = str => console.log(chalk.green(str))
const err = error => {
if (error.length > 0) {
error.forEach(e => console.log(chalk.bold.red(e)))
} else {
console.log(chalk.bold.red(error))
}
process.exit(1)
}
const launchChromeAndRunLighthouse = async url => {
try {
log(`fetching ${url}`)
const chromeFlags = ['--headless', '--no-sandbox', '--disable-gpu']
const chrome = await chromeLauncher.launch({
chromeFlags,
connectionPollInterval: 10000
})
log('running lighthouse')
const results = await lighthouse(
url,
{ chromeFlags, port: chrome.port },
null
)
delete results.artifacts
await chrome.kill()
return results
} catch (e) {
throw e
}
}
const parseReport = async (scores, thresholds, url) => {
const errors = []
const {
performance: performanceScore,
progressiveWebApp: progressiveScore,
accessibility: a11yScore,
bestPractices: bestPracticeScore,
seo: seoScore
} = scores
const {
performance = 0,
progressive = 0,
a11y = 0,
bestPractice = 0,
seo = 0
} = thresholds
if (performance > performanceScore) {
errors.push(`${url} - Performance: ${performanceScore} < ${performance}`)
}
if (progressive > progressiveScore) {
errors.push(
`${url} - Progressive Web App: ${progressiveScore} < ${progressive}`
)
}
if (a11y > a11yScore) {
errors.push(`${url} - a11y: ${a11yScore} < ${a11y}`)
}
if (bestPractice > bestPracticeScore) {
errors.push(
`${url} - Best Practices: ${bestPracticeScore} < ${bestPractice}`
)
}
if (seo > seoScore) {
errors.push(`${url} - SEO: ${seoScore} < ${seo}`)
}
return errors
}
;(async () => {
let errors = []
const config =
JSON.parse(findConfig.read('.lighthouserc')) ||
findConfig.require('.lighthouserc.js')
if (!config) {
err('Could not find .lighthouserc or .lighthouserc.js config file')
}
try {
await Promise.all(
config.map(
({ url, thresholds }) =>
new Promise((resolve, reject) =>
launchChromeAndRunLighthouse(url)
.then(async ({ reportCategories }) => {
const normalizeKeys = keyBy(reportCategories, ({ name }) =>
camelCase(name)
)
const scores = mapValues(normalizeKeys, ({ score }) =>
parseFloat(score.toFixed(2))
)
log('parsing results')
const issues = await parseReport(scores, thresholds, url)
errors = errors.concat(issues)
resolve()
})
.catch(e => {
reject(e)
})
)
)
)
} catch (e) {
throw e
}
if (errors.length) err(errors)
log('Passed thresholds')
})()