From 090530b197e72412a31c4196051f8a1f069a8120 Mon Sep 17 00:00:00 2001 From: Duval Kilpatrick Date: Thu, 2 Jan 2025 15:59:51 -0500 Subject: [PATCH] fix(upgrade): pin Vite to version 5 --- packages/pages/src/upgrade/pagesUpdater.ts | 59 ++++++++++++++++++---- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/packages/pages/src/upgrade/pagesUpdater.ts b/packages/pages/src/upgrade/pagesUpdater.ts index c18fe147..751270b5 100644 --- a/packages/pages/src/upgrade/pagesUpdater.ts +++ b/packages/pages/src/upgrade/pagesUpdater.ts @@ -18,6 +18,24 @@ const pagesComponentsReplacement = "@yext/pages-components"; const DEPENDENCIES = "dependencies"; const DEV_DEPENDENCIES = "devDependencies"; +type Only = { + [P in keyof T]: T[P]; +} & { + [P in keyof U]?: never; +}; + +type Either = Only | Only; + +type DependencyVersion = Either; + +type SpecificVersion = { + specificVersion: string; +}; + +type LatestMajorVersion = { + latestMajorVersion: string; +}; + /** * Helper function to update a package dependency in package.json * @param packageName name of the package to update @@ -26,7 +44,7 @@ const DEV_DEPENDENCIES = "devDependencies"; */ export async function updatePackageDependency( packageName: string, - version: string | null, + version: DependencyVersion | null, install: boolean = false ) { const packagePath = path.resolve("package.json"); @@ -46,15 +64,12 @@ export async function updatePackageDependency( if (!install && !currentVersion) { return; } - let toVersion = version || (await latestVersion(packageName)); + const toVersion = await getPackageVersion(packageName, version); if (!toVersion) { console.error(`Failed to get version for ${packageName}`); return; } - // if getting the latest version, add a caret - if (!version && toVersion.charAt(0) !== "^") { - toVersion = "^" + toVersion; - } + if (currentVersion === toVersion) { return; } @@ -71,6 +86,28 @@ export async function updatePackageDependency( } } +const getPackageVersion = async ( + packageName: string, + version: DependencyVersion | null +): Promise => { + if (!version) { + return "^" + (await latestVersion(packageName)); + } + + if (version.specificVersion) { + return version.specificVersion; + } + + if (version.latestMajorVersion) { + return ( + "^" + + (await latestVersion(packageName, { + version: version.latestMajorVersion, + })) + ); + } +}; + /** * Helper function to remove a package dependency in package.json * @param root folder that contains package.json @@ -196,7 +233,9 @@ export const updatePagesJSToCurrentVersion = async () => { "package.json" ); const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf-8")); - await updatePackageDependency("@yext/pages", packageJson.version); + await updatePackageDependency("@yext/pages", { + specificVersion: packageJson.version as string, + }); } catch (e) { console.error("Failed to upgrade pages version: ", (e as Error).message); process.exit(1); @@ -207,8 +246,10 @@ export const updatePagesJSToCurrentVersion = async () => { * Updates vitejs/plugin-react and vite to specified versions */ export const updateDevDependencies = async () => { - await updatePackageDependency("@vitejs/plugin-react", null); - await updatePackageDependency("vite", null); + await updatePackageDependency("@vitejs/plugin-react", { + latestMajorVersion: "4", + }); + await updatePackageDependency("vite", { latestMajorVersion: "5" }); await updatePackageDependency("@yext/search-headless-react", null); await updatePackageDependency("@yext/search-ui-react", null); };