|
| 1 | +import { existsSync, readFileSync, statSync } from 'node:fs' |
| 2 | +import { dirname, join, resolve } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +type PackageJson = { version?: string } |
| 6 | + |
| 7 | +function readPackageVersion() { |
| 8 | + try { |
| 9 | + const path = join(dirname(fileURLToPath(import.meta.url)), '../../package.json') |
| 10 | + const raw = readFileSync(path, 'utf8') |
| 11 | + const pkg = JSON.parse(raw) as PackageJson |
| 12 | + return typeof pkg.version === 'string' ? pkg.version : '0.0.0' |
| 13 | + } catch { |
| 14 | + return '0.0.0' |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function shortCommit(value: string) { |
| 19 | + const trimmed = value.trim() |
| 20 | + if (!trimmed) return null |
| 21 | + if (trimmed.length <= 8) return trimmed |
| 22 | + return trimmed.slice(0, 8) |
| 23 | +} |
| 24 | + |
| 25 | +export function getCliCommit() { |
| 26 | + const candidates = [ |
| 27 | + process.env.CLAWDHUB_COMMIT, |
| 28 | + process.env.VERCEL_GIT_COMMIT_SHA, |
| 29 | + process.env.GITHUB_SHA, |
| 30 | + process.env.COMMIT_SHA, |
| 31 | + ] |
| 32 | + for (const candidate of candidates) { |
| 33 | + if (!candidate) continue |
| 34 | + const short = shortCommit(candidate) |
| 35 | + if (short) return short |
| 36 | + } |
| 37 | + return readGitCommitFromCwd() |
| 38 | +} |
| 39 | + |
| 40 | +export function getCliVersion() { |
| 41 | + return readPackageVersion() |
| 42 | +} |
| 43 | + |
| 44 | +export function getCliBuildLabel() { |
| 45 | + const version = getCliVersion() |
| 46 | + const commit = getCliCommit() |
| 47 | + return commit ? `v${version} (${commit})` : `v${version}` |
| 48 | +} |
| 49 | + |
| 50 | +function readGitCommitFromCwd() { |
| 51 | + try { |
| 52 | + const gitDir = findGitDir(process.cwd()) |
| 53 | + if (!gitDir) return null |
| 54 | + const headPath = join(gitDir, 'HEAD') |
| 55 | + if (!existsSync(headPath)) return null |
| 56 | + const head = readFileSync(headPath, 'utf8').trim() |
| 57 | + if (!head) return null |
| 58 | + if (!head.startsWith('ref:')) return shortCommit(head) |
| 59 | + const ref = head.replace(/^ref:\s*/, '').trim() |
| 60 | + if (!ref) return null |
| 61 | + const refPath = join(gitDir, ref) |
| 62 | + if (!existsSync(refPath)) return null |
| 63 | + const sha = readFileSync(refPath, 'utf8').trim() |
| 64 | + return shortCommit(sha) |
| 65 | + } catch { |
| 66 | + return null |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function findGitDir(start: string) { |
| 71 | + let current = resolve(start) |
| 72 | + for (;;) { |
| 73 | + const dotGit = join(current, '.git') |
| 74 | + if (existsSync(dotGit)) { |
| 75 | + try { |
| 76 | + const stat = statSync(dotGit) |
| 77 | + if (stat.isDirectory()) return dotGit |
| 78 | + } catch { |
| 79 | + // ignore |
| 80 | + } |
| 81 | + try { |
| 82 | + const content = readFileSync(dotGit, 'utf8').trim() |
| 83 | + const match = content.match(/^gitdir:\s*(.+)$/) |
| 84 | + if (match?.[1]) return resolve(current, match[1]) |
| 85 | + } catch { |
| 86 | + return dotGit |
| 87 | + } |
| 88 | + return dotGit |
| 89 | + } |
| 90 | + const parent = resolve(current, '..') |
| 91 | + if (parent === current) return null |
| 92 | + current = parent |
| 93 | + } |
| 94 | +} |
0 commit comments