Skip to content

Commit

Permalink
refactor: auto-replace (#18954)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Nov 17, 2022
1 parent de921cc commit 1c8398a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/workers/repository/update/branch/auto-replace.ts
Expand Up @@ -57,7 +57,7 @@ export async function confirmIfDepUpdated(
);
return false;
}
if (newUpgrade.currentValue !== newValue) {
if (newValue && newUpgrade.currentValue !== newValue) {
logger.debug(
{
manager,
Expand Down Expand Up @@ -163,10 +163,10 @@ export async function doAutoReplace(
newString = compile(autoReplaceStringTemplate, upgrade, false);
} else {
newString = replaceString!;
if (currentValue) {
if (currentValue && newValue) {
newString = newString.replace(
regEx(escapeRegExp(currentValue), 'g'),
newValue!
newValue
);
}
if (currentDigest && newDigest) {
Expand All @@ -180,28 +180,28 @@ export async function doAutoReplace(
{ packageFile, depName },
`Starting search at index ${searchIndex}`
);
let newContent = existingContent;
// Iterate through the rest of the file
for (; searchIndex < existingContent.length; searchIndex += 1) {
for (; searchIndex < newContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(existingContent, searchIndex, replaceString!)) {
logger.debug(
{ packageFile, depName },
`Found match at index ${searchIndex}`
);
// Now test if the result matches
const testContent = replaceAt(
existingContent,
newContent = replaceAt(
newContent,
searchIndex,
replaceString!,
newString
);
await writeLocalFile(upgrade.packageFile!, testContent);

if (await confirmIfDepUpdated(upgrade, testContent)) {
return testContent;
await writeLocalFile(upgrade.packageFile!, newContent);
if (await confirmIfDepUpdated(upgrade, newContent)) {
return newContent;
}
// istanbul ignore next
await writeLocalFile(upgrade.packageFile!, existingContent);
newContent = existingContent;
}
}
} catch (err) /* istanbul ignore next */ {
Expand Down

0 comments on commit 1c8398a

Please sign in to comment.