import { spawnSync } from "node:child_process"; import packageJson from "../package.json" with { type: "json" }; export interface GitBuildInfo { baseVersion: string; commitsAhead: number; commit: string; dirty: boolean; describe: string; } export interface VersionInfo { name: "hose-use" | "hose-aipro"; version: string; channel: "release" | "dev"; isRelease: boolean; git?: GitBuildInfo; } export function parseGitDescribe(output: string, fallbackVersion = packageJson.version): GitBuildInfo | null { const trimmed = output.trim(); if (!trimmed) return null; const dirty = trimmed.endsWith("-dirty"); const clean = dirty ? trimmed.slice(0, -6) : trimmed; const longMatch = clean.match(/^v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)-(\d+)-g([0-9a-f]+)$/); if (longMatch && longMatch[1] && longMatch[2] && longMatch[3]) { return { baseVersion: longMatch[1], commitsAhead: Number.parseInt(longMatch[2], 10), commit: longMatch[3], dirty, describe: trimmed }; } const tagMatch = clean.match(/^v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)$/); if (tagMatch && tagMatch[1]) { return { baseVersion: tagMatch[1], commitsAhead: 0, commit: "", dirty, describe: trimmed }; } const commitMatch = clean.match(/^([0-9a-f]{7,40})$/); if (commitMatch && commitMatch[1]) { return { baseVersion: fallbackVersion, commitsAhead: 0, commit: commitMatch[1], dirty, describe: trimmed }; } return null; } export function formatDevVersion(baseVersion: string, commitsAhead: number, commit: string, dirty: boolean): string { const dirtySuffix = dirty ? ".dirty" : ""; const commitSuffix = commit ? `+g${commit}${dirtySuffix}` : dirty ? "+dirty" : ""; return `${baseVersion}-dev.${commitsAhead}${commitSuffix}`; } export function resolveVersionInfo(options?: { env?: Record; cwd?: string }): VersionInfo { const customEnv = options?.env; // Keep these as direct property accesses: Bun can inline them into compiled binaries. const buildVersion = customEnv?.HOSE_BUILD_VERSION ?? process.env.HOSE_BUILD_VERSION; const buildIsRelease = customEnv?.HOSE_BUILD_IS_RELEASE ?? process.env.HOSE_BUILD_IS_RELEASE; const buildChannel = customEnv?.HOSE_BUILD_CHANNEL ?? process.env.HOSE_BUILD_CHANNEL; const buildGitBase = customEnv?.HOSE_BUILD_GIT_BASE ?? process.env.HOSE_BUILD_GIT_BASE; const buildGitCommitsAhead = customEnv?.HOSE_BUILD_GIT_COMMITS_AHEAD ?? process.env.HOSE_BUILD_GIT_COMMITS_AHEAD; const buildGitCommit = customEnv?.HOSE_BUILD_GIT_COMMIT ?? process.env.HOSE_BUILD_GIT_COMMIT; const buildGitDirty = customEnv?.HOSE_BUILD_GIT_DIRTY ?? process.env.HOSE_BUILD_GIT_DIRTY; const buildGitDescribe = customEnv?.HOSE_BUILD_GIT_DESCRIBE ?? process.env.HOSE_BUILD_GIT_DESCRIBE; const forceRelease = customEnv?.HOSE_FORCE_RELEASE ?? process.env.HOSE_FORCE_RELEASE; if (buildVersion) { const isRelease = buildIsRelease === "true"; const channel = (buildChannel as "release" | "dev") || (isRelease ? "release" : "dev"); const result: VersionInfo = { name: "hose-use", version: buildVersion, channel, isRelease }; if (buildGitBase) { result.git = { baseVersion: buildGitBase, commitsAhead: Number.parseInt(buildGitCommitsAhead || "0", 10), commit: buildGitCommit || "", dirty: buildGitDirty === "true", describe: buildGitDescribe || buildVersion, }; } return result; } if (buildIsRelease === "true" || forceRelease === "1") { return { name: "hose-use", version: packageJson.version, channel: "release", isRelease: true }; } try { const result = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], { cwd: options?.cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], }); if (result.status === 0 && result.stdout) { const gitInfo = parseGitDescribe(result.stdout.trim(), packageJson.version); if (gitInfo) { if (!gitInfo.commit) { const revision = spawnSync("git", ["rev-parse", "--short", "HEAD"], { cwd: options?.cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], }); if (revision.status === 0 && revision.stdout) gitInfo.commit = revision.stdout.trim(); } return { name: "hose-use", version: formatDevVersion(gitInfo.baseVersion, gitInfo.commitsAhead, gitInfo.commit, gitInfo.dirty), channel: "dev", isRelease: false, git: gitInfo, }; } } } catch { // Fall back below if git is unavailable. } return { name: "hose-use", version: `${packageJson.version}-dev.0+local`, channel: "dev", isRelease: false }; } export const VERSION_INFO = resolveVersionInfo(); export const VERSION = VERSION_INFO.version; export const IS_RELEASE = VERSION_INFO.isRelease; export const CHANNEL = VERSION_INFO.channel; export function getVersionInfo(name: VersionInfo["name"]): VersionInfo { return { ...VERSION_INFO, name }; } export function formatVersionLabel(name: VersionInfo["name"]): string { return `${name} v${VERSION}${CHANNEL === "dev" ? " [DEV BUILD]" : ""}`; }