web-app-template/frontend/app/update-version.ts

55 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2022-02-20 13:43:25 +01:00
import { Appsettings } from 'src/app/models/config/appsettings';
import { SoftwareVersion } from './src/app/models/config/software-version';
const jsonFilePath = './src/assets/config.json';
function Main(): void {
getVersion()
.then(version => {
setVersion(version);
})
.catch(err => {
throw err;
});
}
async function getVersion(): Promise<SoftwareVersion> {
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];
}
if (versions.length > 2) {
micro = versions[2];
}
}
return new SoftwareVersion(major, minor, micro);
}
async function setVersion(version: SoftwareVersion) {
var fs = require('fs');
fs.readFile(jsonFilePath, 'utf8', (err, data) => {
if (err) {
throw err;
}
const settings: Appsettings = JSON.parse(data);
settings.WebVersion = version;
fs.writeFile(jsonFilePath, JSON.stringify(settings, null, 4), 'utf8', () => {});
});
}
Main();