diff --git a/lib/build-info.json b/lib/build-info.json new file mode 100644 index 0000000..7680e7d --- /dev/null +++ b/lib/build-info.json @@ -0,0 +1,7 @@ +{ + "version": "1.0.0", + "commit": null, + "branch": null, + "sourceHash": "f32721f929", + "builtAt": "2026-11-26T09:09:23.099Z" +} diff --git a/lib/build-info.ts b/lib/build-info.ts new file mode 100644 index 0000000..72fa72a --- /dev/null +++ b/lib/build-info.ts @@ -0,0 +1,13 @@ +// lib/build-info.json is generated by scripts/write-build-info.mjs (prebuild). +// Fallback keeps `next dev` and `tsc` working before the first build. +import generated from './build-info.json'; + +export type BuildInfo = { + version: string; + commit: string | null; + branch: string | null; + sourceHash: string; + builtAt: string; +}; + +export const buildInfo: BuildInfo = generated as BuildInfo; diff --git a/scripts/write-build-info.mjs b/scripts/write-build-info.mjs new file mode 100644 index 0000000..1fa6535 --- /dev/null +++ b/scripts/write-build-info.mjs @@ -0,0 +1,45 @@ +// Runs before `next build`. Records what was built so a redeploy is visible on the page. +// Works with or without a .git directory (ZIP deploys have none). +import { execSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { readFileSync, readdirSync, statSync, writeFileSync, mkdirSync } from 'node:fs'; +import { join } from 'node:path'; + +const pkg = JSON.parse(readFileSync('package.json', 'utf8')); + +function git(cmd) { + try { + return execSync(`git ${cmd}`, { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim(); + } catch { + return null; + } +} + +// Content hash of the source tree: changes whenever the code changes, even without git. +function hashTree(dirs) { + const h = createHash('sha256'); + const walk = (d) => { + for (const name of readdirSync(d).sort()) { + const p = join(d, name); + const s = statSync(p); + if (s.isDirectory()) walk(p); + else { h.update(p); h.update(readFileSync(p)); } + } + }; + for (const d of dirs) { try { walk(d); } catch { /* optional dir */ } } + return h.digest('hex').slice(0, 10); +} + +const info = { + version: process.env.APP_VERSION || pkg.version, + commit: + process.env.GIT_COMMIT || process.env.SOURCE_COMMIT || process.env.COMMIT_SHA || + git('rev-parse --short HEAD') || null, + branch: git('rev-parse --abbrev-ref HEAD'), + sourceHash: hashTree(['app', 'lib', 'components', 'public']), + builtAt: new Date().toISOString(), +}; + +mkdirSync('lib', { recursive: true }); +writeFileSync('lib/build-info.json', JSON.stringify(info, null, 2) + '\n'); +console.log('[build-info]', JSON.stringify(info));