|
| 1 | +import { markdownTable } from 'markdown-table' |
| 2 | +import { existsSync, promises as fs } from 'node:fs' |
| 3 | +import path from 'node:path' |
| 4 | +import { displaySize } from './utils' |
| 5 | + |
| 6 | +import type { BundleReport } from './utils' |
| 7 | + |
| 8 | +type UsageReport = Record<string, BundleReport> |
| 9 | + |
| 10 | +const currDir = path.resolve('temp/size') |
| 11 | +const prevDir = path.resolve('temp/size-prev') |
| 12 | +const sizeHeaders = ['Size', 'Gzip', 'Brotli'] |
| 13 | + |
| 14 | +async function rednerFiles(output: string) { |
| 15 | + const filterFiles = (files: string[]) => |
| 16 | + files.filter(file => file[0] !== '_' && !file.endsWith('.txt')) |
| 17 | + |
| 18 | + const curr = filterFiles(await fs.readdir(currDir)) |
| 19 | + const prev = existsSync(prevDir) ? filterFiles(await fs.readdir(prevDir)) : [] |
| 20 | + const fileList = new Set([...curr, ...prev]) |
| 21 | + |
| 22 | + const rows = [] |
| 23 | + for (const file of fileList) { |
| 24 | + const currPath = path.resolve(currDir, file) |
| 25 | + const prevPath = path.resolve(prevDir, file) |
| 26 | + |
| 27 | + const curr = await importJSON(currPath) |
| 28 | + const prev = await importJSON(prevPath) |
| 29 | + const fileName = curr?.file || prev?.file || '' |
| 30 | + |
| 31 | + if (!curr) { |
| 32 | + rows.push([`~~${fileName}~~`]) |
| 33 | + } else { |
| 34 | + rows.push([ |
| 35 | + fileName, |
| 36 | + `${displaySize(curr.size)}${getDiff(curr.size, prev?.size)}`, |
| 37 | + `${displaySize(curr.gzip)}${getDiff(curr.gzip, prev?.gzip)}`, |
| 38 | + `${displaySize(curr.brotli)}${getDiff(curr.brotli, prev?.brotli)}` |
| 39 | + ]) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + output += '### Bundles\n\n' |
| 44 | + output += markdownTable([['File', ...sizeHeaders], ...rows]) |
| 45 | + output += '\n\n' |
| 46 | + |
| 47 | + return output |
| 48 | +} |
| 49 | + |
| 50 | +async function importJSON(filePath: string) { |
| 51 | + if (!existsSync(filePath)) { |
| 52 | + return undefined |
| 53 | + } |
| 54 | + return (await import(filePath, { assert: { type: 'json' } })).default |
| 55 | +} |
| 56 | + |
| 57 | +function getDiff(curr: number, prev: number) { |
| 58 | + if (prev === undefined) { |
| 59 | + return '' |
| 60 | + } |
| 61 | + const diff = curr - prev |
| 62 | + if (diff === 0) { |
| 63 | + return '' |
| 64 | + } |
| 65 | + const sign = diff > 0 ? '+' : '' |
| 66 | + return ` (**${sign}${displaySize(diff)}**)` |
| 67 | +} |
| 68 | + |
| 69 | +async function renderUsages(output: string) { |
| 70 | + const curr = (await importJSON( |
| 71 | + path.resolve(currDir, '_usages.json') |
| 72 | + )) as UsageReport |
| 73 | + const prev = (await importJSON( |
| 74 | + path.resolve(prevDir, '_usages.json') |
| 75 | + )) as UsageReport |
| 76 | + |
| 77 | + output += '\n### Usages\n\n' |
| 78 | + |
| 79 | + const data = Object.values(curr) |
| 80 | + .map(usage => { |
| 81 | + const prevUsage = prev?.[usage.name] |
| 82 | + const diffSize = getDiff(usage.size, prevUsage?.size) |
| 83 | + const diffGzipped = getDiff(usage.gzip, prevUsage?.gzip) |
| 84 | + const diffBrotli = getDiff(usage.brotli, prevUsage?.brotli) |
| 85 | + |
| 86 | + return [ |
| 87 | + usage.name, |
| 88 | + `${displaySize(usage.size)}${diffSize}`, |
| 89 | + `${displaySize(usage.gzip)}${diffGzipped}`, |
| 90 | + `${displaySize(usage.brotli)}${diffBrotli}` |
| 91 | + ] |
| 92 | + }) |
| 93 | + .filter(usage => !!usage) |
| 94 | + |
| 95 | + output += `${markdownTable([['Name', ...sizeHeaders], ...data])}\n\n` |
| 96 | + |
| 97 | + return output |
| 98 | +} |
| 99 | + |
| 100 | +async function main() { |
| 101 | + let output = '## Size Report\n\n' |
| 102 | + output = await rednerFiles(output) |
| 103 | + output = await renderUsages(output) |
| 104 | + process.stdout.write(output) |
| 105 | +} |
| 106 | + |
| 107 | +main().catch(err => { |
| 108 | + console.error(err) |
| 109 | + process.exit(1) |
| 110 | +}) |
0 commit comments