@@ -6,13 +6,14 @@ import * as chalk from 'chalk';
66export function compileProject ( project : string , options : ts . CompilerOptions ) {
77 let parsed = parseProjectConfig ( project , options ) ;
88 let program = ts . createProgram ( parsed . fileNames , parsed . options ) ;
9+ let baseDir = program . getCurrentDirectory ( ) ;
910
1011 // Report any invalid TypeScript options for the project.
11- checkDiagnostics ( program . getOptionsDiagnostics ( ) ) ;
12+ reportDiagnostics ( program . getOptionsDiagnostics ( ) , baseDir ) ;
1213
1314 let emitResult = program . emit ( ) ;
1415
15- checkDiagnostics ( emitResult . diagnostics ) ;
16+ reportDiagnostics ( emitResult . diagnostics , baseDir ) ;
1617}
1718
1819/** Parses a TypeScript project configuration. */
@@ -31,26 +32,26 @@ function parseProjectConfig(project: string, options: ts.CompilerOptions) {
3132}
3233
3334/** Formats the TypeScript diagnostics into a error string. */
34- export function formatDiagnostics ( diagnostics : ts . Diagnostic [ ] ) : string {
35+ export function formatDiagnostics ( diagnostics : ts . Diagnostic [ ] , baseDir : string ) : string {
3536 return diagnostics . map ( diagnostic => {
36- let res = ts . DiagnosticCategory [ diagnostic . category ] ;
37+ let res = `• ${ chalk . red ( `TS ${ diagnostic . code } ` ) } - ` ;
3738
3839 if ( diagnostic . file ) {
3940 let { line, character} = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
41+ let filePath = path . relative ( baseDir , diagnostic . file . fileName ) ;
4042
41- res += ` at ${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ):` ;
43+ res += `${ filePath } (${ line + 1 } ,${ character + 1 } ): ` ;
4244 }
43- res += ` ${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ;
45+ res += `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ;
4446
4547 return res ;
4648 } ) . join ( '\n' ) ;
4749}
4850
49- /** Checks diagnostics and throws errors if present. */
50- export function checkDiagnostics ( diagnostics : ts . Diagnostic [ ] ) {
51+ /** Checks and reports diagnostics if present. */
52+ export function reportDiagnostics ( diagnostics : ts . Diagnostic [ ] , baseDir ?: string ) {
5153 if ( diagnostics && diagnostics . length && diagnostics [ 0 ] ) {
52- console . error ( formatDiagnostics ( diagnostics ) ) ;
53- console . error ( chalk . red ( 'TypeScript compilation failed. Exiting process.' ) ) ;
54- process . exit ( 1 ) ;
54+ console . error ( formatDiagnostics ( diagnostics , baseDir ) ) ;
55+ throw new Error ( 'TypeScript compilation failed.' ) ;
5556 }
5657}
0 commit comments