mirror of
https://github.com/imputnet/cobalt.git
synced 2024-12-26 17:46:06 +00:00
packages: add version-info package
This commit is contained in:
parent
bef9b5b172
commit
0d20ffc004
6
packages/version-info/index.d.ts
vendored
Normal file
6
packages/version-info/index.d.ts
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
declare module "@imput/version-info" {
|
||||
export function getCommit(): Promise<string | undefined>;
|
||||
export function getBranch(): Promise<string | undefined>;
|
||||
export function getRemote(): Promise<string>;
|
||||
export function getVersion(): Promise<string>;
|
||||
}
|
73
packages/version-info/index.js
Normal file
73
packages/version-info/index.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
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');
|
||||
if (!root) {
|
||||
throw 'no git repository root found';
|
||||
} else if (!pack) {
|
||||
throw 'no package root found';
|
||||
}
|
||||
|
||||
const readGit = (filename) => 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 () => {
|
||||
const { version } = JSON.parse(
|
||||
await readFile(join(pack, 'package.json'), 'utf8')
|
||||
);
|
||||
|
||||
return version;
|
||||
}
|
18
packages/version-info/package.json
Normal file
18
packages/version-info/package.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "@imput/version-info",
|
||||
"version": "1.0.0",
|
||||
"description": "helper package for cobalt that provides commit info & version from package file.",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"type": "module",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/imputnet/cobalt.git"
|
||||
},
|
||||
"author": "imput",
|
||||
"license": "AGPL-3.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/imputnet/cobalt/issues"
|
||||
},
|
||||
"homepage": "https://github.com/imputnet/cobalt#readme"
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
packages:
|
||||
- "api"
|
||||
- "web"
|
||||
- "packages/*"
|
||||
|
|
Loading…
Reference in a new issue