-
Notifications
You must be signed in to change notification settings - Fork 2
/
field-report
167 lines (140 loc) · 4.11 KB
/
field-report
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
#!/usr/bin/env node
const path = require("path");
const {
promises: { readdir, readFile },
} = require("fs");
const noFormat = process.env.NO_FORMAT;
const noColor = process.env.NO_COLOR;
const padValue = (key, value = key) => {
if (noFormat) {
return value;
}
const widths = {
TBT: "99,999 ms".length,
CLS: "0.999".length,
Timestamp: "2021-03-19 21:48:49".length,
score: "Score".length,
"total-blocking-time": "99,999 ms".length,
default: "99.9 s".length,
};
const width = widths[key] || widths.default;
const isHeading = key === value;
return isHeading ? value.padEnd(width, " ") : value.padStart(width, " ");
};
const formatMetric = ({ id, score, displayValue, numericValue }) => {
const colorReset = () => (noColor ? "" : "\x1b[0m");
const scoreColor = (score) => {
if (noColor) {
return "";
}
if (score < 0.5) {
return "\x1b[31m"; // red
}
if (score < 0.9) {
return "\x1b[33m"; // yellow
}
return "\x1b[32m"; // green
};
const metricValue = noFormat ? numericValue : padValue(id, displayValue);
return `${scoreColor(score)}${metricValue}${colorReset()}`;
};
const formatTimeStamp = (isoDateString) =>
noFormat
? isoDateString
: isoDateString.replace("T", " ").replace(/\..*/, "");
const formatNameOrPath = ({
name,
requestedUrl,
requestedOrigin,
requestedPath,
finalUrl,
finalOrigin,
finalPath,
}) => {
const requestedNameOrPath = name ? name : requestedPath;
if (requestedUrl === finalUrl) {
return requestedNameOrPath;
} else {
if (requestedOrigin === finalOrigin) {
return `${requestedNameOrPath} -> ${finalPath}`;
} else {
return `${requestedNameOrPath} -> ${finalUrl}`;
}
}
};
async function* getFiles(dir) {
const dirents = await readdir(path.resolve(__dirname, dir), {
withFileTypes: true,
});
for (const dirent of dirents) {
const res = path.join(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(res);
} else {
yield res;
}
}
}
const origin = (url) => new URL(url).origin;
const stripOrigin = (url) => url.replace(origin(url), "");
const getPaths = ({ requestedUrl, finalUrl }) => ({
requestedOrigin: origin(requestedUrl),
requestedPath: stripOrigin(requestedUrl),
finalOrigin: origin(requestedUrl),
finalPath: stripOrigin(finalUrl),
});
const AUDITS = {
FCP: "first-contentful-paint",
LCP: "largest-contentful-paint",
CLS: "cumulative-layout-shift",
TBT: "total-blocking-time",
};
const HEADINGS = ["Timestamp", ...Object.keys(AUDITS), "Score", "URL"];
const getScoreAsMetric = ({ categories: { performance } }) => ({
id: "score",
score: performance.score,
displayValue: Math.floor(performance.score * 100).toString(),
numericValue: Math.floor(performance.score * 100),
});
(async () => {
const [, , snapshotsPath = "", filter] = process.argv;
console.log(HEADINGS.map((key) => padValue(key)).join("\t"));
for await (const resultPath of getFiles(snapshotsPath)) {
if (!resultPath.endsWith(".json")) {
continue;
}
try {
const absoluteResultPath = path.resolve(__dirname, resultPath);
const psiResult = JSON.parse(await readFile(absoluteResultPath, "utf-8"));
const { name, error, lighthouseResult } = psiResult;
if (error) {
console.error(`${resultPath}:`, error.code || error.message);
continue;
}
const paths = getPaths(lighthouseResult);
if (
filter &&
filter !== name &&
filter !== lighthouseResult.requestedUrl &&
filter !== paths.requestedPath
) {
continue;
}
const audits = Object.values(AUDITS).map(
(key) => lighthouseResult.audits[key]
);
const score = getScoreAsMetric(lighthouseResult);
const timeStamp = psiResult.analysisUTCTimestamp;
console.log(
[
formatTimeStamp(timeStamp),
...[...audits, score].map(formatMetric),
formatNameOrPath({ name, ...lighthouseResult, ...paths }),
].join("\t")
);
} catch (e) {
console.error(`${resultPath}:`, e.message);
continue;
}
}
})();