Преглед изворни кода

refactor reflectionsReader

Daniel Zlotin пре 6 година
родитељ
комит
45c6f3d1d5
2 измењених фајлова са 28 додато и 15 уклоњено
  1. 2
    9
      scripts/gen-docs/Main.ts
  2. 26
    6
      scripts/gen-docs/ReflectionsReader.ts

+ 2
- 9
scripts/gen-docs/Main.ts Прегледај датотеку

@@ -14,16 +14,9 @@ class Main {
14 14
   public run() {
15 15
     const classParser = new ClassParser(SOURCE_LINK_PREFIX);
16 16
     const markdownWriter = new MarkdownWriter(TEMPLATES_DIR, OUTPUT_DIR);
17
-    const projectReflections = new ReflectionsReader(TSCONFIG_PATH).read(INPUT_DIR);
17
+    const reflections = new ReflectionsReader(TSCONFIG_PATH).read(INPUT_DIR);
18 18
 
19
-    const externalModules = projectReflections.getChildrenByKind(ReflectionKind.ExternalModule)
20
-      .filter((m) => !m.name.endsWith('.mock"') && !m.name.endsWith('.test"'));
21
-
22
-    const classReflections = externalModules.filter((m) => m.getChildrenByKind(ReflectionKind.Class).length === 1)
23
-      .map((m) => m.getChildrenByKind(ReflectionKind.Class)[0]);
24
-    // just class modules, TODO: extract interfaces and types to their own modules, generate docs for interfaces and types
25
-
26
-    const parsedClasses = classReflections.map((c) => classParser.parseClass(c));
19
+    const parsedClasses = reflections.classReflections.map((c) => classParser.parseClass(c));
27 20
     markdownWriter.writeClasses(parsedClasses);
28 21
     markdownWriter.writeMenu(parsedClasses);
29 22
   }

+ 26
- 6
scripts/gen-docs/ReflectionsReader.ts Прегледај датотеку

@@ -1,6 +1,5 @@
1
-import * as Typedoc from 'typedoc';
2
-import { OptionsReadMode } from 'typedoc/dist/lib/utils/options';
3 1
 import * as fs from 'fs';
2
+import { ReflectionKind, ProjectReflection, DeclarationReflection, Application } from 'typedoc';
4 3
 
5 4
 const OPTIONS = {
6 5
   excludeExternals: true,
@@ -12,18 +11,39 @@ const OPTIONS = {
12 11
   target: 'ES6'
13 12
 };
14 13
 
14
+export interface Reflections {
15
+  classReflections: DeclarationReflection[];
16
+}
17
+
15 18
 export class ReflectionsReader {
16
-  private typedocApp: Typedoc.Application;
19
+  private typedocApp: Application;
17 20
 
18 21
   constructor(tsconfigPath) {
19 22
     const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath).toString());
20
-    this.typedocApp = new Typedoc.Application({ ...OPTIONS, ...tsconfig.compilerOptions });
23
+    this.typedocApp = new Application({ ...OPTIONS, ...tsconfig.compilerOptions });
21 24
   }
22 25
 
23
-  public read(rootPath: string): Typedoc.ProjectReflection {
26
+  // just class modules, TODO: extract interfaces and types to their own modules, generate docs for interfaces and types
27
+  public read(rootPath: string): Reflections {
24 28
     const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
25 29
     const projectReflection = this.typedocApp.convert(expandedFiles);
26 30
     // console.log(JSON.stringify(this.typedocApp.serializer.projectToObject(projectReflection)));
27
-    return projectReflection;
31
+
32
+    const externalModules = this.externalModulesWithoutTestsAndMocks(projectReflection);
33
+    const classReflections = this.classReflections(externalModules);
34
+
35
+    return {
36
+      classReflections
37
+    };
38
+  }
39
+
40
+  private externalModulesWithoutTestsAndMocks(projectReflection: ProjectReflection): DeclarationReflection[] {
41
+    return projectReflection.getChildrenByKind(ReflectionKind.ExternalModule)
42
+      .filter((m) => !m.name.endsWith('.mock"') && !m.name.endsWith('.test"'));
43
+  }
44
+
45
+  private classReflections(externalModules: DeclarationReflection[]): DeclarationReflection[] {
46
+    return externalModules.filter((m) => m.getChildrenByKind(ReflectionKind.Class).length === 1)
47
+      .map((m) => m.getChildrenByKind(ReflectionKind.Class)[0]);
28 48
   }
29 49
 }