26 lines
763 B
TypeScript
26 lines
763 B
TypeScript
import { spawnSync } from "node:child_process";
|
|
import process from "node:process";
|
|
|
|
const commitHash = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i;
|
|
|
|
export function readBuildRevision(cwd: string): string | null {
|
|
const suppliedRevision = process.env["APP_REVISION"]?.trim();
|
|
if (suppliedRevision && commitHash.test(suppliedRevision)) {
|
|
return suppliedRevision.toLowerCase();
|
|
}
|
|
|
|
try {
|
|
const git = spawnSync("git", ["rev-parse", "HEAD"], {
|
|
cwd,
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "ignore"],
|
|
timeout: 1_000,
|
|
windowsHide: true,
|
|
});
|
|
if (git.status !== 0) return null;
|
|
const revision = git.stdout.trim();
|
|
return commitHash.test(revision) ? revision.toLowerCase() : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|