// Generates app/client.ts (game script string), app/assets.ts (public/ images embedded as base64) // and app/build-info.ts (content hash). Run `npm run build` before every commit. import { createHash } from 'node:crypto' import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs' import { execSync } from 'node:child_process' import { join } from 'node:path' const js = readFileSync('client/game.js', 'utf8') if (/<\/script/i.test(js)) throw new Error('client/game.js must not contain a closing script tag') writeFileSync('app/client.ts', '// Generated by scripts/build.mjs from client/game.js. Do not edit by hand.\nexport const CLIENT_JS = ' + JSON.stringify(js) + '\n') const files = [] const walk = (d) => { for (const f of readdirSync(d).sort()) { const p = join(d, f); statSync(p).isDirectory() ? walk(p) : files.push(p) } } walk('public') const types = { '.png': 'image/png', '.jpg': 'image/jpeg', '.svg': 'image/svg+xml' } const assets = {} for (const f of files) { const ext = f.slice(f.lastIndexOf('.')); if (types[ext]) assets[f.slice('public'.length)] = { type: types[ext], b64: readFileSync(f).toString('base64') } } writeFileSync('app/assets.ts', '// Generated by scripts/build.mjs from public/. Do not edit by hand.\nexport const ASSETS: Record = ' + JSON.stringify(assets) + '\n') const h = createHash('sha256') h.update(js) for (const f of files) h.update(f).update(readFileSync(f)) for (const f of ['app/page.ts', 'app/router.ts']) h.update(readFileSync(f)) const buildId = h.digest('hex').slice(0, 12) let sourceCommit = '' try { sourceCommit = execSync('git rev-parse HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim() } catch {} writeFileSync('app/build-info.ts', `// Generated by scripts/build.mjs. Do not edit by hand.\nexport const BUILD = ${JSON.stringify({ buildId, sourceCommit }, null, '\t')} as const\n`) console.log('build', buildId)