Skip to content

Commit

Permalink
add script to update Tooltip imports from @mui/material to @comet/admin
Browse files Browse the repository at this point in the history
  • Loading branch information
juliawegmayr committed Dec 11, 2024
1 parent 1a98c5d commit a2c4196
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/v8/update-import-of-tooltip.ts
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);
}
}

0 comments on commit a2c4196

Please sign in to comment.