-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add update script for renaming menu components
- Loading branch information
1 parent
71a7f32
commit 20a6aeb
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
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,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(); | ||
} | ||
} |