|
| 1 | +import assert from "assert"; |
| 2 | +import cp from "child_process"; |
| 3 | + |
| 4 | +const baseRepo = process.argv[2]; |
| 5 | +const headRepo = process.argv[3]; |
| 6 | + |
| 7 | +/** @type {Array<{ size: number, unpackedSize: number; files: Array<{ path: string; size: number; }>; }>} */ |
| 8 | +const [before, after] = JSON.parse(cp.execFileSync("npm", ["pack", "--dry-run", "--json", baseRepo, headRepo], { encoding: "utf8" })); |
| 9 | + |
| 10 | +/** @param {{ path: string; size: number; }[]} files */ |
| 11 | +function filesToMap(files) { |
| 12 | + return new Map(files.map(f => [f.path, f.size])); |
| 13 | +} |
| 14 | + |
| 15 | +const beforeFileToSize = filesToMap(before.files); |
| 16 | +const afterFileToSize = filesToMap(after.files); |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {number} before |
| 20 | + * @param {number} after |
| 21 | + */ |
| 22 | +function failIfTooBig(before, after) { |
| 23 | + if (after > (before * 1.1)) { |
| 24 | + process.exitCode = 1; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {number} value |
| 30 | + */ |
| 31 | +function sign(value) { |
| 32 | + return value > 0 ? "+" : "-"; |
| 33 | +} |
| 34 | + |
| 35 | +const units = ["B", "KiB", "MiB", "GiB"]; |
| 36 | +/** |
| 37 | + * @param {number} size |
| 38 | + */ |
| 39 | +function prettyPrintSize(size) { |
| 40 | + assert(size >= 0); |
| 41 | + |
| 42 | + let i = 0; |
| 43 | + while (size > 1024) { |
| 44 | + i++; |
| 45 | + size /= 1024; |
| 46 | + } |
| 47 | + |
| 48 | + return `${size.toFixed(2)} ${units[i]}`; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {number} before |
| 53 | + * @param {number} after |
| 54 | + */ |
| 55 | +function prettyPrintSizeDiff(before, after) { |
| 56 | + const diff = after - before; |
| 57 | + return sign(diff) + prettyPrintSize(Math.abs(diff)); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @param {number} before |
| 62 | + * @param {number} after |
| 63 | + */ |
| 64 | +function prettyPercentDiff(before, after) { |
| 65 | + const percent = 100 * (after - before) / before; |
| 66 | + return `${sign(percent)}${Math.abs(percent).toFixed(2)}%`; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * @param {string[]} header |
| 71 | + * @param {string[][]} data |
| 72 | + */ |
| 73 | +function logTable(header, data) { |
| 74 | + /** @type {string[]} */ |
| 75 | + const lines = []; |
| 76 | + |
| 77 | + /** |
| 78 | + * @param {string[]} row |
| 79 | + */ |
| 80 | + function addRow(row) { |
| 81 | + lines.push("| " + row.join(" | ") + " |"); |
| 82 | + } |
| 83 | + |
| 84 | + addRow(header); |
| 85 | + addRow(new Array(header.length).fill("-")); |
| 86 | + for (const row of data) { |
| 87 | + addRow(row); |
| 88 | + } |
| 89 | + |
| 90 | + console.log(lines.join("\n")); |
| 91 | +} |
| 92 | + |
| 93 | +console.log(`# Package size report`); |
| 94 | +console.log(); |
| 95 | + |
| 96 | +console.log(`## Overall package size`); |
| 97 | +console.log(); |
| 98 | + |
| 99 | +if (before.size === after.size && before.unpackedSize === after.unpackedSize) { |
| 100 | + console.log("No change."); |
| 101 | +} |
| 102 | +else { |
| 103 | + logTable( |
| 104 | + ["", "Before", "After", "Diff", "Diff (percent)"], |
| 105 | + [ |
| 106 | + [ |
| 107 | + "Packed", |
| 108 | + prettyPrintSize(before.size), |
| 109 | + prettyPrintSize(after.size), |
| 110 | + prettyPrintSizeDiff(before.size, after.size), |
| 111 | + prettyPercentDiff(before.size, after.size), |
| 112 | + ], |
| 113 | + [ |
| 114 | + "Unpacked", |
| 115 | + prettyPrintSize(before.unpackedSize), |
| 116 | + prettyPrintSize(after.unpackedSize), |
| 117 | + prettyPrintSizeDiff(before.unpackedSize, after.unpackedSize), |
| 118 | + prettyPercentDiff(before.unpackedSize, after.unpackedSize), |
| 119 | + ], |
| 120 | + ] |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +failIfTooBig(before.size, after.size); |
| 125 | +failIfTooBig(before.unpackedSize, after.unpackedSize); |
| 126 | + |
| 127 | +console.log(); |
| 128 | + |
| 129 | + |
| 130 | +/** @type {Map<string, number>} */ |
| 131 | +const fileCounts = new Map(); |
| 132 | +const inBefore = -1; |
| 133 | +const inAfter = 1; |
| 134 | + |
| 135 | +/** |
| 136 | + * @param {Iterable<string>} paths |
| 137 | + * @param {-1 | 1} marker |
| 138 | + */ |
| 139 | +function addFiles(paths, marker) { |
| 140 | + for (const p of paths) { |
| 141 | + fileCounts.set(p, (fileCounts.get(p) ?? 0) + marker); |
| 142 | + } |
| 143 | +} |
| 144 | +addFiles(beforeFileToSize.keys(), inBefore); |
| 145 | +addFiles(afterFileToSize.keys(), inAfter); |
| 146 | + |
| 147 | +const allEntries = [...fileCounts.entries()]; |
| 148 | +const commonFiles = allEntries.filter(([, count]) => count === 0).map(([path]) => path); |
| 149 | +const beforeOnly = allEntries.filter(([, count]) => count === inBefore).map(([path]) => path); |
| 150 | +const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path]) => path); |
| 151 | + |
| 152 | +const commonData = commonFiles.map(path => { |
| 153 | + const beforeSize = beforeFileToSize.get(path) ?? 0; |
| 154 | + const afterSize = afterFileToSize.get(path) ?? 0; |
| 155 | + return { path, beforeSize, afterSize }; |
| 156 | +}) |
| 157 | + .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) |
| 158 | + .map(({ path, beforeSize, afterSize }) => { |
| 159 | + return [ |
| 160 | + "`" + path + "`", |
| 161 | + prettyPrintSize(beforeSize), |
| 162 | + prettyPrintSize(afterSize), |
| 163 | + prettyPrintSizeDiff(beforeSize, afterSize), |
| 164 | + prettyPercentDiff(beforeSize, afterSize), |
| 165 | + ]; |
| 166 | + }); |
| 167 | + |
| 168 | +if (commonData.length > 0) { |
| 169 | + console.log(`## Files`); |
| 170 | + console.log(); |
| 171 | + logTable(["", "Before", "After", "Diff", "Diff (percent)"], commonData); |
| 172 | + console.log(); |
| 173 | +} |
| 174 | + |
| 175 | +if (afterOnly.length > 0) { |
| 176 | + console.log(`## New files`); |
| 177 | + console.log(); |
| 178 | + logTable( |
| 179 | + ["", "Size"], |
| 180 | + afterOnly.map(path => { |
| 181 | + const afterSize = afterFileToSize.get(path) ?? 0; |
| 182 | + return { path, afterSize }; |
| 183 | + }) |
| 184 | + .map(({ path, afterSize }) => { |
| 185 | + return ["`" + path + "`", prettyPrintSize(afterSize)]; |
| 186 | + }), |
| 187 | + ); |
| 188 | + console.log(); |
| 189 | +} |
| 190 | + |
| 191 | +if (beforeOnly.length > 0) { |
| 192 | + console.log(`## Deleted files`); |
| 193 | + console.log(); |
| 194 | + logTable( |
| 195 | + ["", "Size"], |
| 196 | + beforeOnly.map(path => { |
| 197 | + const afterSize = afterFileToSize.get(path) ?? 0; |
| 198 | + return { path, afterSize }; |
| 199 | + }) |
| 200 | + .map(({ path, afterSize }) => { |
| 201 | + return ["`" + path + "`", prettyPrintSize(afterSize)]; |
| 202 | + }), |
| 203 | + ); |
| 204 | + console.log(); |
| 205 | +} |
0 commit comments