react-native-navigation的迁移库

ReflectionsReader.ts 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. interfaceReflections: DeclarationReflection[];
  15. enumReflections: DeclarationReflection[];
  16. }
  17. export class ReflectionsReader {
  18. private typedocApp: Application;
  19. constructor(tsconfigPath) {
  20. const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath).toString());
  21. this.typedocApp = new Application({ ...OPTIONS, ...tsconfig.compilerOptions });
  22. }
  23. // just class modules, TODO: extract interfaces and types to their own modules, generate docs for interfaces and types
  24. public read(rootPath: string): Reflections {
  25. const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
  26. const projectReflection = this.typedocApp.convert(expandedFiles);
  27. // console.log(JSON.stringify(this.typedocApp.serializer.projectToObject(projectReflection)));
  28. const externalModules = this.externalModulesWithoutTestsAndMocks(projectReflection);
  29. const classReflections = this.reflections(externalModules, ReflectionKind.Class);
  30. const interfaceReflections = this.reflections(externalModules, ReflectionKind.Interface);
  31. const enumReflections = this.reflections(externalModules, ReflectionKind.Enum);
  32. return {
  33. classReflections,
  34. interfaceReflections,
  35. enumReflections
  36. };
  37. }
  38. private externalModulesWithoutTestsAndMocks(projectReflection: ProjectReflection): DeclarationReflection[] {
  39. return projectReflection.getChildrenByKind(ReflectionKind.ExternalModule)
  40. .filter((m) => !m.name.endsWith('.mock"') && !m.name.endsWith('.test"'));
  41. }
  42. private reflections(externalModules: DeclarationReflection[], kind: ReflectionKind): DeclarationReflection[] {
  43. return externalModules.filter((m) => m.getChildrenByKind(kind).length === 1)
  44. .map((m) => m.getChildrenByKind(kind)[0]);
  45. }
  46. }