diff --git a/platform/wab/src/wab/server/bundle-migrations/236-rename-duplicated-components.ts b/platform/wab/src/wab/server/bundle-migrations/236-rename-duplicated-components.ts new file mode 100644 index 00000000000..fefc6c0973f --- /dev/null +++ b/platform/wab/src/wab/server/bundle-migrations/236-rename-duplicated-components.ts @@ -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(); + 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"; diff --git a/platform/wab/src/wab/server/db/migrations-list.txt b/platform/wab/src/wab/server/db/migrations-list.txt index a10822fe8a1..e2b9405288a 100644 --- a/platform/wab/src/wab/server/db/migrations-list.txt +++ b/platform/wab/src/wab/server/db/migrations-list.txt @@ -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 \ No newline at end of file +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