web: add prerendered version.json endpoint for frontend metadata

This commit is contained in:
dumbmoron
2024-07-10 16:04:00 +00:00
parent b92579ea2c
commit 23bcd6076a
3 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import { error, json } from '@sveltejs/kit';
import { readFile } from 'node:fs/promises';
import { join, parse } from 'node:path';
import { existsSync } from 'node:fs';
import { cwd } from 'node:process';
const findFile = (file: string) => {
let dir = cwd();
while (dir !== parse(dir).root) {
if (existsSync(join(dir, file))) {
return dir;
}
dir = join(dir, '../');
}
}
const root = findFile('.git');
const pack = findFile('package.json');
export async function GET() {
if (!root) {
return error(500, 'no git repository root found');
} else if (!pack) {
return error(500, 'no package root found');
}
const readGit = (filename: string) => readFile(join(root, filename), 'utf8');
const commit = (await readGit('.git/logs/HEAD'))
?.split('\n')
?.filter(String)
?.pop()
?.split(' ')[1];
const branch = (await readGit('.git/HEAD'))
?.replace(/^ref: refs\/heads\//, '')
?.trim();
const remote = (await readGit('.git/config'))
?.split('\n')
?.find(line => line.includes('url = ') && line.endsWith('.git'))
?.split(':')[1]
?.replace(/\.git$/, '');
const { version } = JSON.parse(
await readFile(join(pack, 'package.json'), 'utf8')
);
if (!commit || !branch || !remote || !version) {
return error(500, 'failed to extract project metadata');
}
return json({ commit, branch, remote, version });
}
export const prerender = true;