-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-gridcoldef-import.ts
43 lines (33 loc) · 1.31 KB
/
replace-gridcoldef-import.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { readFile } from "fs/promises";
import { glob } from "glob";
import { Project } from "ts-morph";
/**
* Replaces the import of `GridColDef` from `@mui/x-data-grid*` with `GridColDef` from `@comet/admin`.
*/
export default async function replaceGridColDefImport() {
const files: string[] = glob.sync(["admin/src/**/*.ts", "admin/src/**/*.tsx"]);
const project = new Project({ tsConfigFilePath: "./admin/tsconfig.json" });
for (const filePath of files) {
const fileContent = (await readFile(filePath)).toString();
if (!fileContent.includes("GridColDef")) {
continue;
}
const sourceFile = project.getSourceFile(filePath);
if (!sourceFile) {
throw new Error(`Can't get source file for ${filePath}`);
}
const dataGridImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue().includes("@mui/x-data-grid"));
if (!dataGridImport) {
continue;
}
dataGridImport
.getNamedImports()
.find((namedImport) => namedImport.getText() === "GridColDef")
?.remove();
sourceFile.addImportDeclaration({
namedImports: ["GridColDef"],
moduleSpecifier: "@comet/admin",
});
sourceFile.saveSync();
}
}