mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
version.json: refactor, don't use error(), use cf pages env if available
This commit is contained in:
parent
a5d87edeca
commit
d1767c550c
1 changed files with 42 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
|||
import { error, json } from '@sveltejs/kit';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { join, parse } from 'node:path';
|
||||
import { existsSync } from 'node:fs';
|
||||
|
@ -18,29 +18,36 @@ const findFile = (file: string) => {
|
|||
|
||||
const root = findFile('.git');
|
||||
const pack = findFile('package.json');
|
||||
if (!root) {
|
||||
throw 'no git repository root found';
|
||||
} else if (!pack) {
|
||||
throw 'no package root found';
|
||||
}
|
||||
|
||||
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 getCommit = async () => {
|
||||
return (await readGit('.git/logs/HEAD'))
|
||||
?.split('\n')
|
||||
?.filter(String)
|
||||
?.pop()
|
||||
?.split(' ')[1];
|
||||
}
|
||||
|
||||
const getBranch = async () => {
|
||||
if (process.env.CF_PAGES_BRANCH) {
|
||||
return process.env.CF_PAGES_BRANCH;
|
||||
}
|
||||
|
||||
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();
|
||||
return (await readGit('.git/HEAD'))
|
||||
?.replace(/^ref: refs\/heads\//, '')
|
||||
?.trim();
|
||||
}
|
||||
|
||||
const getRemote = async () => {
|
||||
let remote = (await readGit('.git/config'))
|
||||
?.split('\n')
|
||||
?.find(line => line.includes('url = ') && line.endsWith('.git'))
|
||||
?.find(line => line.includes('url = '))
|
||||
?.split('url = ')[1];
|
||||
|
||||
if (remote?.startsWith('git@')) {
|
||||
|
@ -51,15 +58,28 @@ export async function GET() {
|
|||
|
||||
remote = remote?.replace(/\.git$/, '');
|
||||
|
||||
if (!remote) {
|
||||
throw 'could not parse remote';
|
||||
}
|
||||
|
||||
return remote;
|
||||
}
|
||||
|
||||
const getVersion = async () => {
|
||||
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 version;
|
||||
}
|
||||
|
||||
return json({ commit, branch, remote, version });
|
||||
export async function GET() {
|
||||
return json({
|
||||
commit: await getCommit(),
|
||||
branch: await getBranch(),
|
||||
remote: await getRemote(),
|
||||
version: await getVersion()
|
||||
});
|
||||
}
|
||||
|
||||
export const prerender = true;
|
Loading…
Reference in a new issue