-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(components): create bundle migration to remove duplicated compone…
…nt names GitOrigin-RevId: 3aee39070f88667b979ef1039526244c07bfea4a
- Loading branch information
1 parent
1b965d4
commit 80afe94
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
platform/wab/src/wab/server/bundle-migrations/236-rename-duplicated-components.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters