@@ -13,6 +13,7 @@ export interface MergeOptions {
1313 backup ?: boolean ; // create .bak file before overwriting
1414 indent ?: number ; // custom indentation (overrides pretty)
1515 arrayMerge ?: "replace" | "concat" ; // array merge strategy (default: replace)
16+ header ?: boolean ; // whether to prepend comment header for JSONC/JSON5 outputs (default: true)
1617}
1718
1819export type MergeResult =
@@ -122,6 +123,7 @@ export function mergeJsonc(opts: MergeOptions): MergeResult {
122123 backup = false ,
123124 indent,
124125 arrayMerge = "replace" ,
126+ header,
125127 } = opts ;
126128
127129 const inputAbs = resolveInputPaths ( inputs , skipMissing ) ;
@@ -136,6 +138,20 @@ export function mergeJsonc(opts: MergeOptions): MergeResult {
136138 const spaces = calculateIndentation ( indent , pretty ) ;
137139 const text = JSON . stringify ( combined , null , spaces ) ;
138140
141+ // For JSONC and JSON5 outputs, prefix a two-line comment header with timestamp
142+ // and list of source files. Use '//' comments which are valid in JSONC/JSON5.
143+ const outExt = outAbs . toLowerCase ( ) . split ( "." ) . pop ( ) ;
144+ let finalText = text ;
145+ const shouldHeader = header !== false ;
146+
147+ if ( shouldHeader && ( outExt === "jsonc" || outExt === "json5" ) ) {
148+ const timestamp = new Date ( ) . toISOString ( ) ;
149+ // Use original input paths (as provided by the caller) for the header
150+ const inputList = inputs . join ( " " ) ;
151+ const header = `// Generated by merge-jsonc on ${ timestamp } \n// Result of combining ${ inputList } \n` ;
152+ finalText = header + text ;
153+ }
154+
139155 if ( ! checkContentChanged ( outAbs , text ) ) {
140156 return { wrote : false , reason : "no_content_change" } ;
141157 }
@@ -148,7 +164,7 @@ export function mergeJsonc(opts: MergeOptions): MergeResult {
148164 return { wrote : false , reason : "dry_run" , preview : text } ;
149165 }
150166
151- const backupPath = atomicWrite ( outAbs , text , backup ) ;
167+ const backupPath = atomicWrite ( outAbs , finalText , backup ) ;
152168
153169 return {
154170 wrote : true ,
0 commit comments