sh_discord_bot/kdb-web/update-version.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-10-21 13:50:11 +02:00
import { Appsettings } from "./src/app/models/config/appsettings";
import { SoftwareVersion } from "./src/app/models/config/software-version";
2022-10-16 00:13:03 +02:00
2022-10-21 13:50:11 +02:00
const jsonFilePath = "./src/assets/config.json";
2022-10-16 00:13:03 +02:00
function Main(): void {
2022-10-21 13:50:11 +02:00
getVersion()
.then(version => {
setVersion(version);
})
.catch(err => {
throw err;
});
2022-10-16 00:13:03 +02:00
}
async function getVersion(): Promise<SoftwareVersion> {
2022-10-21 13:50:11 +02:00
const util = require("util");
const exec = util.promisify(require("child_process").exec);
let major = "0";
let minor = "0";
let micro = "0";
const branch: string = (await exec("git rev-parse --abbrev-ref HEAD")).stdout.toString().trim();
if (branch.includes(".")) {
const versions = branch.split(".");
if (versions.length > 0) {
major = versions[0];
}
if (versions.length > 1) {
minor = versions[1];
2022-10-16 00:13:03 +02:00
}
2022-10-21 13:50:11 +02:00
if (versions.length > 2) {
micro = versions[2];
}
} else if (branch.startsWith("#")) {
const fs = require("fs");
const config: Appsettings = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8'))
major = config.WebVersion.Major;
minor = config.WebVersion.Minor;
micro = `dev${branch.split("#")[1]}`;
}
return new SoftwareVersion(major, minor, micro);
2022-10-16 00:13:03 +02:00
}
2022-10-21 13:50:11 +02:00
2022-10-16 00:13:03 +02:00
async function setVersion(version: SoftwareVersion) {
2022-10-21 13:50:11 +02:00
const fs = require("fs");
fs.readFile(jsonFilePath, "utf8", (err: Error, data: string) => {
if (err) {
throw err;
}
const settings: Appsettings = JSON.parse(data);
settings.WebVersion = version;
fs.writeFile(jsonFilePath, JSON.stringify(settings, null, 4), "utf8", () => {
2022-10-16 00:13:03 +02:00
});
2022-10-21 13:50:11 +02:00
});
fs.readFile('./package.json', "utf8", (err: Error, data: string) => {
if (err) {
throw err;
}
const settings = JSON.parse(data);
settings.version = version.getVersionString();
fs.writeFile('./package.json', JSON.stringify(settings, null, 4), "utf8", () => {
});
});
2022-10-16 00:13:03 +02:00
}
Main();