-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdangerfile.js
89 lines (78 loc) · 2.61 KB
/
dangerfile.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
/* eslint-disable @typescript-eslint/no-var-requires, no-undef */
import { markdown } from "danger";
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const zlib = require("zlib");
const filesToCheck = [
"packages/react/dist/main.js",
"packages/react/dist/style.css",
"packages/react/dist/react/src/main.d.ts",
];
function getFileSize(filePath) {
const stats = fs.statSync(filePath);
return stats.size;
}
function getGzippedSize(filePath) {
const fileContent = fs.readFileSync(filePath);
const gzippedContent = zlib.gzipSync(fileContent);
return gzippedContent.byteLength;
}
function formatSize(sizeInBytes) {
return (sizeInBytes / 1024).toFixed(2) + " kB";
}
function formatPercentageChange(oldSize, newSize) {
if (oldSize === newSize) {
return "=";
}
const change = ((newSize - oldSize) / oldSize) * 100;
return `${change > 0 ? "+" : ""}${change.toFixed(2)}%`;
}
// Get PR bundle file sizes
const prSizes = {};
const prGzipSizes = {};
for (const filePath of filesToCheck) {
prSizes[filePath] = getFileSize(filePath);
prGzipSizes[filePath] = getGzippedSize(filePath);
}
// Switch to the main branch and build
execSync("git config --global --add safe.directory /__w/javascript/javascript");
execSync("git fetch origin main:main");
execSync("git checkout main");
execSync("pnpm install --frozen-lockfile");
execSync("pnpm build --filter @slashid/react");
const mainSizes = {};
const mainGzipSizes = {};
for (const filePath of filesToCheck) {
mainSizes[filePath] = getFileSize(filePath);
mainGzipSizes[filePath] = getGzippedSize(filePath);
}
// Calculate size differences
const sizeDifferences = {};
const gzipSizeDifferences = {};
for (const filePath of filesToCheck) {
sizeDifferences[filePath] = prSizes[filePath] - mainSizes[filePath];
gzipSizeDifferences[filePath] =
prGzipSizes[filePath] - mainGzipSizes[filePath];
}
// Prepare comment
let comment = "## Bundle size comparison\n\n";
comment +=
"| Name | +/- | Base | Current | +/- gzip | Base gzip | Current gzip |\n";
comment += "| --- | --- | --- | --- | --- | --- | --- |\n";
for (const filePath of filesToCheck) {
comment += `| ${path.basename(filePath)} `;
comment += `| ${formatPercentageChange(
mainSizes[filePath],
prSizes[filePath]
)} `;
comment += `| ${formatSize(mainSizes[filePath])} `;
comment += `| ${formatSize(prSizes[filePath])} `;
comment += `| ${formatPercentageChange(
mainGzipSizes[filePath],
prGzipSizes[filePath]
)} `;
comment += `| ${formatSize(mainGzipSizes[filePath])} `;
comment += `| ${formatSize(prGzipSizes[filePath])} |\n`;
}
markdown(comment);