import ts from 'typescript' ; import Logger from './logger' ; export function convertErrors ( diags ) { return diags . reduce ( ( result , diag ) => { let locationText = undefined ; if ( diag . file ) { const position = diag . file . getLineAndCharacterOfPosition ( diag . start ) ; const filename = diag . file . fileName ; locationText = `${ filename }:${ position . line + 1 }:${ position . character + 1 }` ; } let messageText = ts . flattenDiagnosticMessageText ( diag . messageText , "\n" ) ; messageText = `${ messageText } (TS${ diag . code })` ; result . push ( { messageText , locationText , category : diag . category , errorCode : diag . code } ) ; return result ; } , [ ] ) ; } /**
 * Write the compiler errors to console
 */ export function outputErrors ( errors , logger ) { errors . slice ( 0 , 10 ) . forEach ( error => { const write = ( error . category === ts . DiagnosticCategory . Error ) ? logger . error : logger . warn ; if ( error . locationText ) write ( error . locationText ) ; write ( error . messageText ) ; } ) ; } /**
 * Format the diagnostics and write to the console
 */ export function formatErrors ( diags , logger ) { const errors = convertErrors ( diags ) ; outputErrors ( errors , logger ) ; }