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 { readFile } from 'node:fs/promises';
|
||||||
import { join, parse } from 'node:path';
|
import { join, parse } from 'node:path';
|
||||||
import { existsSync } from 'node:fs';
|
import { existsSync } from 'node:fs';
|
||||||
|
@ -18,29 +18,36 @@ const findFile = (file: string) => {
|
||||||
|
|
||||||
const root = findFile('.git');
|
const root = findFile('.git');
|
||||||
const pack = findFile('package.json');
|
const pack = findFile('package.json');
|
||||||
|
|
||||||
export async function GET() {
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
return error(500, 'no git repository root found');
|
throw 'no git repository root found';
|
||||||
} else if (!pack) {
|
} else if (!pack) {
|
||||||
return error(500, 'no package root found');
|
throw 'no package root found';
|
||||||
}
|
}
|
||||||
|
|
||||||
const readGit = (filename: string) => readFile(join(root, filename), 'utf8');
|
const readGit = (filename: string) => readFile(join(root, filename), 'utf8');
|
||||||
|
|
||||||
const commit = (await readGit('.git/logs/HEAD'))
|
const getCommit = async () => {
|
||||||
|
return (await readGit('.git/logs/HEAD'))
|
||||||
?.split('\n')
|
?.split('\n')
|
||||||
?.filter(String)
|
?.filter(String)
|
||||||
?.pop()
|
?.pop()
|
||||||
?.split(' ')[1];
|
?.split(' ')[1];
|
||||||
|
}
|
||||||
|
|
||||||
const branch = (await readGit('.git/HEAD'))
|
const getBranch = async () => {
|
||||||
|
if (process.env.CF_PAGES_BRANCH) {
|
||||||
|
return process.env.CF_PAGES_BRANCH;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (await readGit('.git/HEAD'))
|
||||||
?.replace(/^ref: refs\/heads\//, '')
|
?.replace(/^ref: refs\/heads\//, '')
|
||||||
?.trim();
|
?.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
const getRemote = async () => {
|
||||||
let remote = (await readGit('.git/config'))
|
let remote = (await readGit('.git/config'))
|
||||||
?.split('\n')
|
?.split('\n')
|
||||||
?.find(line => line.includes('url = ') && line.endsWith('.git'))
|
?.find(line => line.includes('url = '))
|
||||||
?.split('url = ')[1];
|
?.split('url = ')[1];
|
||||||
|
|
||||||
if (remote?.startsWith('git@')) {
|
if (remote?.startsWith('git@')) {
|
||||||
|
@ -51,15 +58,28 @@ export async function GET() {
|
||||||
|
|
||||||
remote = remote?.replace(/\.git$/, '');
|
remote = remote?.replace(/\.git$/, '');
|
||||||
|
|
||||||
|
if (!remote) {
|
||||||
|
throw 'could not parse remote';
|
||||||
|
}
|
||||||
|
|
||||||
|
return remote;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getVersion = async () => {
|
||||||
const { version } = JSON.parse(
|
const { version } = JSON.parse(
|
||||||
await readFile(join(pack, 'package.json'), 'utf8')
|
await readFile(join(pack, 'package.json'), 'utf8')
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!commit || !branch || !remote || !version) {
|
return version;
|
||||||
return error(500, 'failed to extract project metadata');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
export const prerender = true;
|
Loading…
Reference in a new issue