Skip to content
Snippets Groups Projects
Unverified Commit 449bc3ec authored by Jamie Magee's avatar Jamie Magee Committed by GitHub
Browse files

refactor(npm): extract replacement to a private function (#11195)

parent b0cc6080
No related branches found
No related tags found
No related merge requests found
import { dequal } from 'dequal'; import { dequal } from 'dequal';
import type { PackageJson } from 'type-fest';
import { logger } from '../../../../logger'; import { logger } from '../../../../logger';
import { matchAt, replaceAt } from '../../../../util/string'; import { matchAt, replaceAt } from '../../../../util/string';
import type { UpdateDependencyConfig } from '../../../types'; import type { UpdateDependencyConfig } from '../../../types';
function replaceAsString(
parsedContents: PackageJson,
fileContent: string,
depType: string,
depName: string,
oldVersion: string,
newValue: string
): string | null {
// Update the file = this is what we want
// eslint-disable-next-line no-param-reassign
parsedContents[depType][depName] = newValue;
// Look for the old version number
const searchString = `"${oldVersion}"`;
const newString = `"${newValue}"`;
// Skip ahead to depType section
let searchIndex = fileContent.indexOf(`"${depType}"`) + depType.length;
logger.trace(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file
for (; searchIndex < fileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(fileContent, searchIndex, searchString)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
fileContent,
searchIndex,
searchString,
newString
);
// Compare the parsed JSON structure of old and new
if (dequal(parsedContents, JSON.parse(testContent))) {
return testContent;
}
}
}
// istanbul ignore next
throw new Error();
}
export function updateDependency({ export function updateDependency({
fileContent, fileContent,
upgrade, upgrade,
...@@ -30,41 +70,21 @@ export function updateDependency({ ...@@ -30,41 +70,21 @@ export function updateDependency({
} }
logger.debug(`npm.updateDependency(): ${depType}.${depName} = ${newValue}`); logger.debug(`npm.updateDependency(): ${depType}.${depName} = ${newValue}`);
try { try {
const parsedContents = JSON.parse(fileContent); const parsedContents: PackageJson = JSON.parse(fileContent);
// Save the old version // Save the old version
const oldVersion: string = parsedContents[depType][depName]; const oldVersion: string = parsedContents[depType][depName];
if (oldVersion === newValue) { if (oldVersion === newValue) {
logger.trace('Version is already updated'); logger.trace('Version is already updated');
return fileContent; return fileContent;
} }
// Update the file = this is what we want let newFileContent = replaceAsString(
parsedContents[depType][depName] = newValue; parsedContents,
// Look for the old version number
const searchString = `"${oldVersion}"`;
const newString = `"${newValue}"`;
let newFileContent = null;
// Skip ahead to depType section
let searchIndex = fileContent.indexOf(`"${depType}"`) + depType.length;
logger.trace(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file
for (; searchIndex < fileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(fileContent, searchIndex, searchString)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
fileContent, fileContent,
searchIndex, depType,
searchString, depName,
newString oldVersion,
newValue
); );
// Compare the parsed JSON structure of old and new
if (dequal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
}
}
// istanbul ignore if // istanbul ignore if
if (!newFileContent) { if (!newFileContent) {
logger.debug( logger.debug(
...@@ -93,33 +113,14 @@ export function updateDependency({ ...@@ -93,33 +113,14 @@ export function updateDependency({
'Upgraded dependency exists in yarn resolutions but is different version' 'Upgraded dependency exists in yarn resolutions but is different version'
); );
} }
// Look for the old version number newFileContent = replaceAsString(
const oldResolution = `"${String(parsedContents.resolutions[depKey])}"`; parsedContents,
const newResolution = `"${newValue}"`;
// Update the file = this is what we want
parsedContents.resolutions[depKey] = newValue;
// Skip ahead to depType section
searchIndex = newFileContent.indexOf(`"resolutions"`);
logger.trace(`Starting search at index ${searchIndex}`);
// Iterate through the rest of the file
for (; searchIndex < newFileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(newFileContent, searchIndex, oldResolution)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
newFileContent, newFileContent,
searchIndex, 'resolutions',
oldResolution, depKey,
newResolution parsedContents.resolutions[depKey],
newValue
); );
// Compare the parsed JSON structure of old and new
if (dequal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
}
}
} }
} }
return newFileContent; return newFileContent;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment