@@ -4,6 +4,7 @@ import { spawnSync } from "node:child_process";
44import { createHash } from "node:crypto" ;
55import fs from "node:fs" ;
66import path from "node:path" ;
7+ import { performance } from "node:perf_hooks" ;
78import { pathToFileURL } from "node:url" ;
89import { resolvePnpmRunner } from "./pnpm-runner.mjs" ;
910
@@ -398,6 +399,32 @@ export function restoreBuildAllStepCacheOutputs(cacheState, params = {}) {
398399 return true ;
399400}
400401
402+ export function formatBuildAllDuration ( durationMs ) {
403+ const clampedMs = Math . max ( 0 , durationMs ) ;
404+ if ( clampedMs < 1000 ) {
405+ return `${ Math . round ( clampedMs ) } ms` ;
406+ }
407+ if ( clampedMs < 10000 ) {
408+ return `${ ( clampedMs / 1000 ) . toFixed ( 2 ) } s` ;
409+ }
410+ return `${ ( clampedMs / 1000 ) . toFixed ( 1 ) } s` ;
411+ }
412+
413+ export function formatBuildAllTimingSummary ( timings ) {
414+ if ( timings . length === 0 ) {
415+ return "[build-all] phase timings: no phases ran" ;
416+ }
417+ const totalMs = timings . reduce ( ( sum , timing ) => sum + timing . durationMs , 0 ) ;
418+ const phases = timings
419+ . toSorted ( ( left , right ) => right . durationMs - left . durationMs )
420+ . map ( ( timing ) => {
421+ const status = timing . status === "ran" ? "" : ` (${ timing . status } )` ;
422+ return `${ timing . label } ${ status } ${ formatBuildAllDuration ( timing . durationMs ) } ` ;
423+ } )
424+ . join ( "; " ) ;
425+ return `[build-all] phase timings: total ${ formatBuildAllDuration ( totalMs ) } ; slowest ${ phases } ` ;
426+ }
427+
401428function isMainModule ( ) {
402429 const argv1 = process . argv [ 1 ] ;
403430 if ( ! argv1 ) {
@@ -408,23 +435,43 @@ function isMainModule() {
408435
409436if ( isMainModule ( ) ) {
410437 const profile = process . argv [ 2 ] ?? "full" ;
438+ const timings = [ ] ;
439+ let exitCode = 0 ;
411440 for ( const step of resolveBuildAllSteps ( profile ) ) {
441+ const startedAt = performance . now ( ) ;
412442 const cacheState = resolveBuildAllStepCacheState ( step ) ;
413443 if ( process . env . OPENCLAW_BUILD_CACHE !== "0" && cacheState . fresh ) {
414444 restoreBuildAllStepCacheOutputs ( cacheState ) ;
415- console . error ( `[build-all] ${ step . label } (cached)` ) ;
445+ const durationMs = performance . now ( ) - startedAt ;
446+ timings . push ( { label : step . label , status : "cached" , durationMs } ) ;
447+ console . error ( `[build-all] ${ step . label } (cached) ${ formatBuildAllDuration ( durationMs ) } ` ) ;
416448 continue ;
417449 }
418450 console . error ( `[build-all] ${ step . label } ` ) ;
419451 const invocation = resolveBuildAllStep ( step ) ;
420452 const result = spawnSync ( invocation . command , invocation . args , invocation . options ) ;
453+ const durationMs = performance . now ( ) - startedAt ;
421454 if ( typeof result . status === "number" ) {
422455 if ( result . status !== 0 ) {
423- process . exit ( result . status ) ;
456+ timings . push ( { label : step . label , status : "failed" , durationMs } ) ;
457+ console . error (
458+ `[build-all] ${ step . label } failed after ${ formatBuildAllDuration ( durationMs ) } ` ,
459+ ) ;
460+ exitCode = result . status ;
461+ break ;
424462 }
425463 writeBuildAllStepCacheStamp ( step , resolveBuildAllStepCacheState ( step ) ) ;
464+ timings . push ( { label : step . label , status : "ran" , durationMs } ) ;
465+ console . error ( `[build-all] ${ step . label } done in ${ formatBuildAllDuration ( durationMs ) } ` ) ;
426466 continue ;
427467 }
428- process . exit ( 1 ) ;
468+ timings . push ( { label : step . label , status : "failed" , durationMs } ) ;
469+ console . error ( `[build-all] ${ step . label } failed after ${ formatBuildAllDuration ( durationMs ) } ` ) ;
470+ exitCode = 1 ;
471+ break ;
472+ }
473+ console . error ( formatBuildAllTimingSummary ( timings ) ) ;
474+ if ( exitCode !== 0 ) {
475+ process . exit ( exitCode ) ;
429476 }
430477}
0 commit comments