I use ts-morph
to analyse the classes inheritance relationship of a project:
I download this opensource projectantv/x6 for testing:
import { Project } from "ts-morph";
const project = new Project();
project.addDirectoryAtPath("/Users/john/github_repo/@antv/X6/packages/")
const classes = project.getSourceFiles().flatMap((sourceFile) =>
sourceFile.getClasses()
);
const classNames = classes.map((classDecl) => ({
name: classDecl.getName(),
fileName: classDecl.getSourceFile().getFilePath(),
}));
console.log(classNames) // there log `[]`
why it get an empty array ?
Edit-01
I log the classes, it is empty array, but under the packages, it's the source code, it have thousands of classes.
const classes = project.getSourceFiles().flatMap((sourceFile) =>
sourceFile.getClasses()
);
console.log("classes: ", classes) // classes: []
From the ts-morph document
you should use addSourceFilesAtPaths
:
//project.addDirectoryAtPath("/Users/john/github_repo/@antv/X6/packages/")
project.addSourceFilesAtPaths("/Users/john/github_repo/@antv/X6/packages/**/*{.d.ts,.ts}");