@@ -5,6 +5,8 @@ import fs from "node:fs";
55import { createRequire } from "node:module" ;
66import path from "node:path" ;
77import { fileURLToPath } from "node:url" ;
8+ import { normalizeControlUiBuildInfo } from "../ui/src/build-info-normalizers.ts" ;
9+ import { resolveBuildIdentityEnvironment } from "./lib/build-identity.mts" ;
810import { assertRealOutputRoot } from "./lib/output-root-guard.mjs" ;
911import { resolvePnpmRunner } from "./pnpm-runner.mts" ;
1012import { buildCmdExeCommandLine , resolveWindowsCmdExePath } from "./windows-cmd-helpers.mjs" ;
@@ -16,6 +18,78 @@ const uiDir = path.join(repoRoot, "ui");
1618const WINDOWS_CMD_EXE_EXTENSIONS = new Set ( [ ".cmd" , ".bat" ] ) ;
1719const FORWARDED_SIGNAL_KILL_GRACE_MS = 250 ;
1820
21+ type UiBuildEnvironmentSources = {
22+ env ?: NodeJS . ProcessEnv ;
23+ now ?: ( ) => Date ;
24+ readBuildInfo ?: ( ) => unknown ;
25+ readGitCommit ?: ( ) => string | null ;
26+ readPackageVersion ?: ( ) => string | null ;
27+ } ;
28+
29+ function readCurrentGitCommit ( ) : string | null {
30+ const result = spawnSync ( "git" , [ "rev-parse" , "HEAD" ] , {
31+ cwd : repoRoot ,
32+ encoding : "utf8" ,
33+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
34+ } ) ;
35+ return result . status === 0 ? result . stdout . trim ( ) : null ;
36+ }
37+
38+ function readCurrentPackageVersion ( ) : string | null {
39+ try {
40+ const parsed = JSON . parse ( fs . readFileSync ( path . join ( repoRoot , "package.json" ) , "utf8" ) ) as {
41+ version ?: unknown ;
42+ } ;
43+ return typeof parsed . version === "string" && parsed . version . trim ( )
44+ ? parsed . version . trim ( )
45+ : null ;
46+ } catch {
47+ return null ;
48+ }
49+ }
50+
51+ function readExistingBuildInfo ( ) : unknown {
52+ try {
53+ return JSON . parse ( fs . readFileSync ( path . join ( repoRoot , "dist/build-info.json" ) , "utf8" ) ) ;
54+ } catch {
55+ return null ;
56+ }
57+ }
58+
59+ /** Reuse a matching runtime identity for a standalone rebuild of the bundled UI. */
60+ export function resolveUiBuildEnvironment (
61+ sources : UiBuildEnvironmentSources = { } ,
62+ ) : NodeJS . ProcessEnv {
63+ const env = sources . env ?? process . env ;
64+ const buildEnv = resolveBuildIdentityEnvironment ( {
65+ commitLabel : "Control UI build commit" ,
66+ env,
67+ now : sources . now ,
68+ readGitCommit : sources . readGitCommit ?? readCurrentGitCommit ,
69+ } ) ;
70+ if ( env . OPENCLAW_BUILD_TIMESTAMP ?. trim ( ) || env . OPENCLAW_CONTROL_UI_BUILD_ID ?. trim ( ) ) {
71+ return buildEnv ;
72+ }
73+
74+ const existing = normalizeControlUiBuildInfo ( ( sources . readBuildInfo ?? readExistingBuildInfo ) ( ) ) ;
75+ const version = ( sources . readPackageVersion ?? readCurrentPackageVersion ) ( ) ;
76+ const release = env . OPENCLAW_CONTROL_UI_RELEASE_BUILD ?. trim ( ) === "1" ;
77+ if (
78+ existing . buildId === "dev" ||
79+ ! existing . builtAt ||
80+ existing . commit !== buildEnv . GIT_COMMIT ||
81+ existing . version !== version ||
82+ existing . release !== release
83+ ) {
84+ return buildEnv ;
85+ }
86+ return {
87+ ...buildEnv ,
88+ OPENCLAW_BUILD_TIMESTAMP : existing . builtAt ,
89+ OPENCLAW_CONTROL_UI_BUILD_ID : existing . buildId ,
90+ } ;
91+ }
92+
1993type UiSpawnCall = {
2094 args : string [ ] ;
2195 command : string ;
@@ -367,13 +441,14 @@ function main(argv: string[] = process.argv.slice(2)): void {
367441 }
368442
369443 if ( action === "build" ) {
444+ const buildEnv = resolveUiBuildEnvironment ( ) ;
370445 const buildCall = noPnpmBuild
371- ? resolveSpawnCall ( process . execPath , [
372- path . join ( repoRoot , "node_modules/vite/bin/vite.js" ) ,
373- " build",
374- ... rest ,
375- ] )
376- : resolvePnpmSpawnCall ( [ "run" , "build" , ...rest ] ) ;
446+ ? resolveSpawnCall (
447+ process . execPath ,
448+ [ path . join ( repoRoot , "node_modules/vite/bin/vite.js" ) , " build", ... rest ] ,
449+ buildEnv ,
450+ )
451+ : resolvePnpmSpawnCall ( [ "run" , "build" , ...rest ] , buildEnv ) ;
377452 runSpawnCallSync ( buildCall , "Control UI build" ) ;
378453 if ( rest . some ( ( arg ) => arg === "--help" || arg === "-h" ) ) {
379454 return ;
@@ -386,7 +461,7 @@ function main(argv: string[] = process.argv.slice(2)): void {
386461 resolveSpawnCall (
387462 process . execPath ,
388463 [ "--import" , "tsx" , path . join ( repoRoot , "scripts" , validator ) ] ,
389- process . env ,
464+ buildEnv ,
390465 { cwd : repoRoot } ,
391466 ) ,
392467 validator ,
0 commit comments