import * as path from 'path' ; import * as ts from 'typescript' ; /** The default FormatDiagnosticsHost for tsec. */ export const FORMAT_DIAGNOSTIC_HOST = { getCurrentDirectory : ts . sys . getCurrentDirectory , getCanonicalFileName : fileName => path . resolve ( fileName ) , getNewLine : ( ) => ts . sys . newLine , } ; const NEW_LINE = FORMAT_DIAGNOSTIC_HOST . getNewLine ( ) ; /** Report the summary of error Diagnostic. */ export function reportErrorSummary ( errorCount ) { if ( errorCount > 0 ) { if ( errorCount === 1 ) { ts . sys . write ( `Found 1 error.${ NEW_LINE }` ) ; } else { ts . sys . write ( `Found ${ errorCount } errors.${ NEW_LINE }` ) ; } } } /** Report a list of Diagnostic with an optional trailing error summary. */ function reportDiagnostics ( diagnostics , withSummary , pretty ) { const formatter = pretty ? ts . formatDiagnosticsWithColorAndContext : ts . formatDiagnostics ; ts . sys . write ( formatter ( diagnostics , FORMAT_DIAGNOSTIC_HOST ) ) ; const errorCount = diagnostics . filter ( d => d . category === ts . DiagnosticCategory . Error ) . length ; if ( withSummary && errorCount !== 0 ) { ts . sys . write ( NEW_LINE + NEW_LINE ) ; reportErrorSummary ( errorCount ) ; } return errorCount ; } /**
 * create a diagnostic reporter with a pretty option inferred from compiler or
 * build options.
 */ export function createDiagnosticsReporter ( options ) { let pretty = ts . sys . writeOutputIsTTY === undefined ? false : ts . sys . writeOutputIsTTY ( ) ; if ( options [ 'pretty' ] !== undefined ) pretty = options [ 'pretty' ] as boolean ; return ( diagnostics , withSummary ) => reportDiagnostics ( diagnostics , withSummary , pretty ) ; }