react-native-navigation的迁移库

ReflectionsReader.ts 1.9KB

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