import { EnvMissingError } from './errors' ; import { ReporterOptions } from './types' ; type ExtraOptions < T > = { onError ? : ( errors : Partial < Record < keyof T , Error > > ) => void ; logger : ( output : string ) => void ; } ; const defaultLogger = console . error . bind ( console ) ; const isNode = ! ! ( typeof process === 'object' && process ?. versions ?. node ) ; const colorWith = ( colorCode ) => ( str ) => isNode ? `\x1b[${ colorCode }m${ str }\x1b[0m` : str ; const colors = { blue : colorWith ( '34' ) , white : colorWith ( '37' ) , yellow : colorWith ( '33' ) } ; const RULE = colors . white ( '================================' ) ; export const defaultReporter = < T = any > ( { errors = { } } , { onError , logger } = { logger : defaultLogger } ) => { if ( ! Object . keys ( errors ) . length ) return ; const missingVarsOutput = [ ] ; const invalidVarsOutput = [ ] ; for ( const [ k , err ] of Object . entries ( errors ) ) { if ( err instanceof EnvMissingError ) { missingVarsOutput . push ( `    ${ colors . blue ( k ) }: ${ err . message || '(required)' }` ) ; } else invalidVarsOutput . push ( `    ${ colors . blue ( k ) }: ${ ( err as Error ) ?. message || '(invalid format)' }` ) ; } if ( invalidVarsOutput . length ) { invalidVarsOutput . unshift ( ` ${ colors . yellow ( 'Invalid' ) } environment variables:` ) ; } if ( missingVarsOutput . length ) { missingVarsOutput . unshift ( ` ${ colors . yellow ( 'Missing' ) } environment variables:` ) ; } const output = [ RULE , invalidVarsOutput . sort ( ) . join ( '\n' ) , missingVarsOutput . sort ( ) . join ( '\n' ) , colors . yellow ( '\n Exiting with error code 1' ) , RULE , ] . filter ( ( x ) => ! ! x ) . join ( '\n' ) ; logger ( output ) ; if ( onError ) { onError ( errors ) ; } else if ( isNode ) { process . exit ( 1 ) ; } else { throw new TypeError ( 'Environment validation failed' ) ; } } ;