import { execFileSync } from 'node:child_process' import { existsSync, readdirSync, statSync } from 'node:fs' import { join } from 'node:path' import { DIST_DIR, REPO_ROOT } from './paths.mjs' /** * The harness measures a production build that embeds the reference sheet. * It never builds by itself: the build command is printed instead, so a run * always names the build it measured (mtime, chunk sizes, HEAD). */ export const BUILD_COMMAND = 'SHEET_BUNDLE=draft corepack pnpm --filter @faunosteo/app build' // A build without the sheet (SHEET_BUNDLE=none) leaves a 40-byte stub chunk. const MIN_SHEET_CHUNK_BYTES = 10_000 function assetSize(pattern) { const assets = join(DIST_DIR, 'assets') const name = readdirSync(assets).find((file) => pattern.test(file)) return name ? { name, bytes: statSync(join(assets, name)).size } : null } function headCommit() { try { const args = ['-C', REPO_ROOT, 'rev-parse', '--short', 'HEAD'] return execFileSync('git', args, { encoding: 'utf8' }).trim() } catch { return null } } export function checkBuild() { const index = join(DIST_DIR, 'index.html') if (!existsSync(index)) { throw new Error(`no build in apps/app/dist: run ${BUILD_COMMAND}`) } const sheet = assetSize(/^_virtual_faunosteo-sheet-.*\.js$/) if (!sheet || sheet.bytes < MIN_SHEET_CHUNK_BYTES) { throw new Error(`the build carries no sheet: run ${BUILD_COMMAND}`) } return { distDir: 'apps/app/dist', builtAt: statSync(index).mtime.toISOString(), sheetChunk: sheet, entryChunk: assetSize(/^index-.*\.js$/), wasm: assetSize(/\.wasm$/), headCommit: headCommit(), } }