Files
jacob-mathison d0ac1c5622
Lint and Build Checks / Lint, Test, and Build (push) Failing after 560h18m4s
feat(changelog): add release ledger data
2026-06-16 11:19:24 -05:00

119 lines
3.2 KiB
JavaScript

import { readFile, writeFile } from 'node:fs/promises';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
const envPath = new URL('../.env', import.meta.url);
const packagePath = new URL('../package.json', import.meta.url);
const packageLockPath = new URL('../package-lock.json', import.meta.url);
function getNextPatchVersion(version) {
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
if (match == null) {
throw new Error(`Expected package version to use #.#.# format, got "${version}".`);
}
const [, major, minor, patch] = match;
return `${major}.${minor}.${Number(patch) + 1}`;
}
async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf8'));
}
async function writeJson(filePath, json) {
await writeFile(filePath, `${JSON.stringify(json, null, 2)}\n`);
}
async function getLastSuccessfulCommit() {
const { stdout } = await execFileAsync('git', ['rev-parse', '--short=7', 'HEAD']);
return stdout.trim();
}
function getBuildDate() {
const now = new Date();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
return `${now.getFullYear()}${month}${day}`;
}
async function readEnvLines() {
try {
return (await readFile(envPath, 'utf8')).split(/\r?\n/);
} catch (error) {
if (error != null && typeof error === 'object' && 'code' in error) {
const nodeError = error;
if (nodeError.code === 'ENOENT') {
return [];
}
}
throw error;
}
}
async function writeEnvValues(values) {
const lines = await readEnvLines();
const remainingKeys = new Set(Object.keys(values));
const nextLines = lines
.filter((line, index) => line.length > 0 || index < lines.length - 1)
.map(line => {
const [key] = line.split('=', 1);
if (!remainingKeys.has(key)) {
return line;
}
remainingKeys.delete(key);
return `${key}=${values[key]}`;
});
for (const key of remainingKeys) {
nextLines.push(`${key}=${values[key]}`);
}
await writeFile(envPath, `${nextLines.join('\n')}\n`);
}
async function main() {
const packageJson = await readJson(packagePath);
const nextVersion = getNextPatchVersion(packageJson.version);
const buildDate = getBuildDate();
const lastSuccessfulCommit = await getLastSuccessfulCommit();
packageJson.version = nextVersion;
await writeJson(packagePath, packageJson);
const packageLockJson = await readJson(packageLockPath);
packageLockJson.version = nextVersion;
if (
packageLockJson.packages != null &&
packageLockJson.packages[''] != null
) {
packageLockJson.packages[''].version = nextVersion;
}
await writeJson(packageLockPath, packageLockJson);
await writeEnvValues({
VITE_LAST_BUILD_DATE: buildDate,
VITE_LAST_SUCCESSFUL_COMMIT: lastSuccessfulCommit,
});
await execFileAsync('git', ['add', '.env', 'package.json', 'package-lock.json']);
console.log(
`Updated build version to ${nextVersion}-${buildDate}-${lastSuccessfulCommit}`,
);
}
main().catch(error => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});