feat(cli): add git commit hash and build time to --version output

Include commit short hash and formatted build timestamp in the

version string, matching the output format of cs --version.

Signed-off-by: 林凯90331 <90331@sangfor.com>

Co-authored-by: CoStrict <zgsm@sangfor.com.cn>
This commit is contained in:
林凯90331 2026-05-11 20:27:22 +08:00
parent ab2195b5d1
commit e8dca4d3bd
3 changed files with 15 additions and 2 deletions

View File

@ -6,9 +6,17 @@
* corresponding MACRO.* identifier at transpile / bundle time.
*/
export function getMacroDefines(): Record<string, string> {
const { execSync } = require("child_process");
let commit = "unknown";
try {
commit = execSync("git rev-parse --short HEAD", { encoding: "utf-8", cwd: __dirname }).trim();
} catch {
// ignore git errors
}
return {
"MACRO.VERSION": JSON.stringify("4.0.13"),
"MACRO.BUILD_TIME": JSON.stringify(new Date().toISOString()),
"MACRO.COMMIT": JSON.stringify(commit),
"MACRO.FEEDBACK_CHANNEL": JSON.stringify(""),
"MACRO.ISSUES_EXPLAINER": JSON.stringify(""),
"MACRO.NATIVE_PACKAGE_URL": JSON.stringify(""),

View File

@ -8,6 +8,7 @@ if (typeof globalThis.MACRO === 'undefined') {
;(globalThis as any).MACRO = {
VERSION: process.env.CLAUDE_CODE_VERSION || '2.1.888',
BUILD_TIME: new Date().toISOString(),
COMMIT: 'dev',
FEEDBACK_CHANNEL: '',
ISSUES_EXPLAINER: '',
NATIVE_PACKAGE_URL: '',
@ -85,8 +86,11 @@ async function main(): Promise<void> {
args.length === 1 &&
(args[0] === '--version' || args[0] === '-v' || args[0] === '-V')
) {
// MACRO.VERSION is inlined at build time
console.log(`${MACRO.VERSION} (csc)`)
// MACRO.VERSION / MACRO.COMMIT / MACRO.BUILD_TIME are inlined at build time
const d = new Date(MACRO.BUILD_TIME)
const p = (n: number) => String(n).padStart(2, '0')
const buildTime = `${d.getFullYear()}/${p(d.getMonth() + 1)}/${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`
console.log(`${MACRO.VERSION} (commit: ${MACRO.COMMIT}, built: ${buildTime})`)
return
}

View File

@ -9,6 +9,7 @@
declare namespace MACRO {
export const VERSION: string
export const BUILD_TIME: string
export const COMMIT: string
export const FEEDBACK_CHANNEL: string
export const ISSUES_EXPLAINER: string
export const NATIVE_PACKAGE_URL: string