import * as ts from "typescript" ; import * as path from "path" ; import { Extractor , GetCompilerOptions , Contracts } from "ts-extractor" ; import { ApiHelpers } from "ts-extractor/dist/internal" ; import { GeneratorConfiguration , WorkingGeneratorConfiguration } from "../contracts/generator-configuration" ; import { Plugin } from "../contracts/plugin" ; import { PluginRegistry } from "../registries/plugin-registry" ; import { DefaultPlugins } from "../default-plugins" ; import { GeneratorHelpers } from "../generator-helpers" ; import { LoggerHelpers } from "../utils/logger" ; export class GeneratorConfigurationBuilder { constructor ( private projectDirectory ) { this . configuration . projectDirectory = projectDirectory ; } private configuration : Partial < WorkingGeneratorConfiguration > = { excludePrivateApi : true } ; private compilerOptions : Partial < ts . CompilerOptions > | undefined ; private tsConfigLocation : string | undefined ; private resolveProjectDirectory ( ) { return this . configuration . projectDirectory || this . projectDirectory ; } private extractorFilterApiItem : Contracts . FilterApiItemsHandler = ( apiItem ) : boolean => { if ( this . configuration . excludePrivateApi ) { const accessModifier = ApiHelpers . ResolveAccessModifierFromModifiers ( apiItem . Declaration . modifiers ) ; if ( accessModifier === Contracts . AccessModifier . Private ) { return false ; } const metadata = apiItem . GetItemMetadata ( ) ; if ( metadata . JSDocTags . findIndex ( x => x . name === GeneratorHelpers . JSDocTags . Private ) !== - 1 ) { return false ; } } return true ; } ; param tsconfigLocation public SetTsConfigLocation ( tsconfigLocation ) { this . tsConfigLocation = tsconfigLocation ; return this ; } public OverrideCompilerOptions ( compilerOptions ) { this . compilerOptions = { ... this . compilerOptions , ... compilerOptions } ; return this ; } param configuration public OverrideConfiguration ( configuration ) { this . configuration = { ... this . configuration , ... configuration } ; return this ; } public SetExternalPackages ( packages ) { this . configuration . externalPackage = packages ; return this ; } public SetOutputDirectory ( outputDirectory ) { this . configuration . outputDirectory = outputDirectory ; return this ; } public AddPlugins ( plugins ) { const currentPlugins = this . configuration . plugins || [ ] ; this . configuration . plugins = [ ... plugins , ... currentPlugins ] ; return this ; } public async Build ( entryFiles ) { if ( this . configuration . verbosity != null ) { LoggerHelpers . SetLogLevel ( this . configuration . verbosity ) ; } const pluginManager = new PluginRegistry ( ) ; for ( const item of DefaultPlugins ) { pluginManager . Register ( item ) ; } if ( this . configuration . plugins != null ) { for ( const plugin of this . configuration . plugins ) { pluginManager . Register ( plugin ) ; } } let compilerOptions = this . compilerOptions ; if ( compilerOptions == null ) { const tsconfigLocation = this . tsConfigLocation || path . join ( this . resolveProjectDirectory ( ) , "tsconfig.json" ) ; compilerOptions = await GetCompilerOptions ( tsconfigLocation ) ; } const extractor = new Extractor ( { CompilerOptions : compilerOptions , ProjectDirectory : this . resolveProjectDirectory ( ) , Exclude : this . configuration . exclude , OutputPathSeparator : this . configuration . outputPathSeparator , ExternalPackages : this . configuration . externalPackage , FilterApiItems : this . extractorFilterApiItem , Verbosity : this . configuration . verbosity } ) ; const outputDirectory = this . configuration . outputDirectory || path . join ( this . resolveProjectDirectory ( ) , "/docs/" ) ; return { PluginManager : pluginManager , ExtractedData : extractor . Extract ( entryFiles ) , OutputDirectory : outputDirectory } ; } }