Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to add DialogContent in all existing EditDialogs #61

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/v8/add-dialog-content-to-edit-dialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Node, Project, SyntaxKind, ts } from "ts-morph";

export default async function addDialogContentToEditDialog() {
const project = new Project({ tsConfigFilePath: "./admin/tsconfig.json" });
const sourceFiles = project.getSourceFiles("admin/src/**/*.tsx");

sourceFiles.forEach((sourceFile) => {
const importDeclarations = sourceFile.getImportDeclarations();

const muiImport = importDeclarations.find(
(importDeclaration) => importDeclaration.getModuleSpecifier().getLiteralValue() === "@mui/material",
);

if (!muiImport) {
sourceFile.addImportDeclaration({
moduleSpecifier: "@mui/material",
namedImports: ["DialogContent"],
});
} else if (!muiImport.getNamedImports().some((namedImport) => namedImport.getName() === "DialogContent")) {
muiImport.addNamedImport("DialogContent");
}

const jsxElements = sourceFile.getDescendantsOfKind(SyntaxKind.JsxElement);

const elementsToReplace: { node: Node<ts.Node>; newText: string }[] = [];

jsxElements.forEach((jsxElement) => {
const tagName = jsxElement.getOpeningElement().getTagNameNode().getText();
if (tagName !== "EditDialog") return;

const children = jsxElement.getJsxChildren();
const functionAsChild = children.find((child) => Node.isJsxExpression(child) && Node.isArrowFunction(child.getExpression()));

if (functionAsChild && Node.isJsxExpression(functionAsChild)) {
const expression = functionAsChild.getExpression();
if (!expression || !Node.isArrowFunction(expression)) return;

const body = expression.getBody();
const parameters = expression.getParameters();
let wrappedContent: string;

if (Node.isBlock(body)) {
const returnStatement = body.getStatements().find((statement) => Node.isReturnStatement(statement));
if (!returnStatement || !Node.isReturnStatement(returnStatement)) return;

let returnedExpression = returnStatement.getExpression();
if (returnedExpression && Node.isParenthesizedExpression(returnedExpression)) {
returnedExpression = returnedExpression.getExpression();
}

if (!returnedExpression) return;

if (Node.isJsxFragment(returnedExpression)) {
const fragmentChildren = returnedExpression.getChildrenOfKind(SyntaxKind.JsxElement);
const fragmentText = fragmentChildren.map((child) => child.getText()).join("\n");
wrappedContent = `(<DialogContent>\n${fragmentText}\n</DialogContent>)`;
} else {
const innerContent = returnedExpression.getText().replace(/\n/g, "\n");
wrappedContent = `(<DialogContent>\n${innerContent}\n</DialogContent>)`;
}

const newFunction = `{(${parameters.map((param) => param.getText()).join(", ")}) => {
return ${wrappedContent};}}`;
elementsToReplace.push({ node: functionAsChild, newText: newFunction });
}
} else {
const openEditDialog = jsxElement.getOpeningElement().getText();
const closeEditDialog = jsxElement.getClosingElement().getText();
const childrensContent = children.map((child) => child.getText()).join("\n");

const newContent = `${openEditDialog}\n<DialogContent>\n${childrensContent}\n</DialogContent>\n${closeEditDialog}`;
elementsToReplace.push({ node: jsxElement, newText: newContent });
}
});

elementsToReplace.forEach(({ node, newText }) => {
node.replaceWithText(newText);
});

sourceFile.saveSync();
});

await project.save();
}