Files
taskboard/scripts/write-build-info.mjs

46 lines
1.6 KiB
JavaScript

// 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));