@@ -43,24 +43,23 @@ export class FileModificationCalculator {
4343 }
4444
4545 async calculate ( modifications : ModifiedResource [ ] ) : Promise < FileModificationResult > {
46- const resultResources = [ ...this . existingConfigs ]
47-
48- if ( this . existingConfigs . length === 0 || ! this . existingFile ) {
49- const newFile = JSON . stringify (
50- modifications
51- . filter ( ( r ) => r . modification === ModificationType . INSERT_OR_UPDATE )
52- . map ( ( r ) => r . resource . raw ) ,
53- null , 2 )
54-
55- return {
56- newFile,
57- diff : this . diff ( '' , newFile ) ,
58- }
59- }
46+ // if (this.existingConfigs.length === 0 || !this.existingFile) {
47+ // const newFile = JSON.stringify(
48+ // modifications
49+ // .filter((r) => r.modification === ModificationType.INSERT_OR_UPDATE)
50+ // .map((r) => r.resource.raw),
51+ // null, 2)
52+ //
53+ // return {
54+ // newFile,
55+ // diff: this.diff('', newFile),
56+ // }
57+ // }
6058
6159 this . validate ( modifications ) ;
6260
63- let newFile = this . existingFile . contents . trimEnd ( ) ;
61+ let newFile = this . existingFile ! . contents . trimEnd ( ) ;
62+ const updateCache = [ ...modifications ] ;
6463
6564 // Reverse the traversal order so we edit from the back. This way the line numbers won't be messed up with new edits.
6665 for ( const existing of this . existingConfigs . reverse ( ) ) {
@@ -70,6 +69,7 @@ export class FileModificationCalculator {
7069 if ( duplicateIndex === - 1 ) {
7170 continue ;
7271 }
72+ updateCache . splice ( duplicateIndex , 1 )
7373
7474 const modified = modifications [ duplicateIndex ] ;
7575 const duplicateSourceKey = existing . sourceMapKey ?. split ( '#' ) . at ( 1 ) ! ;
@@ -82,10 +82,19 @@ export class FileModificationCalculator {
8282 continue ;
8383 }
8484
85+ // Update an existing resource
8586 newFile = this . remove ( newFile , this . sourceMap , sourceIndex ) ;
8687 newFile = this . update ( newFile , modified . resource , this . sourceMap , sourceIndex ) ;
8788 }
8889
90+ // Insert new resources
91+ const newResourcesToInsert = updateCache
92+ . filter ( ( r ) => r . modification === ModificationType . INSERT_OR_UPDATE )
93+ . map ( ( r ) => r . resource )
94+ const insertionIndex = newFile . length - 2 ; // Last element is guarenteed to be the closing bracket. We insert 1 before that
95+
96+ newFile = this . insert ( newFile , newResourcesToInsert , insertionIndex ) ;
97+
8998 return {
9099 newFile : newFile ,
91100 diff : this . diff ( this . existingFile . contents , newFile ) ,
@@ -137,15 +146,20 @@ export class FileModificationCalculator {
137146 }
138147
139148 // Insert always works at the end
140- private insertConfig (
149+ private insert (
141150 file : string ,
142- config : string ,
143- indentString : string ,
144- ) {
145- const configWithIndents = config . split ( / \n / ) . map ( ( l ) => `${ indentString } l` ) . join ( '\n' ) ;
146- const result = file . substring ( 0 , configWithIndents . length - 1 ) + ',' + configWithIndents + file . at ( - 1 ) ;
151+ resources : ResourceConfig [ ] ,
152+ position : number ,
153+ ) : string {
154+ let result = file ;
155+
156+ for ( const newResource of resources . reverse ( ) ) {
157+ let content = JSON . stringify ( newResource . raw , null , 2 ) ;
158+ content = content . split ( / \n / ) . map ( ( l ) => `${ this . indentString } ${ l } ` ) . join ( '\n' )
159+ content = `,\n${ content } ` ;
147160
148- // Need to fix the position of the comma
161+ result = this . splice ( result , position , 0 , content )
162+ }
149163
150164 return result ;
151165 }
@@ -165,7 +179,7 @@ export class FileModificationCalculator {
165179 // Start one later so we leave the previous trailing comma alone
166180 const start = isFirst || isLast ? value ! . position : value ! . position + 1 ;
167181
168- let result = this . r ( file , start , valueEnd ! . position )
182+ let result = this . removeSlice ( file , start , valueEnd ! . position )
169183
170184 // If there's no gap between the remaining elements, we add a space.
171185 if ( ! isFirst && ! / \s / . test ( result [ start ] ) ) {
@@ -224,7 +238,7 @@ export class FileModificationCalculator {
224238 return s . substring ( 0 , start ) + insert + s . substring ( start + deleteCount ) ;
225239 }
226240
227- private r ( s : string , start : number , end : number ) {
241+ private removeSlice ( s : string , start : number , end : number ) {
228242 return s . substring ( 0 , start ) + s . substring ( end ) ;
229243 }
230244
0 commit comments