|
| 1 | +import { readdir, stat } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 6 | +const svgDir = path.resolve(__dirname, "..", "svgs"); |
| 7 | + |
| 8 | +function formatBytes(bytes) { |
| 9 | + if (bytes >= 1024) { |
| 10 | + return `${(bytes / 1024).toFixed(1)} KB`; |
| 11 | + } |
| 12 | + return `${bytes.toLocaleString()} B`; |
| 13 | +} |
| 14 | + |
| 15 | +function parseArgs() { |
| 16 | + const args = process.argv.slice(2); |
| 17 | + const idx = args.indexOf("--threshold"); |
| 18 | + if (idx !== -1 && args[idx + 1]) { |
| 19 | + return { threshold: Number(args[idx + 1]) }; |
| 20 | + } |
| 21 | + return { threshold: null }; |
| 22 | +} |
| 23 | + |
| 24 | +async function svgSizes() { |
| 25 | + const { threshold } = parseArgs(); |
| 26 | + const files = (await readdir(svgDir)).filter((f) => f.endsWith(".svg")).sort(); |
| 27 | + |
| 28 | + const entries = await Promise.all( |
| 29 | + files.map(async (file) => { |
| 30 | + const info = await stat(path.join(svgDir, file)); |
| 31 | + return { file, size: info.size }; |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + entries.sort((a, b) => b.size - a.size); |
| 36 | + |
| 37 | + const maxName = Math.max(...entries.map((e) => e.file.length)); |
| 38 | + const maxSize = Math.max(...entries.map((e) => formatBytes(e.size).length)); |
| 39 | + const colWidth = maxName + maxSize + 4; |
| 40 | + const termWidth = process.stdout.columns || 80; |
| 41 | + const cols = Math.max(1, Math.floor(termWidth / (colWidth + 2))); |
| 42 | + |
| 43 | + console.log("\nSVG File Sizes (largest first)"); |
| 44 | + console.log("\u2500".repeat(Math.min(termWidth, cols * (colWidth + 2)))); |
| 45 | + |
| 46 | + for (let i = 0; i < entries.length; i += cols) { |
| 47 | + const row = entries.slice(i, i + cols); |
| 48 | + const cells = row.map(({ file, size }) => { |
| 49 | + const sizeStr = formatBytes(size).padStart(maxSize); |
| 50 | + const marker = threshold !== null && size > threshold ? "*" : " "; |
| 51 | + return `${marker} ${file.padEnd(maxName)} ${sizeStr}`; |
| 52 | + }); |
| 53 | + console.log(cells.join(" ")); |
| 54 | + } |
| 55 | + |
| 56 | + const totalSize = entries.reduce((sum, e) => sum + e.size, 0); |
| 57 | + const avg = Math.round(totalSize / entries.length); |
| 58 | + const max = entries[0]; |
| 59 | + |
| 60 | + console.log("\u2500".repeat(Math.min(termWidth, cols * (colWidth + 2)))); |
| 61 | + console.log(` Total: ${entries.length} files, ${formatBytes(totalSize)}`); |
| 62 | + console.log(` Avg: ${formatBytes(avg)}`); |
| 63 | + console.log(` Max: ${max.file} (${formatBytes(max.size)})`); |
| 64 | + |
| 65 | + if (threshold !== null) { |
| 66 | + const over = entries.filter((e) => e.size > threshold); |
| 67 | + console.log( |
| 68 | + ` Over ${formatBytes(threshold)}: ${over.length} file${over.length === 1 ? "" : "s"} *`, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + console.log(); |
| 73 | +} |
| 74 | + |
| 75 | +svgSizes(); |
0 commit comments