import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as os from 'os' ; import { Application , TSConfigReader } from 'typedoc' ; import concatMd from 'concat-md' ; import { Logger } from './logger' ; export interface GenerateOptions { projectPath : string ; typescriptExcludes ? : string [ ] ; logger : Logger ; } export interface Docgen { generate : ( options : GenerateOptions ) => Promise < void > ; } export class TscDocgen implements Docgen { public async generate ( options ) { const realTempDir = await fs . realpath ( os . tmpdir ( ) ) ; const tempOutDir = await fs . mkdtemp ( path . join ( realTempDir , 'cdkdx-' ) ) ; await this . runTypedoc ( tempOutDir , options ) ; const api = await concatMd ( tempOutDir , { fileNameAsTitle : true , } ) ; await fs . writeFile ( 'API.md' , api , { encoding : 'utf8' , } ) ; } private async runTypedoc ( outDir , options ) { const app = new Application ( ) ; app . options . addReader ( new TSConfigReader ( ) ) ; app . bootstrap ( { entryPoints : [ path . join ( options . projectPath , 'src' , 'index.ts' ) ] , disableSources : true , readme : 'none' , theme : 'markdown' , plugin : [ 'typedoc-plugin-markdown' ] , exclude : options . typescriptExcludes , logger : ( message , level ) => { if ( level === 3 ) { options . logger . fail ( `${ message }\n` ) ; } } , } ) ; const project = app . convert ( ) ; if ( project ) { await app . generateDocs ( project , outDir ) ; return ; } throw new Error ( 'Docgen execution failed.' ) ; } }