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

Fix script to replace ExceptionInterceptor #63

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
61 changes: 48 additions & 13 deletions src/v7/replace-exception-interceptor-with-exception-filter.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
import { readFile, writeFile } from "fs/promises";

import { formatCode } from "../util/format-code.util";
import { Project, SyntaxKind } from "ts-morph";

/**
* Replaces the old ExceptionInterceptor with the new ExceptionFilter
*/
export default async function replaceExceptionInterceptorWithExceptionFilter() {
const filePath = "api/src/main.ts";
let fileContent = (await readFile(filePath)).toString();
const project = new Project({ tsConfigFilePath: "./api/tsconfig.json" });

const sourceFile = project.getSourceFile("api/src/main.ts");

if (!sourceFile) {
return;
}

const cmsApiImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@comet/cms-api");

if (!fileContent.includes("ExceptionInterceptor")) {
console.log("ExceptionInterceptor not found in main.ts. Make sure that you use the new ExceptionFilter.");
if (!cmsApiImport) {
return;
}

fileContent = fileContent.replace(
"app.useGlobalInterceptors(new ExceptionInterceptor(config.debug));",
"app.useGlobalFilters(new ExceptionFilter(config.debug));",
);
fileContent = fileContent.replace("ExceptionInterceptor", "ExceptionFilter");
const exceptionInterceptorImport = cmsApiImport.getNamedImports().find((namedImport) => namedImport.getName() === "ExceptionInterceptor");

if (!exceptionInterceptorImport) {
return;
}

const bootstrap = sourceFile.getFirstDescendantByKind(SyntaxKind.FunctionDeclaration);

if (!bootstrap) {
return;
}

const useGlobalInterceptors = sourceFile
.getDescendantsOfKind(SyntaxKind.ExpressionStatement)
.find((node) => node.getText().includes("useGlobalInterceptors"));

if (!useGlobalInterceptors) {
return;
}

bootstrap.insertStatements(useGlobalInterceptors.getChildIndex() + 1, "app.useGlobalFilters(new ExceptionFilter(config.debug));");

const exceptionInterceptor = useGlobalInterceptors
.getDescendantsOfKind(SyntaxKind.NewExpression)
.find((node) => node.getText().includes("ExceptionInterceptor"));

if (exceptionInterceptor) {
useGlobalInterceptors.getFirstDescendantByKindOrThrow(SyntaxKind.CallExpression).removeArgument(exceptionInterceptor);

if (useGlobalInterceptors.getDescendantsOfKind(SyntaxKind.NewExpression).length === 0) {
useGlobalInterceptors.remove();
}
}

exceptionInterceptorImport.remove();
cmsApiImport.addNamedImport("ExceptionFilter");

await writeFile(filePath, await formatCode(fileContent, filePath));
await sourceFile.save();
}