import * as core from '@actions/core' ; import * as exec from '@actions/exec' ; import { ExecOptions } from '@actions/exec' ; import * as glob from '@actions/glob' ; import * as io from '@actions/io' ; import * as fs from 'fs' ; import { BASE_BRANCH_DIR , BUILD_DIR } from './const' ; export enum Status { Unknown = 'unknown' , Success = 'success' , Failed = 'failed' } type UnknownStatus = { status : Status . Unknown ; } ; type SuccessStatus = { status : Status . Success ; } ; export type FailedStatus = { status : Status . Failed ; errors : string [ ] ; } ; type BranchStatus = UnknownStatus | SuccessStatus | FailedStatus ; export interface EntryPointStatus { name : string ; file : string ; base : BranchStatus ; head : BranchStatus ; } export interface EntryPoints { [ entryPoint ] : EntryPointStatus ; } param entryPointDir returns async function analyzeEntryPoint ( entryPointDir ) { let errors = [ ] ; const options = { ignoreReturnCode : true , delay : 1000 , } ; options . listeners = { errline : ( line ) => { errors . push ( line ) ; } , } ; const exitCode = await exec . exec ( 'sh' , [ './.github/api-extractor-action/api-extractor.sh' , entryPointDir ] , options ) ; return [ exitCode , errors . filter ( ( line ) => line . startsWith ( 'ERROR: ' ) ) , ] ; } param entryPoints param name param newName function addEntryPoint ( entryPoints , name , newName ) { if ( ! entryPoints [ name ] ) { entryPoints [ name ] = { name : name , head : { status : Status . Unknown } , base : { status : Status . Unknown } , file : `${ newName . split ( '/' ) [ 1 ] }.api.md` , } ; } } param path returns function getDirectoryOfPackageJson ( path ) { return path . substring ( 0 , path . length - `/package.json` . length ) ; } param targetDir async function copyApiExtractorConfig ( targetDir ) { const globber = await glob . create ( '.github/api-extractor-action/api-extractor.json' , { } ) ; const configs = await globber . glob ( ) ; if ( configs . length ) { const apiExtractorConfigPath = ( await globber . glob ( ) ) [ 0 ] ; await io . cp ( apiExtractorConfigPath , targetDir ) ; } else { throw new Error ( 'Could not find api-extractor.json config' ) ; } } param path returns function updateNameInPackageJson ( path ) { const packageContent = JSON . parse ( fs . readFileSync ( path , 'utf-8' ) ) ; const name = packageContent . name ; const newName = name . replace ( /\//g , '_' ) . replace ( /\_/ , '/' ) ; fs . writeFileSync ( path , JSON . stringify ( { ... packageContent , name : newName } , undefined , 2 ) ) ; return { name , newName , } ; } export async function analyzeEntryPoints ( branch , entryPoints ) { const dir = branch === 'head' ? `${ BUILD_DIR }/**/package.json` : `${ BASE_BRANCH_DIR }/${ BUILD_DIR }/**/package.json` ; const globber = await glob . create ( dir , { } ) ; const files = await globber . glob ( ) ; core . info ( `\u001b[35mAPI extractor for ${ branch } branch` ) ; for ( let path of files ) { const { name , newName } = updateNameInPackageJson ( path ) ; addEntryPoint ( entryPoints , name , newName ) ; const directory = getDirectoryOfPackageJson ( path ) ; await copyApiExtractorConfig ( directory ) ; core . startGroup ( `${ name }` ) ; const [ exitCode , errors ] = await analyzeEntryPoint ( directory ) ; if ( exitCode !== 0 ) { entryPoints [ name ] [ branch ] = { status : Status . Failed , errors : errors , } ; } else { entryPoints [ name ] [ branch ] . status = Status . Success ; } core . endGroup ( ) ; } }