@@ -20,7 +20,7 @@ const TS_EXT = /\.ts$/;
20
20
export type Diagnostics = Array < ts . Diagnostic | api . Diagnostic > ;
21
21
22
22
function isTsDiagnostic ( diagnostic : any ) : diagnostic is ts . Diagnostic {
23
- return diagnostic && ( diagnostic . file || diagnostic . messageText ) ;
23
+ return diagnostic && diagnostic . source != 'angular' ;
24
24
}
25
25
26
26
export function formatDiagnostics ( options : api . CompilerOptions , diags : Diagnostics ) : string {
@@ -41,9 +41,9 @@ export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnosti
41
41
` at ${ d . span . start . file . url } (${ d . span . start . line + 1 } ,${ d . span . start . col + 1 } )` ;
42
42
}
43
43
if ( d . span && d . span . details ) {
44
- res += `: ${ d . span . details } , ${ d . message } \n` ;
44
+ res += `: ${ d . span . details } , ${ d . messageText } \n` ;
45
45
} else {
46
- res += `: ${ d . message } \n` ;
46
+ res += `: ${ d . messageText } \n` ;
47
47
}
48
48
return res ;
49
49
}
@@ -54,6 +54,7 @@ export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnosti
54
54
}
55
55
56
56
export interface ParsedConfiguration {
57
+ project : string ;
57
58
options : api . CompilerOptions ;
58
59
rootNames : string [ ] ;
59
60
errors : Diagnostics ;
@@ -81,7 +82,7 @@ export function readConfiguration(
81
82
let { config, error} = ts . readConfigFile ( projectFile , ts . sys . readFile ) ;
82
83
83
84
if ( error ) {
84
- return { errors : [ error ] , rootNames : [ ] , options : { } } ;
85
+ return { project , errors : [ error ] , rootNames : [ ] , options : { } } ;
85
86
}
86
87
const parseConfigHost = {
87
88
useCaseSensitiveFileNames : true ,
@@ -94,16 +95,40 @@ export function readConfiguration(
94
95
const rootNames = parsed . fileNames . map ( f => path . normalize ( f ) ) ;
95
96
96
97
const options = createNgCompilerOptions ( basePath , config , parsed . options ) ;
97
- return { rootNames, options, errors : parsed . errors } ;
98
+ return { project : projectFile , rootNames, options, errors : parsed . errors } ;
98
99
} catch ( e ) {
99
100
const errors : Diagnostics = [ {
100
101
category : ts . DiagnosticCategory . Error ,
101
- message : e . stack ,
102
+ messageText : e . stack ,
103
+ source : api . SOURCE ,
104
+ code : api . UNKNOWN_ERROR_CODE
102
105
} ] ;
103
- return { errors, rootNames : [ ] , options : { } } ;
106
+ return { project : '' , errors, rootNames : [ ] , options : { } } ;
104
107
}
105
108
}
106
109
110
+ export interface PerformCompilationResult {
111
+ diagnostics : Diagnostics ;
112
+ program ?: api . Program ;
113
+ emitResult ?: ts . EmitResult ;
114
+ }
115
+
116
+ export function exitCodeFromResult ( result : PerformCompilationResult | undefined ) : number {
117
+ if ( ! result ) {
118
+ // If we didn't get a result we should return failure.
119
+ return 1 ;
120
+ }
121
+ if ( ! result . diagnostics || result . diagnostics . length === 0 ) {
122
+ // If we have a result and didn't get any errors, we succeeded.
123
+ return 0 ;
124
+ }
125
+
126
+ // Return 2 if any of the errors were unknown.
127
+ return result . diagnostics . some ( d => d . source === 'angular' && d . code === api . UNKNOWN_ERROR_CODE ) ?
128
+ 2 :
129
+ 1 ;
130
+ }
131
+
107
132
export function performCompilation (
108
133
{ rootNames, options, host, oldProgram, emitCallback, customTransformers} : {
109
134
rootNames : string [ ] ,
@@ -112,11 +137,7 @@ export function performCompilation(
112
137
oldProgram ?: api . Program ,
113
138
emitCallback ?: api . TsEmitCallback ,
114
139
customTransformers ?: api . CustomTransformers
115
- } ) : {
116
- program ?: api . Program ,
117
- emitResult ?: ts . EmitResult ,
118
- diagnostics : Diagnostics ,
119
- } {
140
+ } ) : PerformCompilationResult {
120
141
const [ major , minor ] = ts . version . split ( '.' ) ;
121
142
122
143
if ( Number ( major ) < 2 || ( Number ( major ) === 2 && Number ( minor ) < 3 ) ) {
@@ -168,19 +189,24 @@ export function performCompilation(
168
189
( ( options . skipMetadataEmit || options . flatModuleOutFile ) ? 0 : api . EmitFlags . Metadata )
169
190
} ) ;
170
191
allDiagnostics . push ( ...emitResult . diagnostics ) ;
192
+ return { diagnostics : allDiagnostics , program, emitResult} ;
171
193
}
194
+ return { diagnostics : allDiagnostics , program} ;
172
195
} catch ( e ) {
173
196
let errMsg : string ;
197
+ let code : number ;
174
198
if ( isSyntaxError ( e ) ) {
175
199
// don't report the stack for syntax errors as they are well known errors.
176
200
errMsg = e . message ;
201
+ code = api . DEFAULT_ERROR_CODE ;
177
202
} else {
178
203
errMsg = e . stack ;
204
+ // It is not a syntax error we might have a program with unknown state, discard it.
205
+ program = undefined ;
206
+ code = api . UNKNOWN_ERROR_CODE ;
179
207
}
180
- allDiagnostics . push ( {
181
- category : ts . DiagnosticCategory . Error ,
182
- message : errMsg ,
183
- } ) ;
208
+ allDiagnostics . push (
209
+ { category : ts . DiagnosticCategory . Error , messageText : errMsg , code, source : api . SOURCE } ) ;
210
+ return { diagnostics : allDiagnostics , program} ;
184
211
}
185
- return { program, emitResult, diagnostics : allDiagnostics } ;
186
212
}
0 commit comments