From 72ece7c00b091011617fccf719df7f602cf4f7c7 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:36 -0400 Subject: refactor: scripts/ --- scripts/build-version.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 scripts/build-version.js (limited to 'scripts/build-version.js') diff --git a/scripts/build-version.js b/scripts/build-version.js new file mode 100755 index 0000000..0d77f7d --- /dev/null +++ b/scripts/build-version.js @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const path = require("path"); +const versionFile = path.join("build", "version.json"); +const indexFile = path.join("build", "index.html"); + +const versionDate = (date) => date.toISOString().replace(".000", ""); + +const commitHash = () => { + try { + return require("child_process") + .execSync("git rev-parse --short HEAD") + .toString() + .trim(); + } catch { + return "none"; + } +}; + +const commitDate = (hash) => { + try { + const unix = require("child_process") + .execSync(`git show -s --format=%ct ${hash}`) + .toString() + .trim(); + const date = new Date(parseInt(unix) * 1000); + return versionDate(date); + } catch { + return versionDate(new Date()); + } +}; + +const getFullVersion = () => { + const hash = commitHash(); + return `${commitDate(hash)}-${hash}`; +}; + +const data = JSON.stringify( + { + version: getFullVersion(), + }, + undefined, + 2, +); + +fs.writeFileSync(versionFile, data); + +// https://stackoverflow.com/a/14181136/8418 +fs.readFile(indexFile, "utf8", (error, data) => { + if (error) { + return console.error(error); + } + const result = data.replace(/{version}/g, getFullVersion()); + + fs.writeFile(indexFile, result, "utf8", (error) => { + if (error) { + return console.error(error); + } + }); +}); -- cgit v1.2.3