Skip to content

Commit

Permalink
fix(upgrade): pin Vite to version 5 (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilpatrick authored Jan 3, 2025
1 parent 4de8a07 commit 7dc2dd3
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions packages/pages/src/upgrade/pagesUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ const pagesComponentsReplacement = "@yext/pages-components";
const DEPENDENCIES = "dependencies";
const DEV_DEPENDENCIES = "devDependencies";

type Only<T, U> = {
[P in keyof T]: T[P];
} & {
[P in keyof U]?: never;
};

type Either<T, U> = Only<T, U> | Only<U, T>;

type DependencyVersion = Either<SpecificVersion, LatestMajorVersion>;

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
Expand All @@ -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");
Expand All @@ -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;
}
Expand All @@ -71,6 +86,28 @@ export async function updatePackageDependency(
}
}

const getPackageVersion = async (
packageName: string,
version: DependencyVersion | null
): Promise<string | void> => {
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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
Expand Down

0 comments on commit 7dc2dd3

Please sign in to comment.