fix: stamp build commit metadata

This commit is contained in:
Peter Steinberger
2026-01-17 12:30:11 +00:00
parent 25d8043b9d
commit 49c35c752c
4 changed files with 73 additions and 3 deletions

View File

@@ -0,0 +1,48 @@
import { execSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const distDir = path.join(rootDir, "dist");
const pkgPath = path.join(rootDir, "package.json");
const readPackageVersion = () => {
try {
const raw = fs.readFileSync(pkgPath, "utf8");
const parsed = JSON.parse(raw) as { version?: string };
return parsed.version ?? null;
} catch {
return null;
}
};
const resolveCommit = () => {
const envCommit = process.env.GIT_COMMIT?.trim() || process.env.GIT_SHA?.trim();
if (envCommit) return envCommit;
try {
return execSync("git rev-parse HEAD", {
cwd: rootDir,
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
} catch {
return null;
}
};
const version = readPackageVersion();
const commit = resolveCommit();
const buildInfo = {
version,
commit,
builtAt: new Date().toISOString(),
};
fs.mkdirSync(distDir, { recursive: true });
fs.writeFileSync(
path.join(distDir, "build-info.json"),
`${JSON.stringify(buildInfo, null, 2)}\n`,
);