Skip to content

Commit

Permalink
Only read version tag files once on startup (#174)
Browse files Browse the repository at this point in the history
We already read it once for the `/health-check` endpoint and cached the response but this way we can use `getVersionTags()` everywhere without worrying about it.

Also, it's no longer `async` so we can use it in things like Express route paths and CDN asset tags more easily.
  • Loading branch information
MadLittleMods authored Apr 19, 2023
1 parent 78ee88e commit 50a1d65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
34 changes: 16 additions & 18 deletions server/lib/get-version-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

const assert = require('assert');
const path = require('path');
const { readFile } = require('fs').promises;
const fs = require('fs');

const packageInfo = require('../../package.json');
assert(packageInfo.version);

async function getVersionTags() {
let commit;
let version;
let versionDate;
let packageVersion = packageInfo.version;
const packageVersion = packageInfo.version;

function readFileSync(path) {
try {
[commit, version, versionDate] = await Promise.all([
readFile(path.join(__dirname, '../../dist/GIT_COMMIT'), 'utf8'),
readFile(path.join(__dirname, '../../dist/VERSION'), 'utf8'),
readFile(path.join(__dirname, '../../dist/VERSION_DATE'), 'utf8'),
]);
return fs.readFileSync(path, 'utf8');
} catch (err) {
console.warn('Unable to read version tags', err);
commit = 'Not specified';
version = 'Not specified';
versionDate = 'Not specified';
console.warn(`Unable to read version tags path=${path}`, err);
return null;
}
}

const commit = readFileSync(path.join(__dirname, '../../dist/GIT_COMMIT'), 'utf8').trim();
const version = readFileSync(path.join(__dirname, '../../dist/VERSION'), 'utf8').trim();
const versionDate = readFileSync(path.join(__dirname, '../../dist/VERSION_DATE'), 'utf8').trim();

function getVersionTags() {
return {
commit: commit.trim(),
version: version.trim(),
versionDate: versionDate.trim(),
commit,
version,
versionDate,
packageVersion,
};
}
Expand Down
2 changes: 1 addition & 1 deletion server/routes/install-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function installRoutes(app) {
identifyRoute('health-check'),
asyncHandler(async function (req, res) {
if (!healthCheckResponse) {
const versionTags = await getVersionTags();
const versionTags = getVersionTags();
const responseObject = {
ok: true,
...versionTags,
Expand Down

0 comments on commit 50a1d65

Please sign in to comment.