-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.ts
43 lines (33 loc) · 1004 Bytes
/
format.ts
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
import {green, red, yellow, type Chalk} from 'chalk'
import type {Report} from './report'
const METRIC_NAME_LENGTH = 18
const CRITICAL_TRESHOLD = 0.5
const WARN_TRESHOLD = 0.9
const TRESHOLDS: [number, Chalk][] = [
[CRITICAL_TRESHOLD, red],
[WARN_TRESHOLD, yellow],
[Infinity, green]
]
function formatMetricValue(value: number) {
const [, color]: any = TRESHOLDS.find(([treshold]) => value < treshold)
return color(value.toFixed(2))
}
export function formatReport(report: Report) {
let output = '\nLighthouse report\n'
Object.entries(report).forEach(([url, metrics]) => {
output += '\n'
output += `URL: ${url}`
output += '\n'
output += `Number of runs: ${metrics.performance.length}`
output += '\n\n'
Object.entries(metrics).forEach(([metric, values]) => {
const row = [
metric.padEnd(METRIC_NAME_LENGTH, ' '),
...values.map(formatMetricValue)
]
output += row.join(' ')
output += '\n'
})
})
return output
}