import { Contracts , ExtractDto } from "ts-extractor" ; import { LogLevel } from "simplr-logger" ; import { Contracts as MarkdownContracts } from "@simplrjs/markdown" ; import * as path from "path" ; import { ApiItemReference } from "./contracts/api-item-reference" ; import { ApiDefinitionKindAdditional , PluginResultData , Plugin , PluginConstructor } from "./contracts/plugin" ; import { Logger } from "./utils/logger" ; import { SerializedApiDefinitionConstructor , SerializedApiTypeConstructor } from "./contracts/serialized-api-item" ; import { ApiDefinitionList , ApiDefinitions } from "./api-items/api-definition-list" ; import { ApiTypeList , ApiTypes } from "./api-items/api-type-list" ; import { ApiDefinitionDefault } from "./api-items/api-definition-default" ; import { ApiTypeDefault } from "./api-items/api-type-default" ; export namespace GeneratorHelpers { /**
     * Checks if object has `TsDocsGenPlugin` static function.
     */ export function IsPluginClass ( arg ) { return arg . TsDocsGenPlugin != null ; } param file export async function ResolvePlugin ( file ) { const $module = await import ( file ) ; const result = [ ] ; if ( $module == null ) { return [ ] ; } for ( const key in $module ) { if ( $module . hasOwnProperty ( key ) ) { const value = $module [ key ] ; if ( GeneratorHelpers . IsPluginClass ( value ) ) { result . push ( new value ( ) ) ; } } } return result ; } export function SerializeApiDefinition < TKind extends Contracts . ApiBaseDefinition > ( extractedData , apiItem , reference ) { for ( const [ kind , constructorItem ] of ApiDefinitionList ) { if ( kind === apiItem . ApiKind ) { const $constructor = constructorItem ; const serializedApiItem = new $constructor ( extractedData , apiItem , reference ) ; return serializedApiItem ; } } LogWithApiItemPosition ( LogLevel . Warning , apiItem , `"${ apiItem . ApiKind }" is not supported!` ) ; return new ApiDefinitionDefault ( extractedData , apiItem , reference ) ; } export function SerializeApiType ( extractedData , apiType ) { if ( apiType != null ) { for ( const [ kind , constructorItem ] of ApiTypeList ) { if ( kind === apiType . ApiTypeKind ) { const $constructor = constructorItem ; return new $constructor ( extractedData , apiType ) ; } } } LogWithApiItemPosition ( LogLevel . Error , apiType , `"${ apiType . ApiTypeKind }" is not supported` ) ; return new ApiTypeDefault ( extractedData , apiType ) ; } export enum JSDocTags { Private = "private" , Beta = "beta" , Deprecated = "deprecated" , Internal = "internal" , Summary = "summary" } export const MARKDOWN_EXT = ".md" ; export const ApiDefinitionKind = Object . assign ( ApiDefinitionKindAdditional , Contracts . ApiDefinitionKind ) ; export const DEFAULT_CODE_OPTIONS = { lang : "typescript" } ; export const DEFAULT_TABLE_OPTIONS = { removeColumnIfEmpty : true , removeRowIfEmpty : true } ; export function GetApiDefinitionKind ( ) { return Object . assign ( Contracts . ApiDefinitionKind , ApiDefinitionKindAdditional ) ; } export function GetApiItemReferences ( extractedData , itemsReference ) { let overallReferences = [ ] ; for ( const item of itemsReference ) { for ( const referenceId of item . Ids ) { const apiItem = extractedData . Registry [ referenceId ] ; switch ( apiItem . ApiKind ) { case Contracts . ApiDefinitionKind . Export : { if ( apiItem . SourceFileId != null ) { const sourceFileReference = { Alias : apiItem . Name , Ids : [ apiItem . SourceFileId ] } ; const referencesList = GetApiItemReferences ( extractedData , [ sourceFileReference ] ) ; overallReferences = overallReferences . concat ( referencesList ) ; } break ; } case Contracts . ApiDefinitionKind . ImportSpecifier : case Contracts . ApiDefinitionKind . ExportSpecifier : { if ( apiItem . ApiItems == null ) { LogWithApiItemPosition ( LogLevel . Warning , apiItem , `ApiItems are missing in "${ apiItem . Name }"?` ) ; break ; } const apiItemReference = { Alias : apiItem . Name , Ids : apiItem . ApiItems } ; const referencesList = GetApiItemReferences ( extractedData , [ apiItemReference ] ) ; overallReferences = overallReferences . concat ( referencesList ) ; break ; } case Contracts . ApiDefinitionKind . SourceFile : { if ( apiItem . Members == null ) { LogWithApiItemPosition ( LogLevel . Warning , apiItem , "Members are missing" ) ; } const referencesList = GetApiItemReferences ( extractedData , apiItem . Members ) ; overallReferences = overallReferences . concat ( referencesList ) ; break ; } default : { overallReferences . push ( { Id : referenceId , Alias : item . Alias } ) ; } } } } return overallReferences ; } export function LogWithApiItemPosition ( logLevel , apiItem , ... message ) { const { FileName , Line , Character } = apiItem . Location ; const linePrefix = `${ FileName }(${ Line + 1 },${ Character })` ; Logger . Log ( logLevel , `${ linePrefix }: ${ message }` ) ; } export function StandardizePath ( pathString ) { return pathString . split ( path . sep ) . join ( "/" ) ; } export function IsApiItemKind < TKindDto extends Contracts . ApiDefinition > ( itemKind , apiItem ) { return apiItem . ApiKind === itemKind ; } export function MergePluginResultData < T extends PluginResultData > ( a , b ) { if ( b == null ) { return a ; } a . Headings = a . Headings . concat ( b . Headings || [ ] ) ; a . Members = ( a . Members || [ ] ) . concat ( b . Members || [ ] ) ; a . Result = a . Result . concat ( b . Result || [ ] ) ; a . UsedReferences = a . UsedReferences . concat ( b . UsedReferences || [ ] ) ; return a ; } export function GetDefaultPluginResultData < TKind = Contracts . ApiDefinition > ( ) { return { Headings : [ ] , Result : [ ] , UsedReferences : [ ] , Members : [ ] } ; } const TAB_STRING = "    " ; export function Tab ( size = 1 ) { let result = "" ; for ( let i = 0 ; i < size ; i ++ ) { result += TAB_STRING ; } return result ; } export function EnsureFullStop ( sentence , punctuationMark = "." ) { const trimmedSentence = sentence . trim ( ) ; const punctuationMarks = ".!:;,-" ; const lastSymbol = trimmedSentence [ trimmedSentence . length - 1 ] ; if ( punctuationMarks . indexOf ( lastSymbol ) !== - 1 ) { return trimmedSentence ; } return trimmedSentence + punctuationMark ; } }