33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
import { execSync } from "node:child_process";
|
||
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
import { dirname, resolve } from "node:path";
|
||
|
|
|
||
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const versionPath = resolve(__dirname, "..", "version.json");
|
||
|
|
|
||
|
|
function git(args) {
|
||
|
|
return execSync(`git ${args}`, { encoding: "utf8" }).trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
let commit = "unknown";
|
||
|
|
let date = new Date().toISOString().slice(0, 10).replace(/-/g, "");
|
||
|
|
try {
|
||
|
|
commit = git("log -1 --format=%h");
|
||
|
|
date = git("log -1 --format=%cd --date=format:%Y%m%d");
|
||
|
|
} catch (err) {
|
||
|
|
console.warn(`[update-version] git unavailable, using fallback: ${err.message}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const build = `${date}.${commit}`;
|
||
|
|
const current = JSON.parse(readFileSync(versionPath, "utf8"));
|
||
|
|
const next = { ...current, build };
|
||
|
|
|
||
|
|
if (current.build === build) {
|
||
|
|
console.log(`[update-version] build unchanged: ${build}`);
|
||
|
|
} else {
|
||
|
|
writeFileSync(versionPath, JSON.stringify(next, null, 2) + "\n");
|
||
|
|
console.log(`[update-version] ${current.build} -> ${build}`);
|
||
|
|
}
|