Spaces:
Running
Running
File size: 1,812 Bytes
5bab120 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import { existsSync } from 'node:fs';
import { join, parse } from 'node:path';
import { cwd } from 'node:process';
import { readFile } from 'node:fs/promises';
const findFile = (file) => {
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');
const readGit = (filename) => {
if (!root) {
throw 'no git repository root found';
}
return readFile(join(root, filename), 'utf8');
}
export const getCommit = async () => {
return (await readGit('.git/logs/HEAD'))
?.split('\n')
?.filter(String)
?.pop()
?.split(' ')[1];
}
export const getBranch = async () => {
if (process.env.CF_PAGES_BRANCH) {
return process.env.CF_PAGES_BRANCH;
}
return (await readGit('.git/HEAD'))
?.replace(/^ref: refs\/heads\//, '')
?.trim();
}
export const getRemote = async () => {
let remote = (await readGit('.git/config'))
?.split('\n')
?.find(line => line.includes('url = '))
?.split('url = ')[1];
if (remote?.startsWith('git@')) {
remote = remote.split(':')[1];
} else if (remote?.startsWith('http')) {
remote = new URL(remote).pathname.substring(1);
}
remote = remote?.replace(/\.git$/, '');
if (!remote) {
throw 'could not parse remote';
}
return remote;
}
export const getVersion = async () => {
if (!pack) {
throw 'no package root found';
}
const { version } = JSON.parse(
await readFile(join(pack, 'package.json'), 'utf8')
);
return version;
}
|