Skip to content

Commit

Permalink
Add upgrade script for replacing ExceptionInterceptor with ExceptionF…
Browse files Browse the repository at this point in the history
…ilter
  • Loading branch information
thomasdax98 committed Jan 10, 2025
1 parent 92f0a75 commit 2cc5018
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/v7/replace-exception-interceptor-with-exception-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { readFile, writeFile } from "fs/promises";
import { Project } from "ts-morph";

import { formatCode } from "../util/format-code.util";

/**
* 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();

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

const searchString = "app.useGlobalInterceptors\\(new ExceptionInterceptor\\(config.debug\\)\\);";
const re = new RegExp(`^.*${searchString}.*$`, "gm");
fileContent = fileContent.replace(re, "app.useGlobalFilters(new ExceptionFilter(config.debug));");

await writeFile(filePath, await formatCode(fileContent, filePath));

const project = new Project({ tsConfigFilePath: "./api/tsconfig.json" });

const sourceFile = project.getSourceFile(filePath);

if (!sourceFile) {
throw new Error(`Can't get source file for ${filePath}`);
}

sourceFile.addImportDeclaration({
namedImports: ["ExceptionFilter"],
moduleSpecifier: "@comet/cms-api",
});

sourceFile.saveSync();
}

0 comments on commit 2cc5018

Please sign in to comment.