forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionsReader.ts
54 lines (45 loc) · 1.95 KB
/
ReflectionsReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import * as fs from 'fs';
import { ReflectionKind, ProjectReflection, DeclarationReflection, Application } from 'typedoc';
const OPTIONS = {
excludeExternals: true,
excludePrivate: true,
includeDeclarations: true,
mode: 'modules',
module: 'commonjs',
readme: 'none',
target: 'ES6'
};
export interface Reflections {
classReflections: DeclarationReflection[];
interfaceReflections: DeclarationReflection[];
enumReflections: DeclarationReflection[];
}
export class ReflectionsReader {
private typedocApp: Application;
constructor(tsconfigPath) {
const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath).toString());
this.typedocApp = new Application({ ...OPTIONS, ...tsconfig.compilerOptions });
}
public read(rootPath: string): Reflections {
const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
const projectReflection = this.typedocApp.convert(expandedFiles);
// console.log(JSON.stringify(this.typedocApp.serializer.projectToObject(projectReflection)));
const externalModules = this.externalModulesWithoutTestsAndMocks(projectReflection);
const classReflections = this.reflections(externalModules, ReflectionKind.Class);
const interfaceReflections = this.reflections(externalModules, ReflectionKind.Interface);
const enumReflections = this.reflections(externalModules, ReflectionKind.Enum);
return {
classReflections,
interfaceReflections,
enumReflections
};
}
private externalModulesWithoutTestsAndMocks(projectReflection: ProjectReflection): DeclarationReflection[] {
return projectReflection.getChildrenByKind(ReflectionKind.ExternalModule)
.filter((m) => !m.name.endsWith('.mock"') && !m.name.endsWith('.test"'));
}
private reflections(externalModules: DeclarationReflection[], kind: ReflectionKind): DeclarationReflection[] {
return externalModules.filter((m) => m.getChildrenByKind(kind).length === 1)
.map((m) => m.getChildrenByKind(kind)[0]);
}
}