1
1
import * as ts from 'typescript' ;
2
2
import * as path from 'path' ;
3
+ import * as chalk from 'chalk' ;
3
4
4
5
/** Compiles a TypeScript project with possible extra options. */
5
6
export function compileProject ( project : string , options : ts . CompilerOptions ) {
@@ -10,9 +11,8 @@ export function compileProject(project: string, options: ts.CompilerOptions) {
10
11
checkDiagnostics ( program . getOptionsDiagnostics ( ) ) ;
11
12
12
13
let emitResult = program . emit ( ) ;
13
- let emitDiagnostics = ts . getPreEmitDiagnostics ( program ) . concat ( emitResult . diagnostics ) ;
14
14
15
- checkDiagnostics ( emitDiagnostics ) ;
15
+ checkDiagnostics ( emitResult . diagnostics ) ;
16
16
}
17
17
18
18
/** Parses a TypeScript project configuration. */
@@ -38,11 +38,9 @@ export function formatDiagnostics(diagnostics: ts.Diagnostic[]): string {
38
38
if ( diagnostic . file ) {
39
39
let { line, character} = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
40
40
41
- res += ' at ' + diagnostic . file . fileName + ':' ;
42
- res += ( line + 1 ) + ':' + ( character + 1 ) + ':' ;
41
+ res += ` at ${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ):` ;
43
42
}
44
-
45
- res += ' ' + ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) ;
43
+ res += ` ${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ;
46
44
47
45
return res ;
48
46
} ) . join ( '\n' ) ;
@@ -51,6 +49,8 @@ export function formatDiagnostics(diagnostics: ts.Diagnostic[]): string {
51
49
/** Checks diagnostics and throws errors if present. */
52
50
export function checkDiagnostics ( diagnostics : ts . Diagnostic [ ] ) {
53
51
if ( diagnostics && diagnostics . length && diagnostics [ 0 ] ) {
54
- throw new Error ( formatDiagnostics ( diagnostics ) ) ;
52
+ console . error ( formatDiagnostics ( diagnostics ) ) ;
53
+ console . error ( chalk . red ( 'TypeScript compilation failed. Exiting process.' ) ) ;
54
+ process . exit ( 1 ) ;
55
55
}
56
56
}
0 commit comments