generate-typescript-docs.ts 3.0 KB

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