2020-02-28 22:51:36 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2020-12-18 13:02:29 +01:00
|
|
|
const versionFile = path.join("build", "version.json");
|
|
|
|
const indexFile = path.join("build", "index.html");
|
2020-02-28 22:51:36 +01:00
|
|
|
|
2021-01-10 19:48:12 +01:00
|
|
|
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";
|
|
|
|
}
|
2020-02-28 22:51:36 +01:00
|
|
|
};
|
|
|
|
|
2021-01-10 19:48:12 +01:00
|
|
|
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}`;
|
|
|
|
};
|
2020-02-28 22:51:36 +01:00
|
|
|
|
|
|
|
const data = JSON.stringify(
|
|
|
|
{
|
2021-01-10 19:48:12 +01:00
|
|
|
version: getFullVersion(),
|
2020-02-28 22:51:36 +01:00
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
|
2020-12-18 13:02:29 +01:00
|
|
|
fs.writeFileSync(versionFile, data);
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/14181136/8418
|
|
|
|
fs.readFile(indexFile, "utf8", (error, data) => {
|
|
|
|
if (error) {
|
|
|
|
return console.error(error);
|
|
|
|
}
|
2021-01-10 19:48:12 +01:00
|
|
|
const result = data.replace(/{version}/g, getFullVersion());
|
2020-12-18 13:02:29 +01:00
|
|
|
|
|
|
|
fs.writeFile(indexFile, result, "utf8", (error) => {
|
|
|
|
if (error) {
|
|
|
|
return console.error(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|