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: do not move packages that are not shared to catalog #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
52 changes: 39 additions & 13 deletions catalog/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Api } from "@codemod.com/workflow";
import * as semver from "semver";

type PackagesVersions = Record<string, string[]>;
type PackagesVersions = Record<string, PackageUsage>;

type PackageUsage = {
versions: string[];
dependents: string[];
};

type PackageJson = {
name: string;
Expand All @@ -21,6 +26,7 @@ const validRange = (version: string) => {

const readDependencies = (
packagesVersions: PackagesVersions,
packageName: string,
dependencies: Record<string, string> = {},
) => {
for (const [name, version] of Object.entries(dependencies)) {
Expand All @@ -32,11 +38,15 @@ const readDependencies = (
}

if (!packagesVersions[name]) {
packagesVersions[name] = [];
packagesVersions[name] = { versions: [], dependents: [] };
}

if (!packagesVersions[name].includes(version)) {
packagesVersions[name].push(version);
if (!packagesVersions[name].versions.includes(version)) {
packagesVersions[name].versions.push(version);
}

if (!packagesVersions[name].dependents.includes(version)) {
packagesVersions[name].dependents.push(packageName);
}
}
};
Expand All @@ -57,7 +67,7 @@ export async function workflow({ files, dirs, exec }: Api) {
const packagesVersions: PackagesVersions = {};

const packageJsonFiles = dirs({
dirs: [...workspaceConfig.packages, './'],
dirs: [...workspaceConfig.packages, "./"],
ignore: ["**/node_modules/**"],
}).files("package.json");

Expand All @@ -70,16 +80,30 @@ export async function workflow({ files, dirs, exec }: Api) {
return;
}

readDependencies(packagesVersions, packageJson.dependencies);
readDependencies(packagesVersions, packageJson.devDependencies);
readDependencies(packagesVersions, packageJson.optionalDependencies);
readDependencies(
packagesVersions,
packageJson.name,
packageJson.dependencies,
);
readDependencies(
packagesVersions,
packageJson.name,
packageJson.devDependencies,
);
readDependencies(
packagesVersions,
packageJson.name,
packageJson.optionalDependencies,
);
});

const packagesSelected = Object.entries(packagesVersions).filter(
([_, versions]) => versions.length === 1,
([_, { versions, dependents }]) =>
versions.length === 1 && dependents.length > 1,
);
const packagesNotSelected = Object.entries(packagesVersions).filter(
([_, versions]) => versions.length > 1,
([_, { versions, dependents }]) =>
versions.length > 1 || dependents.length <= 1,
);

if (packagesSelected.length === 0) {
Expand All @@ -88,7 +112,10 @@ export async function workflow({ files, dirs, exec }: Api) {
}

const updateCatalog = Object.fromEntries(
packagesSelected.map(([name, versions]) => [name, versions[0] as string]),
packagesSelected.map(([name, { versions }]) => [
name,
versions[0] as string,
]),
);

await workspaceFile.update<{ catalog: Record<string, string> }>(
Expand All @@ -107,7 +134,6 @@ export async function workflow({ files, dirs, exec }: Api) {
);

await packageJsonFiles.json().update<PackageJson>((packageJson) => {

for (const [name] of packagesSelected) {
if (packageJson.dependencies?.[name]) {
packageJson.dependencies[name] = "catalog:";
Expand Down Expand Up @@ -143,7 +169,7 @@ ${packagesSelected.length} packages were safely moved to the catalog.

${
packagesNotSelected.length
? `Packages not moved due to version differences: ${packagesNotSelected.length}
? `Packages not moved: ${packagesNotSelected.length}
`
: ""
}`);
Expand Down