|
| 1 | +import { stat, readdir } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +const BASE = fileURLToPath(import.meta.resolve('../../out/base')); |
| 6 | +const HEAD = fileURLToPath(import.meta.resolve('../../out/head')); |
| 7 | +const UNITS = ['B', 'KB', 'MB', 'GB']; |
| 8 | + |
| 9 | +/** |
| 10 | + * Formats bytes into human-readable format |
| 11 | + * @param {number} bytes - Number of bytes |
| 12 | + * @returns {string} Formatted string (e.g., "1.50 KB") |
| 13 | + */ |
| 14 | +const formatBytes = bytes => { |
| 15 | + if (!bytes) { |
| 16 | + return '0 B'; |
| 17 | + } |
| 18 | + |
| 19 | + const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024)); |
| 20 | + return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${UNITS[i]}`; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Formats the difference between base and head sizes |
| 25 | + * @param {number} base - Base file size in bytes |
| 26 | + * @param {number} head - Head file size in bytes |
| 27 | + * @returns {string} Formatted diff string (e.g., "+1.50 KB (+10. 00%)") |
| 28 | + */ |
| 29 | +const formatDiff = (base, head) => { |
| 30 | + const diff = head - base; |
| 31 | + const sign = diff > 0 ? '+' : ''; |
| 32 | + const percent = base ? `${sign}${((diff / base) * 100).toFixed(2)}%` : 'N/A'; |
| 33 | + return `${sign}${formatBytes(diff)} (${percent})`; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Gets all files in a directory with their stats |
| 38 | + * @param {string} dir - Directory path to search |
| 39 | + * @returns {Promise<Map<string, number>>} Map of filename to size |
| 40 | + */ |
| 41 | +const getDirectoryStats = async dir => { |
| 42 | + const files = await readdir(dir); |
| 43 | + const entries = await Promise.all( |
| 44 | + files.map(async file => [file, (await stat(path.join(dir, file))).size]) |
| 45 | + ); |
| 46 | + return new Map(entries); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Generates a table row for a file |
| 51 | + * @param {string} file - Filename |
| 52 | + * @param {number} baseSize - Base size in bytes |
| 53 | + * @param {number} headSize - Head size in bytes |
| 54 | + * @returns {string} Markdown table row |
| 55 | + */ |
| 56 | +const generateRow = (file, baseSize, headSize) => { |
| 57 | + const baseCol = formatBytes(baseSize); |
| 58 | + const headCol = formatBytes(headSize); |
| 59 | + const diffCol = formatDiff(baseSize, headSize); |
| 60 | + |
| 61 | + return `| \`${file}\` | ${baseCol} | ${headCol} | ${diffCol} |`; |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * Generates a markdown table |
| 66 | + * @param {string[]} files - List of files |
| 67 | + * @param {Map<string, number>} baseStats - Base stats map |
| 68 | + * @param {Map<string, number>} headStats - Head stats map |
| 69 | + * @returns {string} Markdown table |
| 70 | + */ |
| 71 | +const generateTable = (files, baseStats, headStats) => { |
| 72 | + const header = '| File | Base | Head | Diff |\n|------|------|------|------|'; |
| 73 | + const rows = files.map(f => |
| 74 | + generateRow(f, baseStats.get(f), headStats.get(f)) |
| 75 | + ); |
| 76 | + return `${header}\n${rows.join('\n')}`; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Wraps content in a details/summary element |
| 81 | + * @param {string} summary - Summary text |
| 82 | + * @param {string} content - Content to wrap |
| 83 | + * @returns {string} Markdown details element |
| 84 | + */ |
| 85 | +const details = (summary, content) => |
| 86 | + `<details>\n<summary>${summary}</summary>\n\n${content}\n\n</details>`; |
| 87 | + |
| 88 | +const [baseStats, headStats] = await Promise.all( |
| 89 | + [BASE, HEAD].map(getDirectoryStats) |
| 90 | +); |
| 91 | + |
| 92 | +const allFiles = Array.from( |
| 93 | + new Set([...baseStats.keys(), ...headStats.keys()]) |
| 94 | +); |
| 95 | + |
| 96 | +// Filter to only changed files (exist in both and have different sizes) |
| 97 | +const changedFiles = allFiles.filter( |
| 98 | + f => |
| 99 | + baseStats.has(f) && |
| 100 | + headStats.has(f) && |
| 101 | + baseStats.get(f) !== headStats.get(f) |
| 102 | +); |
| 103 | + |
| 104 | +if (changedFiles.length) { |
| 105 | + // Separate HTML files and their matching JS files from other files |
| 106 | + const pages = []; |
| 107 | + const other = []; |
| 108 | + |
| 109 | + // Get all HTML base names |
| 110 | + const htmlBaseNames = new Set( |
| 111 | + changedFiles |
| 112 | + .filter(f => path.extname(f) === '.html') |
| 113 | + .map(f => path.basename(f, '.html')) |
| 114 | + ); |
| 115 | + |
| 116 | + for (const file of changedFiles) { |
| 117 | + const ext = path.extname(file); |
| 118 | + const basename = path.basename(file, ext); |
| 119 | + |
| 120 | + // All HTML files go to pages |
| 121 | + if (ext === '.html') { |
| 122 | + pages.push(file); |
| 123 | + } |
| 124 | + // JS files go to pages only if they have a matching HTML file |
| 125 | + else if (ext === '.js' && htmlBaseNames.has(basename)) { |
| 126 | + pages.push(file); |
| 127 | + } |
| 128 | + // Everything else goes to other |
| 129 | + else { |
| 130 | + other.push(file); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + pages.sort(); |
| 135 | + other.sort(); |
| 136 | + |
| 137 | + console.log('## Web Generator\n'); |
| 138 | + |
| 139 | + if (other.length) { |
| 140 | + console.log(generateTable(other, baseStats, headStats)); |
| 141 | + } |
| 142 | + |
| 143 | + if (pages.length) { |
| 144 | + console.log( |
| 145 | + details( |
| 146 | + `Pages (${pages.filter(f => path.extname(f) === '.html').length})`, |
| 147 | + generateTable(pages, baseStats, headStats) |
| 148 | + ) |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments