Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upgrade): pin Vite to version 5 #561

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading