import { createRequire } from 'module' ; import { join } from 'path' ; import { existsSync } from 'fs' ; import { OutputFormat , ErrorKind , CheckupError , CheckupLogParser , FormatterOptions , FormatterCtor , normalizePackageName , } from '@checkup/core' ; import { Log } from 'sarif' ; import SummaryFormatter from './summary' ; import JsonFormatter from './json' ; param { FormatterOptions } options param { string } options . cwd param { OutputFormat | string } options . format param { string } options . outputFile return { Formatter } export function getFormatter ( options ) { let mergedOptions = Object . assign ( { } , { format : 'summary' , } , options ) ; let Formatter ; switch ( mergedOptions . format ) { case OutputFormat . summary : { Formatter = SummaryFormatter ; break ; } case OutputFormat . json : { Formatter = JsonFormatter ; break ; } default : { try { let formatterName = existsSync ( join ( options . cwd , options . format ) ) ? options . format : normalizePackageName ( options . format , 'checkup-formatter' ) ; const CustomFormatter = createRequire ( join ( options . cwd , '__placeholder__.js' ) ) ( formatterName ) ; Formatter = CustomFormatter ; break ; } catch { throw new CheckupError ( ErrorKind . FormatterNotFound , { format : mergedOptions . format , validFormats : [ ... Object . values ( OutputFormat ) ] , } ) ; } } } return { format ( log ) { let formatter = new Formatter ( mergedOptions ) ; let logParser = new CheckupLogParser ( log ) ; return formatter . format ( logParser ) ; } , } ; }