react-native-navigation的迁移库

ReflectionsReader.ts 1.8KB

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