import { execFileSync } from 'node:child_process' import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs' import { join, sep } from 'node:path' import { sha256 } from './inputs.mjs' import { DIST_DIR, REPO_ROOT, relativeToRepo } 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 and digests, HEAD). * `headCommit` is HEAD when the run starts, which can differ from the commit * the build was made from: the chunk digests identify the build itself. */ 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)) if (!name) return null const path = join(assets, name) return { name, bytes: statSync(path).size, sha256: sha256(readFileSync(path)), } } // Results are committed: name the build by its place in the repository, // never by the machine's absolute path (it carries the user's home). function reportedDistDir() { if (DIST_DIR.startsWith(REPO_ROOT + sep)) return relativeToRepo(DIST_DIR) return '' } 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: reportedDistDir(), builtAt: statSync(index).mtime.toISOString(), sheetChunk: sheet, entryChunk: assetSize(/^index-.*\.js$/), wasm: assetSize(/\.wasm$/), importWorker: assetSize(/^import\.worker-.*\.js$/), headCommit: headCommit(), } }