react-native-navigation的迁移库

gen-docs.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import * as _ from 'lodash';
  2. import * as Typedoc from 'typedoc';
  3. import * as Handlebars from 'handlebars';
  4. import * as fs from 'fs';
  5. const ROOT_DIR = `${__dirname}/..`;
  6. const SRC_DIR = `${ROOT_DIR}/lib/src`;
  7. const DOCS_DIR = `${ROOT_DIR}/docs`;
  8. const TEMPLATES_DIR = `${DOCS_DIR}/templates`;
  9. const API_DIR = `${DOCS_DIR}/api`;
  10. class Main {
  11. public generateApiDocsMarkdown() {
  12. const moduleName = 'Navigation';
  13. const reflections = new ReflectionsReader().read(moduleName);
  14. const classRef = new Parser().parseClass(reflections);
  15. new MarkdownCreator().create(classRef);
  16. }
  17. }
  18. class MarkdownCreator {
  19. private handlebarsFn: HandlebarsTemplateDelegate<any>;
  20. constructor() {
  21. this.handlebarsFn = this.setupHandlebars();
  22. }
  23. public create(context: ClassReflection) {
  24. const result = this.handlebarsFn(context);
  25. // console.log(result);
  26. API_DIR.toString();
  27. }
  28. private setupHandlebars(): HandlebarsTemplateDelegate<any> {
  29. const mainTemplate = readFile(`${TEMPLATES_DIR}/main.hbs`);
  30. const classTemplate = readFile(`${TEMPLATES_DIR}/class.hbs`);
  31. const methodTemplate = readFile(`${TEMPLATES_DIR}/method.hbs`);
  32. Handlebars.registerPartial('class', classTemplate);
  33. Handlebars.registerPartial('method', methodTemplate);
  34. return Handlebars.compile(mainTemplate, { strict: true });
  35. }
  36. }
  37. interface ClassReflection {
  38. name: string;
  39. methods: MethodReflection[];
  40. }
  41. interface MethodReflection {
  42. name: string;
  43. argumentValues: string[];
  44. returnValue: string;
  45. comment: string;
  46. source: string;
  47. }
  48. class Parser {
  49. public parseClass(reflections): ClassReflection {
  50. const theModuleRaw = reflections.children[0];
  51. const theClassRaw = theModuleRaw.children[0];
  52. const methods = this.parseMethods(theClassRaw);
  53. const result = {
  54. name: theClassRaw.name,
  55. methods
  56. };
  57. return result;
  58. }
  59. private parseMethods(theClassRaw): MethodReflection[] {
  60. const methodsRaw = _.filter(theClassRaw.children, (child) => child.kind === Typedoc.ReflectionKind.Method);
  61. methodsRaw.toString();
  62. const result = [{
  63. name: 'theName',
  64. argumentValues: ['asd', 'zxc'],
  65. returnValue: 'returnVal',
  66. comment: 'bla bla bla',
  67. source: 'fromHere'
  68. }];
  69. return result;
  70. }
  71. }
  72. class ReflectionsReader {
  73. private typedocApp: Typedoc.Application;
  74. constructor() {
  75. const tsconfig = JSON.parse(readFile(`${ROOT_DIR}/tsconfig.json`));
  76. this.typedocApp = new Typedoc.Application({
  77. excludeExternals: true,
  78. excludePrivate: true,
  79. excludeProtected: true,
  80. includeDeclarations: true,
  81. module: 'commonjs',
  82. readme: 'none',
  83. target: 'ES6',
  84. ...tsconfig.compilerOptions
  85. });
  86. }
  87. read(moduleName) {
  88. return this.typedocApp.serializer.projectToObject(
  89. this.typedocApp.convert(
  90. this.typedocApp.expandInputFiles(
  91. [`${SRC_DIR}/${moduleName}.ts`]
  92. )
  93. )
  94. );
  95. }
  96. }
  97. function readFile(f) {
  98. return fs.readFileSync(f).toString();
  99. }
  100. new Main().generateApiDocsMarkdown();