Skip to content

Commit

Permalink
fix(components): create bundle migration to remove duplicated compone…
Browse files Browse the repository at this point in the history
…nt names

GitOrigin-RevId: 3aee39070f88667b979ef1039526244c07bfea4a
  • Loading branch information
IcaroG authored and actions-user committed Oct 17, 2024
1 parent 1b965d4 commit 80afe94
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
BundleMigrationType,
unbundleSite,
} from "@/wab/server/db/bundle-migration-utils";
import { UnbundledMigrationFn } from "@/wab/server/db/BundleMigrator";
import { Bundler } from "@/wab/shared/bundler";
import { isCodeComponent } from "@/wab/shared/core/components";
import { extractComponentUsages } from "@/wab/shared/core/sites";
import { Component } from "@/wab/shared/model/classes";
import { TplMgr } from "@/wab/shared/TplMgr";

export const migrate: UnbundledMigrationFn = async (bundle, db, entity) => {
const bundler = new Bundler();
const { site, siteOrProjectDep } = await unbundleSite(
bundler,
bundle,
db,
entity
);

const duplicatedComponents = new Map<string, Component[]>();
for (const component of site.components) {
const key = component.name;
// Do not count code components or sub components as duplicates.
if (isCodeComponent(component) || component.superComp) {
continue;
}
if (!duplicatedComponents.has(key)) {
duplicatedComponents.set(key, []);
}
duplicatedComponents.get(key)!.push(component);
}

const tplMgr = new TplMgr({ site });
duplicatedComponents.forEach((components) => {
if (components.length <= 1) {
return;
}
// Sort in descending order. components[0] will have the most usages.
components.sort(
(a, b) =>
extractComponentUsages(site, b).components.length -
extractComponentUsages(site, a).components.length
);
components.forEach((component, index) => {
if (index === 0) {
return;
}
component.name = tplMgr.getUniqueComponentName(component.name);
});
});

const newBundle = bundler.bundle(
siteOrProjectDep,
entity.id,
"236-rename-duplicated-components"
);
Object.assign(bundle, newBundle);
};

export const MIGRATION_TYPE: BundleMigrationType = "unbundled";
3 changes: 2 additions & 1 deletion platform/wab/src/wab/server/db/migrations-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,5 @@ platform/wab/src/wab/server/bundle-migrations/231-add-interaction-variant-for-co
platform/wab/src/wab/server/bundle-migrations/232-remove-viewport-height.ts
platform/wab/src/wab/server/bundle-migrations/233-rename-interaction-variants-for-code-components.ts
platform/wab/src/wab/server/bundle-migrations/234-variant-selectors-no-display-name.ts
platform/wab/src/wab/server/bundle-migrations/235-variant-selectors-no-display-name-corrected.ts
platform/wab/src/wab/server/bundle-migrations/235-variant-selectors-no-display-name-corrected.ts
platform/wab/src/wab/server/bundle-migrations/236-rename-duplicated-components.ts

0 comments on commit 80afe94

Please sign in to comment.