-
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 script to update Tooltip imports from @mui/material to @comet/admin
- Loading branch information
1 parent
1a98c5d
commit a2c4196
Showing
1 changed file
with
55 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,55 @@ | ||
import { readFile, writeFile } from "fs/promises"; | ||
import { glob } from "glob"; | ||
import { Project } from "ts-morph"; | ||
|
||
export default async function updateImportOfTooltip() { | ||
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().includes("@comet/admin")); | ||
const adminImports = adminImport?.getNamedImports().map((namedImport) => namedImport.getText()); | ||
|
||
if (adminImports) { | ||
if (adminImports.includes("Tooltip")) { | ||
continue; | ||
} | ||
} | ||
|
||
const muiTooltipImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@mui/material"); | ||
|
||
if (!muiTooltipImport) continue; | ||
|
||
const namedImports = muiTooltipImport.getNamedImports(); | ||
const tooltipImport = namedImports.find((namedImport) => namedImport.getText() === "Tooltip"); | ||
|
||
if (tooltipImport) { | ||
tooltipImport.remove(); | ||
} | ||
|
||
if (muiTooltipImport.getNamedImports().length === 0) { | ||
muiTooltipImport.remove(); | ||
} | ||
|
||
if (adminImport) { | ||
adminImport.addNamedImports(["Tooltip"]); | ||
} else { | ||
sourceFile.addImportDeclaration({ | ||
namedImports: ["Tooltip"], | ||
moduleSpecifier: "@comet/admin", | ||
}); | ||
} | ||
|
||
await sourceFile.save(); | ||
|
||
const fileContent = (await readFile(filePath)).toString(); | ||
|
||
await writeFile(filePath, fileContent); | ||
} | ||
} |