// Generators normally write their products. --check compares the same bytes without creating or updating files. import { mkdir, readFile, writeFile } from 'node:fs/promises'; import path from 'node:path'; export async function generatedWriter(directory) { const args = process.argv.slice(2), script = path.basename(process.argv[1]); if (args.length && (args.length !== 1 || args[0] !== '--check')) throw new Error(`Usage: node scripts/${script} [--check]`); if (!args.length) { await mkdir(directory, { recursive: true }); return (name, bytes) => writeFile(new URL(name, directory), bytes); } return async (name, bytes) => { const file = new URL(name, directory); let status = 'out of date'; try { if ((await readFile(file)).equals(Buffer.from(bytes))) return; } catch (error) { if (error.code !== 'ENOENT') throw error; status = 'missing'; } console.error(`${name} is ${status}. Run node scripts/${script} to regenerate it.`); process.exitCode = 1; }; }