@@ -12,6 +12,7 @@ import {
1212 getPossibleFlagMetrics ,
1313 type FlagMetric ,
1414} from '../build/src/telemetry/flagUtils.js' ;
15+ import { applyToExisting } from '../build/src/telemetry/toolMetricsUtils.js' ;
1516
1617function writeFlagUsageMetrics ( ) {
1718 const outputPath = path . resolve ( 'src/telemetry/flag_usage_metrics.json' ) ;
@@ -21,15 +22,29 @@ function writeFlagUsageMetrics() {
2122 throw new Error ( `Error: Directory ${ dir } does not exist.` ) ;
2223 }
2324
24- const metrics = getPossibleFlagMetrics ( cliOptions ) ;
25+ let existingMetrics : FlagMetric [ ] = [ ] ;
26+ if ( fs . existsSync ( outputPath ) ) {
27+ try {
28+ existingMetrics = JSON . parse (
29+ fs . readFileSync ( outputPath , 'utf8' ) ,
30+ ) as FlagMetric [ ] ;
31+ } catch {
32+ console . warn (
33+ `Warning: Failed to parse existing metrics from ${ outputPath } . Starting fresh.` ,
34+ ) ;
35+ }
36+ }
2537
26- // Sort metrics by name for deterministic output
27- metrics . sort ( ( a : FlagMetric , b : FlagMetric ) => a . name . localeCompare ( b . name ) ) ;
38+ const newMetrics = getPossibleFlagMetrics ( cliOptions ) ;
39+ const mergedMetrics = applyToExisting < FlagMetric > (
40+ existingMetrics ,
41+ newMetrics ,
42+ ) ;
2843
29- fs . writeFileSync ( outputPath , JSON . stringify ( metrics , null , 2 ) + '\n' ) ;
44+ fs . writeFileSync ( outputPath , JSON . stringify ( mergedMetrics , null , 2 ) + '\n' ) ;
3045
3146 console . log (
32- `Successfully wrote ${ metrics . length } flag usage metrics to ${ outputPath } ` ,
47+ `Successfully wrote ${ mergedMetrics . length } flag usage metrics to ${ outputPath } ` ,
3348 ) ;
3449}
3550
0 commit comments