From 0d20ffc004d3d8e49ef07ca6d46d20630651e22b Mon Sep 17 00:00:00 2001 From: wukko Date: Sat, 3 Aug 2024 21:27:14 +0600 Subject: [PATCH] packages: add version-info package --- packages/version-info/index.d.ts | 6 +++ packages/version-info/index.js | 73 ++++++++++++++++++++++++++++++ packages/version-info/package.json | 18 ++++++++ pnpm-workspace.yaml | 1 + 4 files changed, 98 insertions(+) create mode 100644 packages/version-info/index.d.ts create mode 100644 packages/version-info/index.js create mode 100644 packages/version-info/package.json diff --git a/packages/version-info/index.d.ts b/packages/version-info/index.d.ts new file mode 100644 index 00000000..a6ee9d71 --- /dev/null +++ b/packages/version-info/index.d.ts @@ -0,0 +1,6 @@ +declare module "@imput/version-info" { + export function getCommit(): Promise; + export function getBranch(): Promise; + export function getRemote(): Promise; + export function getVersion(): Promise; +} diff --git a/packages/version-info/index.js b/packages/version-info/index.js new file mode 100644 index 00000000..0df3b855 --- /dev/null +++ b/packages/version-info/index.js @@ -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; +} diff --git a/packages/version-info/package.json b/packages/version-info/package.json new file mode 100644 index 00000000..e82a6201 --- /dev/null +++ b/packages/version-info/package.json @@ -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" +} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 8c71ee3e..d7ab6cba 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,4 @@ packages: - "api" - "web" + - "packages/*"