186 lines
6.0 KiB
TypeScript
186 lines
6.0 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { existsSync } from "node:fs";
|
|
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: "seal-use";
|
|
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;
|
|
|
|
// Format 1: v0.10.15-2-ga521a24
|
|
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
|
|
};
|
|
}
|
|
|
|
// Format 2: v0.10.15
|
|
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
|
|
};
|
|
}
|
|
|
|
// Format 3: 83ade4d
|
|
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<string, string | undefined>;
|
|
cwd?: string;
|
|
}): VersionInfo {
|
|
const customEnv = options?.env;
|
|
const buildVersion = customEnv?.SEAL_BUILD_VERSION ?? process.env.SEAL_BUILD_VERSION;
|
|
const buildIsRelease = customEnv?.SEAL_BUILD_IS_RELEASE ?? process.env.SEAL_BUILD_IS_RELEASE;
|
|
const buildChannel = customEnv?.SEAL_BUILD_CHANNEL ?? process.env.SEAL_BUILD_CHANNEL;
|
|
const buildGitBase = customEnv?.SEAL_BUILD_GIT_BASE ?? process.env.SEAL_BUILD_GIT_BASE;
|
|
const buildGitCommitsAhead = customEnv?.SEAL_BUILD_GIT_COMMITS_AHEAD ?? process.env.SEAL_BUILD_GIT_COMMITS_AHEAD;
|
|
const buildGitCommit = customEnv?.SEAL_BUILD_GIT_COMMIT ?? process.env.SEAL_BUILD_GIT_COMMIT;
|
|
const buildGitDirty = customEnv?.SEAL_BUILD_GIT_DIRTY ?? process.env.SEAL_BUILD_GIT_DIRTY;
|
|
const buildGitDescribe = customEnv?.SEAL_BUILD_GIT_DESCRIBE ?? process.env.SEAL_BUILD_GIT_DESCRIBE;
|
|
const forceRelease = customEnv?.SEAL_FORCE_RELEASE ?? process.env.SEAL_FORCE_RELEASE;
|
|
// Source invocations may be launched from any directory, including another Git repository.
|
|
// Compiled builds use the injected metadata above; their virtual source root is not a checkout.
|
|
const cwd = options?.cwd ?? resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
|
|
// 1. Check compile-time inlined environment variables
|
|
if (buildVersion) {
|
|
const isRelease = buildIsRelease === "true";
|
|
const channel = (buildChannel as "release" | "dev") || (isRelease ? "release" : "dev");
|
|
const result: VersionInfo = {
|
|
name: "seal-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;
|
|
}
|
|
|
|
// 2. Explicit force release via environment
|
|
if (buildIsRelease === "true" || forceRelease === "1") {
|
|
return {
|
|
name: "seal-use",
|
|
version: packageJson.version,
|
|
channel: "release",
|
|
isRelease: true
|
|
};
|
|
}
|
|
|
|
// 3. Dynamic resolution from git repository in dev mode
|
|
try {
|
|
// A copied npm package has no checkout. Git would otherwise walk up into
|
|
// the consuming application's repository and report its version as ours.
|
|
if (options?.cwd === undefined && !existsSync(join(cwd, ".git"))) {
|
|
return { name: "seal-use", version: `${packageJson.version}-dev.0+local`, channel: "dev", isRelease: false };
|
|
}
|
|
const res = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], {
|
|
cwd,
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "ignore"]
|
|
});
|
|
|
|
if (res.status === 0 && res.stdout) {
|
|
const gitInfo = parseGitDescribe(res.stdout.trim(), packageJson.version);
|
|
if (gitInfo) {
|
|
if (!gitInfo.commit) {
|
|
const revRes = spawnSync("git", ["rev-parse", "--short", "HEAD"], {
|
|
cwd,
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "ignore"]
|
|
});
|
|
if (revRes.status === 0 && revRes.stdout) {
|
|
gitInfo.commit = revRes.stdout.trim();
|
|
}
|
|
}
|
|
|
|
const devVersion = formatDevVersion(
|
|
gitInfo.baseVersion,
|
|
gitInfo.commitsAhead,
|
|
gitInfo.commit,
|
|
gitInfo.dirty
|
|
);
|
|
|
|
return {
|
|
name: "seal-use",
|
|
version: devVersion,
|
|
channel: "dev",
|
|
isRelease: false,
|
|
git: gitInfo
|
|
};
|
|
}
|
|
}
|
|
} catch {
|
|
// Fall back below if git is not installed or error occurs
|
|
}
|
|
|
|
// 4. Default fallback when git is not available
|
|
return {
|
|
name: "seal-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;
|