import { rmdirSync , existsSync , mkdirSync } from 'fs' ; import * as path from 'path' ; import { setReleaseTags } from './setReleaseTags' ; import { Extractor , ExtractorConfig , ExtractorResult , IConfigFile } from '@microsoft/api-extractor' ; import { compileDeclarations } from './compileDeclarations' ; export const build = async ( moduleDirectory , apiExtractorJsonPath , dtsFolder = 'lib' ) => { const entryPoint = 'src/index.ts' ; const basePath = path . resolve ( moduleDirectory , 'tsconfig.json' ) ; const moduleName = path . basename ( path . dirname ( basePath ) ) ; const mainEntryPointFile = path . join ( moduleDirectory , dtsFolder , moduleName , entryPoint ) ; try { compileDeclarations ( basePath , path . join ( path . dirname ( basePath ) , entryPoint ) , dtsFolder ) ; const releaseTag = 'internal' ; await setReleaseTags ( path . dirname ( mainEntryPointFile ) , releaseTag ) ; let cfg = ExtractorConfig . loadFile ( apiExtractorJsonPath ) ; cfg . projectFolder = path . resolve ( moduleDirectory ) ; cfg . mainEntryPointFilePath = `<projectFolder>/${ mainEntryPointFile . replace ( '.ts' , '.d.ts' ) }` ; if ( cfg . apiReport ?. enabled ) { let { reportFolder } = cfg . apiReport ; if ( ! existsSync ( reportFolder ) ) { mkdirSync ( reportFolder ) ; } } const extractorConfig = ExtractorConfig . prepare ( { configObject : cfg , configObjectFullPath : apiExtractorJsonPath , packageJsonFullPath : path . join ( cfg . projectFolder , 'package.json' ) } ) ; const extractorResult = Extractor . invoke ( extractorConfig , { localBuild : true , showVerboseMessages : true } ) ; if ( extractorResult . succeeded ) { console . log ( `API Extractor completed successfully` ) ; process . exitCode = 0 ; } else { console . error ( `API Extractor completed with ${ extractorResult . errorCount } errors` + ` and ${ extractorResult . warningCount } warnings` ) ; process . exitCode = 1 ; } } catch ( e ) { console . error ( e ) ; process . exitCode = 1 ; } finally { console . log ( `Cleanup temporary declaration files` ) ; rmdirSync ( path . join ( moduleDirectory , dtsFolder ) , { recursive : true } ) ; } } ;