generate-typescript-docs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* tslint:disable:no-console */
  2. import fs from 'fs-extra';
  3. import klawSync from 'klaw-sync';
  4. import path, { extname } from 'path';
  5. import { deleteGeneratedDocs } from './docgen-utils';
  6. import { TypeMap } from './typescript-docgen-types';
  7. import { TypescriptDocsParser } from './typescript-docs-parser';
  8. import { TypescriptDocsRenderer } from './typescript-docs-renderer';
  9. interface DocsSectionConfig {
  10. sourceDirs: string[];
  11. outputPath: string;
  12. }
  13. const sections: DocsSectionConfig[] = [
  14. {
  15. sourceDirs: ['packages/core/src/', 'packages/common/src/'],
  16. outputPath: 'typescript-api',
  17. },
  18. {
  19. sourceDirs: ['packages/asset-server-plugin/src/'],
  20. outputPath: 'plugins',
  21. },
  22. {
  23. sourceDirs: ['packages/email-plugin/src/'],
  24. outputPath: 'plugins',
  25. },
  26. {
  27. sourceDirs: ['packages/admin-ui-plugin/src/'],
  28. outputPath: 'plugins',
  29. },
  30. {
  31. sourceDirs: ['packages/elasticsearch-plugin/src/'],
  32. outputPath: 'plugins',
  33. },
  34. ];
  35. generateTypescriptDocs(sections);
  36. const watchMode = !!process.argv.find(arg => arg === '--watch' || arg === '-w');
  37. if (watchMode) {
  38. console.log(`Watching for changes to source files...`);
  39. sections.forEach(section => {
  40. section.sourceDirs.forEach(dir => {
  41. fs.watch(dir, { recursive: true }, (eventType, file) => {
  42. if (extname(file) === '.ts') {
  43. console.log(`Changes detected in ${dir}`);
  44. generateTypescriptDocs([section], true);
  45. }
  46. });
  47. });
  48. });
  49. }
  50. /**
  51. * Uses the TypeScript compiler API to parse the given files and extract out the documentation
  52. * into markdown files
  53. */
  54. function generateTypescriptDocs(config: DocsSectionConfig[], isWatchMode: boolean = false) {
  55. const timeStart = +new Date();
  56. // This map is used to cache types and their corresponding Hugo path. It is used to enable
  57. // hyperlinking from a member's "type" to the definition of that type.
  58. const globalTypeMap: TypeMap = new Map();
  59. if (!isWatchMode) {
  60. for (const {outputPath, sourceDirs} of config) {
  61. deleteGeneratedDocs(absOutputPath(outputPath));
  62. }
  63. }
  64. for (const { outputPath, sourceDirs } of config) {
  65. const sourceFilePaths = getSourceFilePaths(sourceDirs);
  66. const parsedDeclarations = new TypescriptDocsParser().parse(sourceFilePaths);
  67. for (const info of parsedDeclarations) {
  68. const { category, fileName } = info;
  69. const pathToTypeDoc = `${outputPath}/${category ? category + '/' : ''}${fileName === '_index' ? '' : fileName}`;
  70. globalTypeMap.set(info.title, pathToTypeDoc);
  71. }
  72. const docsUrl = `/docs`;
  73. const generatedCount = new TypescriptDocsRenderer().render(
  74. parsedDeclarations,
  75. docsUrl,
  76. absOutputPath(outputPath),
  77. globalTypeMap,
  78. );
  79. if (generatedCount) {
  80. console.log(`Generated ${generatedCount} typescript api docs for "${outputPath}" in ${+new Date() - timeStart}ms`);
  81. }
  82. }
  83. }
  84. function absOutputPath(outputPath: string): string {
  85. return path.join(__dirname, '../../docs/content/docs/', outputPath);
  86. }
  87. function getSourceFilePaths(sourceDirs: string[]): string[] {
  88. return sourceDirs
  89. .map(scanPath =>
  90. klawSync(path.join(__dirname, '../../', scanPath), {
  91. nodir: true,
  92. filter: item => path.extname(item.path) === '.ts',
  93. traverseAll: true,
  94. }),
  95. )
  96. .reduce((allFiles, files) => [...allFiles, ...files], [])
  97. .map(item => item.path);
  98. }