mempool/backend/src/api/backend-info.ts
Erik Arvstedt d591f7c456
backend: Fetch package version at build time
Extract `fetch-version.ts` which is called at build time to create
file `dist/api/version.json`.
This file is read by `backend-info.ts` at runtime.

This also fixes handing over the Git commit hash to the backend app
in the Docker backend image, which was broken as of 2022-07-12.
(Reason: The commit hash was previously required at runtime, but was
only provided at build time.)
2022-09-02 12:50:43 +02:00

38 lines
975 B
TypeScript

import fs from 'fs';
import path from 'path';
import os from 'os';
import { IBackendInfo } from '../mempool.interfaces';
class BackendInfo {
private backendInfo: IBackendInfo;
constructor() {
// This file is created by ./fetch-version.ts during building
const versionFile = path.join(__dirname, 'version.json')
var versionInfo;
if (fs.existsSync(versionFile)) {
versionInfo = JSON.parse(fs.readFileSync(versionFile).toString());
} else {
// Use dummy values if `versionFile` doesn't exist (e.g., during testing)
versionInfo = {
version: '?',
gitCommit: '?'
};
}
this.backendInfo = {
hostname: os.hostname(),
version: versionInfo.version,
gitCommit: versionInfo.gitCommit
};
}
public getBackendInfo(): IBackendInfo {
return this.backendInfo;
}
public getShortCommitHash() {
return this.backendInfo.gitCommit.slice(0, 7);
}
}
export default new BackendInfo();