@@ -134,6 +134,21 @@ function getDepsSignature(deps: PackageDependency[]): string {
134
134
. join ( ',' ) ;
135
135
}
136
136
137
+ function firstIndexOf (
138
+ existingContent : string ,
139
+ depName : string ,
140
+ currentValue : string ,
141
+ position = 0 ,
142
+ ) : number {
143
+ const depIndex = existingContent . indexOf ( depName , position ) ;
144
+ const valIndex = existingContent . indexOf ( currentValue , position ) ;
145
+ const index = depIndex < valIndex ? depIndex : valIndex ;
146
+ if ( index < 0 ) {
147
+ return position === 0 ? - 1 : existingContent . length ;
148
+ }
149
+ return index ;
150
+ }
151
+
137
152
export async function checkBranchDepsMatchBaseDeps (
138
153
upgrade : BranchUpgradeConfig ,
139
154
branchContent : string ,
@@ -218,9 +233,7 @@ export async function doAutoReplace(
218
233
logger . trace ( { depName, replaceString } , 'autoReplace replaceString' ) ;
219
234
let searchIndex : number ;
220
235
if ( replaceWithoutReplaceString ) {
221
- const depIndex = existingContent . indexOf ( depName ! ) ;
222
- const valIndex = existingContent . indexOf ( currentValue ! ) ;
223
- searchIndex = depIndex < valIndex ? depIndex : valIndex ;
236
+ searchIndex = firstIndexOf ( existingContent , depName ! , currentValue ! ) ;
224
237
} else {
225
238
searchIndex = existingContent . indexOf ( replaceString ! ) ;
226
239
}
@@ -317,8 +330,13 @@ export async function doAutoReplace(
317
330
`Found depName at index ${ searchIndex } ` ,
318
331
) ;
319
332
if ( nameReplaced ) {
320
- startIndex += 1 ;
321
- searchIndex = startIndex ;
333
+ startIndex = firstIndexOf (
334
+ existingContent ,
335
+ depName ! ,
336
+ currentValue ! ,
337
+ startIndex + 1 ,
338
+ ) ;
339
+ searchIndex = startIndex - 1 ;
322
340
await writeLocalFile ( upgrade . packageFile ! , existingContent ) ;
323
341
newContent = existingContent ;
324
342
nameReplaced = false ;
@@ -329,6 +347,7 @@ export async function doAutoReplace(
329
347
newContent = replaceAt ( newContent , searchIndex , depName ! , newName ) ;
330
348
await writeLocalFile ( upgrade . packageFile ! , newContent ) ;
331
349
nameReplaced = true ;
350
+ searchIndex += newName . length - 1 ;
332
351
} else if (
333
352
newValue &&
334
353
matchAt ( newContent , searchIndex , currentValue ! )
@@ -337,6 +356,20 @@ export async function doAutoReplace(
337
356
{ packageFile, currentValue } ,
338
357
`Found currentValue at index ${ searchIndex } ` ,
339
358
) ;
359
+ if ( valueReplaced ) {
360
+ startIndex = firstIndexOf (
361
+ existingContent ,
362
+ depName ! ,
363
+ currentValue ! ,
364
+ startIndex + 1 ,
365
+ ) ;
366
+ searchIndex = startIndex - 1 ;
367
+ await writeLocalFile ( upgrade . packageFile ! , existingContent ) ;
368
+ newContent = existingContent ;
369
+ nameReplaced = false ;
370
+ valueReplaced = false ;
371
+ continue ;
372
+ }
340
373
// Now test if the result matches
341
374
newContent = replaceAt (
342
375
newContent ,
@@ -346,11 +379,19 @@ export async function doAutoReplace(
346
379
) ;
347
380
await writeLocalFile ( upgrade . packageFile ! , newContent ) ;
348
381
valueReplaced = true ;
382
+ searchIndex += newValue . length - 1 ;
349
383
}
350
384
if ( nameReplaced && valueReplaced ) {
351
385
if ( await confirmIfDepUpdated ( upgrade , newContent ) ) {
352
386
return newContent ;
353
387
}
388
+ startIndex = firstIndexOf (
389
+ existingContent ,
390
+ depName ! ,
391
+ currentValue ! ,
392
+ startIndex + 1 ,
393
+ ) ;
394
+ searchIndex = startIndex - 1 ;
354
395
await writeLocalFile ( upgrade . packageFile ! , existingContent ) ;
355
396
newContent = existingContent ;
356
397
nameReplaced = false ;
0 commit comments