Skip to content

Commit

Permalink
add update script for renaming menu components
Browse files Browse the repository at this point in the history
  • Loading branch information
juliawegmayr committed Dec 30, 2024
1 parent 71a7f32 commit 20a6aeb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/v8/rename-menu-components-in-admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { glob } from "glob";
import { Project, ts } from "ts-morph";

const renameMap: Record<string, string> = {
IMenuContent: "IMainNavigation",
IWithMenu: "IWithMainNavigation",
Menu: "MainNavigation",
MenuClassKey: "MainNavigationClassKey",
MenuCollapsibleItem: "MainNavigationCollapsibleItem",
MenuCollapsibleItemClassKey: "MainNavigationCollapsibleItemClassKey",
MenuCollapsibleItemProps: "MainNavigationCollapsibleItemProps",
MenuContext: "MainNavigationContext",
MenuItem: "MainNavigationItem",
MenuItemAnchorLink: "MainNavigationItemAnchorLink",
MenuItemAnchorLinkProps: "MainNavigationItemAnchorLinkProps",
MenuItemClassKey: "MainNavigationItemClassKey",
MenuItemGroup: "MainNavigationItemGroup",
MenuItemGroupClassKey: "MainNavigationItemGroupClassKey",
MenuItemGroupProps: "MainNavigationItemGroupProps",
MenuItemProps: "MainNavigationItemProps",
MenuItemRouterLink: "MainNavigationItemRouterLink",
MenuItemRouterLinkProps: "MainNavigationItemRouterLinkProps",
MenuProps: "MainNavigationProps",
withMenu: "withMainNavigation",
};

export default async function renameComponents() {
const project = new Project({ tsConfigFilePath: "./admin/tsconfig.json" });
const files: string[] = glob.sync(["admin/src/**/*.tsx"]);

for (const filePath of files) {
const sourceFile = project.getSourceFile(filePath);

if (!sourceFile) {
throw new Error(`Can't get source file for ${filePath}`);
}

const adminImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@comet/admin");

if (adminImport) {
const namedImports = adminImport.getNamedImports();

for (const [oldName, newName] of Object.entries(renameMap)) {
const namedImport = namedImports.find((imported) => imported.getName() === oldName);

if (namedImport) {
namedImport.setName(newName);

const references = sourceFile
.getDescendantsOfKind(ts.SyntaxKind.Identifier)
.filter((identifier) => identifier.getText() === oldName);

for (const reference of references) {
reference.replaceWithText(newName);
}
}
}
}

await sourceFile.save();
}
}

0 comments on commit 20a6aeb

Please sign in to comment.