Procházet zdrojové kódy

refactor reflectionsReader

Daniel Zlotin před 7 roky
rodič
revize
45c6f3d1d5
2 změnil soubory, kde provedl 28 přidání a 15 odebrání
  1. 2
    9
      scripts/gen-docs/Main.ts
  2. 26
    6
      scripts/gen-docs/ReflectionsReader.ts

+ 2
- 9
scripts/gen-docs/Main.ts Zobrazit soubor

14
   public run() {
14
   public run() {
15
     const classParser = new ClassParser(SOURCE_LINK_PREFIX);
15
     const classParser = new ClassParser(SOURCE_LINK_PREFIX);
16
     const markdownWriter = new MarkdownWriter(TEMPLATES_DIR, OUTPUT_DIR);
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
     markdownWriter.writeClasses(parsedClasses);
20
     markdownWriter.writeClasses(parsedClasses);
28
     markdownWriter.writeMenu(parsedClasses);
21
     markdownWriter.writeMenu(parsedClasses);
29
   }
22
   }

+ 26
- 6
scripts/gen-docs/ReflectionsReader.ts Zobrazit soubor

1
-import * as Typedoc from 'typedoc';
2
-import { OptionsReadMode } from 'typedoc/dist/lib/utils/options';
3
 import * as fs from 'fs';
1
 import * as fs from 'fs';
2
+import { ReflectionKind, ProjectReflection, DeclarationReflection, Application } from 'typedoc';
4
 
3
 
5
 const OPTIONS = {
4
 const OPTIONS = {
6
   excludeExternals: true,
5
   excludeExternals: true,
12
   target: 'ES6'
11
   target: 'ES6'
13
 };
12
 };
14
 
13
 
14
+export interface Reflections {
15
+  classReflections: DeclarationReflection[];
16
+}
17
+
15
 export class ReflectionsReader {
18
 export class ReflectionsReader {
16
-  private typedocApp: Typedoc.Application;
19
+  private typedocApp: Application;
17
 
20
 
18
   constructor(tsconfigPath) {
21
   constructor(tsconfigPath) {
19
     const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath).toString());
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
     const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
28
     const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
25
     const projectReflection = this.typedocApp.convert(expandedFiles);
29
     const projectReflection = this.typedocApp.convert(expandedFiles);
26
     // console.log(JSON.stringify(this.typedocApp.serializer.projectToObject(projectReflection)));
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
 }