2022-09-08 00:30:04 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const { mkdir, writeFile } = require('fs').promises;
|
|
|
|
const util = require('util');
|
|
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
|
|
|
|
async function mkdirp(path) {
|
|
|
|
try {
|
|
|
|
await mkdir(path, { recursive: true });
|
|
|
|
} catch (err) {
|
|
|
|
console.log('mkdirp err', err);
|
|
|
|
// no-op, the directory is already created
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeVersionFiles() {
|
|
|
|
let commit;
|
|
|
|
let branch;
|
|
|
|
try {
|
|
|
|
commit = (await exec(`git rev-parse HEAD`)).stdout;
|
|
|
|
branch = (await exec(`git rev-parse --abbrev-ref HEAD`)).stdout;
|
|
|
|
} catch (err) {
|
|
|
|
console.log(
|
|
|
|
`Failed to use \`git\` to find the commit and branch.` +
|
|
|
|
` Falling back to using environment variables assuming we're running in GitHub CI. The error encountered:`,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
|
|
|
|
// Pull these values from environment variables provided by GitHub CI
|
|
|
|
commit = process.env.GITHUB_SHA;
|
|
|
|
branch = process.env.GITHUB_REF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!commit || !branch) {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to get a suitable commit=${commit} or branch=${branch} while writing version files`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-24 22:50:53 -06:00
|
|
|
await mkdirp(path.join(__dirname, '../public/'));
|
|
|
|
await writeFile(path.join(__dirname, '../public/GIT_COMMIT'), commit);
|
|
|
|
await writeFile(path.join(__dirname, '../public/VERSION'), branch);
|
|
|
|
await writeFile(path.join(__dirname, '../public/VERSION_DATE'), new Date().toISOString());
|
2022-09-08 00:30:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = writeVersionFiles;
|