-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upgrade script for replacing ExceptionInterceptor with ExceptionF…
…ilter
- Loading branch information
1 parent
92f0a75
commit 2cc5018
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/v7/replace-exception-interceptor-with-exception-filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |