-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathbuildSizeReport.js
executable file
·192 lines (166 loc) · 4.81 KB
/
buildSizeReport.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
#!/usr/bin/env node
/*
* Input: path to 2 build directories to compare.
* Output: markdown report of size changes.
*/
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
const glob = require("glob");
// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) {
return "0 B";
}
const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
/**
* The size, in bytes, of the given file after gzip.
*/
function computedFile(dir, filePath) {
const pathToFile = path.join(dir, filePath);
const str = fs.readFileSync(pathToFile);
return zlib.gzipSync(str).length;
}
/**
* Returns list of minified files in the given directory.
*/
async function minifiedFiles(dir) {
return await new Promise((res, rej) => {
glob(dir + "/**/*.min.{js,css}", {}, (err, files) => {
if (err) {
rej(err);
} else {
res(files.map((f) => f.replace(dir + "/", "")));
}
});
});
}
/**
* Returns object of changes between the given lists of strings.
*/
function itemChanges(baseList, newList) {
const baseSet = new Set(baseList);
const newSet = new Set(newList);
let added = [];
for (const str of newList) {
if (!baseSet.has(str)) {
added.push(str);
}
}
let changed = [];
let removed = [];
for (const str of baseList) {
newSet.has(str) ? changed.push(str) : removed.push(str);
}
return {
added,
changed,
removed,
};
}
function reportHeader() {
return (
"# Build Size Report\n\n" +
"Changes to minified artifacts in `/build`, after **gzip** compression."
);
}
function reportAddedFilesSection(_base, pr, addedFiles) {
let md = "";
const maybeS = addedFiles.length === 1 ? "" : "s";
md += `## ${addedFiles.length} Added File${maybeS}\n\n`;
md += "<details>\n";
md += "<summary>View Changes</summary>\n\n";
md += "| file | size |\n";
md += "| --- | --- |\n";
for (const file of addedFiles) {
const computedSize = computedFile(pr, file);
md += `| ${file} | +${formatBytes(computedSize)} |\n`;
}
md += "\n";
md += "</details>\n";
return md;
}
function reportRemovedFilesSection(base, _pr, removedFiles) {
let md = "";
const maybeS = removedFiles.length === 1 ? "" : "s";
md += `## ${removedFiles.length} Removed File${maybeS}\n\n`;
md += "<details>\n";
md += "<summary>View Changes</summary>\n\n";
md += "| file | size |\n";
md += "| --- | --- |\n";
for (const file of removedFiles) {
const computedSize = computedFile(base, file);
md += `| ${file} | -${formatBytes(computedSize)} |\n`;
}
md += "\n";
md += "</details>\n";
return md;
}
function reportChangedFilesSection(base, pr, changedFiles) {
let md = "";
let numFilesChanged = 0;
let combinedSizeChange = 0;
let sizeChangeMd = "| file | base | pr | diff |\n";
sizeChangeMd += "| --- | --- | --- | --- |\n";
for (const file of changedFiles) {
const computedBase = computedFile(base, file);
const computedPR = computedFile(pr, file);
const diff = computedPR - computedBase;
if (diff !== 0) {
combinedSizeChange += diff;
numFilesChanged += 1;
const sign = diff >= 0 ? "+" : "";
sizeChangeMd += `| ${file} | ${formatBytes(computedBase)} | ${formatBytes(
computedPR
)} | ${sign}${formatBytes(diff)} |\n`;
}
}
if (numFilesChanged > 0) {
const maybeS = numFilesChanged === 1 ? "" : "s";
const sign = combinedSizeChange >= 0 ? "+" : "";
md += `## ${numFilesChanged} file${maybeS} changed\n`;
md += `Total change ${sign}${formatBytes(combinedSizeChange)}\n\n`;
md += "<details>\n";
md += "<summary>View Changes</summary>\n\n";
md += sizeChangeMd;
md += "\n";
md += "</details>\n";
} else {
md += "## No changes\n";
md += "No existing files changed.\n";
}
return md;
}
/**
* Returns markdown report of size differences.
*/
async function createReport() {
const [base, pr] = process.argv.slice(2);
const baseFiles = await minifiedFiles(base);
const prFiles = await minifiedFiles(pr);
const {
added: addedFiles,
removed: removedFiles,
changed: changedFiles,
} = itemChanges(baseFiles, prFiles);
let md = reportHeader();
md += "\n\n";
if (addedFiles.length > 0) {
md += reportAddedFilesSection(base, pr, addedFiles);
md += "\n";
}
if (removedFiles.length > 0) {
md += reportRemovedFilesSection(base, pr, removedFiles);
md += "\n";
}
md += reportChangedFilesSection(base, pr, changedFiles);
return md;
}
(async () => {
console.log(await createReport());
})();