From a2c4196a38e88bcff17489386dbfd48dcbb2e926 Mon Sep 17 00:00:00 2001 From: Julia Wegmayr Date: Wed, 11 Dec 2024 16:38:43 +0100 Subject: [PATCH] add script to update Tooltip imports from @mui/material to @comet/admin --- src/v8/update-import-of-tooltip.ts | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/v8/update-import-of-tooltip.ts diff --git a/src/v8/update-import-of-tooltip.ts b/src/v8/update-import-of-tooltip.ts new file mode 100644 index 0000000..bf25fe9 --- /dev/null +++ b/src/v8/update-import-of-tooltip.ts @@ -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); + } +}