react-native-navigation的迁移库

ReflectionsReader.ts 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. 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 interfaceReflections = this.reflections(externalModules, ReflectionKind.Interface);
  30. const enumReflections = this.reflections(externalModules, ReflectionKind.Enum);
  31. return {
  32. classReflections,
  33. interfaceReflections,
  34. enumReflections
  35. };
  36. }
  37. private externalModulesWithoutTestsAndMocks(projectReflection: ProjectReflection): DeclarationReflection[] {
  38. return projectReflection.getChildrenByKind(ReflectionKind.ExternalModule)
  39. .filter((m) => !m.name.endsWith('.mock"') && !m.name.endsWith('.test"'));
  40. }
  41. private reflections(externalModules: DeclarationReflection[], kind: ReflectionKind): DeclarationReflection[] {
  42. return externalModules.filter((m) => m.getChildrenByKind(kind).length === 1)
  43. .map((m) => m.getChildrenByKind(kind)[0]);
  44. }
  45. }