@@ -6,13 +6,14 @@ import * as chalk from 'chalk';
6
6
export function compileProject ( project : string , options : ts . CompilerOptions ) {
7
7
let parsed = parseProjectConfig ( project , options ) ;
8
8
let program = ts . createProgram ( parsed . fileNames , parsed . options ) ;
9
+ let baseDir = program . getCurrentDirectory ( ) ;
9
10
10
11
// Report any invalid TypeScript options for the project.
11
- checkDiagnostics ( program . getOptionsDiagnostics ( ) ) ;
12
+ reportDiagnostics ( program . getOptionsDiagnostics ( ) , baseDir ) ;
12
13
13
14
let emitResult = program . emit ( ) ;
14
15
15
- checkDiagnostics ( emitResult . diagnostics ) ;
16
+ reportDiagnostics ( emitResult . diagnostics , baseDir ) ;
16
17
}
17
18
18
19
/** Parses a TypeScript project configuration. */
@@ -31,26 +32,26 @@ function parseProjectConfig(project: string, options: ts.CompilerOptions) {
31
32
}
32
33
33
34
/** 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 {
35
36
return diagnostics . map ( diagnostic => {
36
- let res = ts . DiagnosticCategory [ diagnostic . category ] ;
37
+ let res = `• ${ chalk . red ( `TS ${ diagnostic . code } ` ) } - ` ;
37
38
38
39
if ( diagnostic . file ) {
39
40
let { line, character} = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
41
+ let filePath = path . relative ( baseDir , diagnostic . file . fileName ) ;
40
42
41
- res += ` at ${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ):` ;
43
+ res += `${ filePath } (${ line + 1 } ,${ character + 1 } ): ` ;
42
44
}
43
- res += ` ${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ;
45
+ res += `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ;
44
46
45
47
return res ;
46
48
} ) . join ( '\n' ) ;
47
49
}
48
50
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 ) {
51
53
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.' ) ;
55
56
}
56
57
}
0 commit comments