@@ -20,7 +20,7 @@ const TS_EXT = /\.ts$/;
2020export type Diagnostics = Array < ts . Diagnostic | api . Diagnostic > ;
2121
2222function isTsDiagnostic ( diagnostic : any ) : diagnostic is ts . Diagnostic {
23- return diagnostic && ( diagnostic . file || diagnostic . messageText ) ;
23+ return diagnostic && diagnostic . source != 'angular' ;
2424}
2525
2626export function formatDiagnostics ( options : api . CompilerOptions , diags : Diagnostics ) : string {
@@ -41,9 +41,9 @@ export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnosti
4141 ` at ${ d . span . start . file . url } (${ d . span . start . line + 1 } ,${ d . span . start . col + 1 } )` ;
4242 }
4343 if ( d . span && d . span . details ) {
44- res += `: ${ d . span . details } , ${ d . message } \n` ;
44+ res += `: ${ d . span . details } , ${ d . messageText } \n` ;
4545 } else {
46- res += `: ${ d . message } \n` ;
46+ res += `: ${ d . messageText } \n` ;
4747 }
4848 return res ;
4949 }
@@ -54,6 +54,7 @@ export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnosti
5454}
5555
5656export interface ParsedConfiguration {
57+ project : string ;
5758 options : api . CompilerOptions ;
5859 rootNames : string [ ] ;
5960 errors : Diagnostics ;
@@ -81,7 +82,7 @@ export function readConfiguration(
8182 let { config, error} = ts . readConfigFile ( projectFile , ts . sys . readFile ) ;
8283
8384 if ( error ) {
84- return { errors : [ error ] , rootNames : [ ] , options : { } } ;
85+ return { project , errors : [ error ] , rootNames : [ ] , options : { } } ;
8586 }
8687 const parseConfigHost = {
8788 useCaseSensitiveFileNames : true ,
@@ -94,16 +95,40 @@ export function readConfiguration(
9495 const rootNames = parsed . fileNames . map ( f => path . normalize ( f ) ) ;
9596
9697 const options = createNgCompilerOptions ( basePath , config , parsed . options ) ;
97- return { rootNames, options, errors : parsed . errors } ;
98+ return { project : projectFile , rootNames, options, errors : parsed . errors } ;
9899 } catch ( e ) {
99100 const errors : Diagnostics = [ {
100101 category : ts . DiagnosticCategory . Error ,
101- message : e . stack ,
102+ messageText : e . stack ,
103+ source : api . SOURCE ,
104+ code : api . UNKNOWN_ERROR_CODE
102105 } ] ;
103- return { errors, rootNames : [ ] , options : { } } ;
106+ return { project : '' , errors, rootNames : [ ] , options : { } } ;
104107 }
105108}
106109
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+
107132export function performCompilation (
108133 { rootNames, options, host, oldProgram, emitCallback, customTransformers} : {
109134 rootNames : string [ ] ,
@@ -112,11 +137,7 @@ export function performCompilation(
112137 oldProgram ?: api . Program ,
113138 emitCallback ?: api . TsEmitCallback ,
114139 customTransformers ?: api . CustomTransformers
115- } ) : {
116- program ?: api . Program ,
117- emitResult ?: ts . EmitResult ,
118- diagnostics : Diagnostics ,
119- } {
140+ } ) : PerformCompilationResult {
120141 const [ major , minor ] = ts . version . split ( '.' ) ;
121142
122143 if ( Number ( major ) < 2 || ( Number ( major ) === 2 && Number ( minor ) < 3 ) ) {
@@ -168,19 +189,24 @@ export function performCompilation(
168189 ( ( options . skipMetadataEmit || options . flatModuleOutFile ) ? 0 : api . EmitFlags . Metadata )
169190 } ) ;
170191 allDiagnostics . push ( ...emitResult . diagnostics ) ;
192+ return { diagnostics : allDiagnostics , program, emitResult} ;
171193 }
194+ return { diagnostics : allDiagnostics , program} ;
172195 } catch ( e ) {
173196 let errMsg : string ;
197+ let code : number ;
174198 if ( isSyntaxError ( e ) ) {
175199 // don't report the stack for syntax errors as they are well known errors.
176200 errMsg = e . message ;
201+ code = api . DEFAULT_ERROR_CODE ;
177202 } else {
178203 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 ;
179207 }
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} ;
184211 }
185- return { program, emitResult, diagnostics : allDiagnostics } ;
186212}
0 commit comments