70 lines
No EOL
2.2 KiB
TypeScript
70 lines
No EOL
2.2 KiB
TypeScript
/**
|
|
* Generates src/generated/versionInfo.json with the project's current version,
|
|
* git tag/describe info, and the last commit's metadata. Consumed by the admin
|
|
* VersionOverlay via GET /api/version (the file is read at runtime so a fresh
|
|
* post-bump regeneration rsync'd to the server shows up without a rebuild).
|
|
*
|
|
* Gracefully no-ops on missing git metadata (no .git, no tags) so the JSON
|
|
* always exists with whatever fields are resolvable.
|
|
*/
|
|
import { execSync } from "node:child_process";
|
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
const projectRoot = path.resolve(here, "..", "..");
|
|
|
|
function git(args: string): string | null {
|
|
try {
|
|
const out = execSync(`git ${args}`, { cwd: projectRoot, encoding: "utf8" });
|
|
const trimmed = out.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function readPackageVersion(): string | null {
|
|
try {
|
|
const pkg = JSON.parse(readFileSync(path.join(projectRoot, "package.json"), "utf8"));
|
|
return typeof pkg?.version === "string" && pkg.version.length > 0 ? pkg.version : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function main(): void {
|
|
const version = readPackageVersion();
|
|
const tag = git("describe --tags --always --abbrev=0");
|
|
const gitDescribe = git("describe --tags --always --abbrev=7");
|
|
const commitHash = git("rev-parse --short HEAD");
|
|
const commitDate = git("log -1 --format=%cI");
|
|
const commitSubject = git("log -1 --format=%s");
|
|
const buildTime = new Date().toISOString();
|
|
|
|
const info = {
|
|
version,
|
|
tag,
|
|
gitDescribe,
|
|
commitHash,
|
|
commitDate,
|
|
commitSubject,
|
|
buildTime,
|
|
};
|
|
|
|
const outDir = path.join(projectRoot, "src", "generated");
|
|
if (!existsSync(outDir)) {
|
|
mkdirSync(outDir, { recursive: true });
|
|
}
|
|
writeFileSync(
|
|
path.join(outDir, "versionInfo.json"),
|
|
`${JSON.stringify(info, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
|
|
const label = gitDescribe ?? tag ?? version ?? "unknown";
|
|
console.log(`[version-info] wrote src/generated/versionInfo.json (${label})`);
|
|
}
|
|
|
|
main(); |