Last active
August 4, 2022 16:20
-
-
Save thatcatcancode/681deac94d641d7e1fad309cf924c5a4 to your computer and use it in GitHub Desktop.
An example of a morph that generates interfaces from classes.
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
import { Project } from 'ts-morph' | |
/** | |
* Get the scope of the NestJS project by specifying its tsconfig. | |
*/ | |
const project = new Project({ | |
tsConfigFilePath: 'tsconfig.json', | |
}) | |
/** | |
* Get the types pkg file you created to house the new types. | |
*/ | |
const typePkgFile = project.getSourceFile('./types-pkg/index.ts') | |
if (!typePkgFile) throw Error('Cannot find type package file.') | |
/** | |
* Extract interfaces from class models and add to the types pkg. | |
*/ | |
const modelFiles = project.getSourceFiles(['./src/**/*.model.ts']) | |
modelFiles.forEach((file) => { | |
const classes = file.getClasses() | |
classes?.forEach((c) => { | |
const i = c.extractInterface() | |
typePkgFile.addInterface(i) | |
const newInterface = typePkgFile.getInterface(i.name) | |
if (!newInterface) return | |
newInterface.setIsExported(true) | |
}) | |
}) | |
project.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment