2016-10-20 12:31:17 +00:00
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Copyright ( c ) Microsoft Corporation . All rights reserved .
* Licensed under the MIT License . See License . txt in the project root for license information .
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * /
'use strict' ;
import * as es from 'event-stream' ;
import * as _ from 'underscore' ;
2019-02-05 21:21:05 +00:00
import * as fancyLog from 'fancy-log' ;
import * as ansiColors from 'ansi-colors' ;
2017-02-08 10:05:50 +00:00
import * as fs from 'fs' ;
import * as path from 'path' ;
2016-10-20 12:31:17 +00:00
2018-04-05 08:00:50 +00:00
const allErrors : string [ ] [ ] = [ ] ;
2018-10-02 23:54:39 +00:00
let startTime : number | null = null ;
2016-10-20 12:31:17 +00:00
let count = 0 ;
function onStart ( ) : void {
if ( count ++ > 0 ) {
return ;
}
startTime = new Date ( ) . getTime ( ) ;
2019-02-05 21:21:05 +00:00
fancyLog ( ` Starting ${ ansiColors . green ( 'compilation' ) } ... ` ) ;
2016-10-20 12:31:17 +00:00
}
function onEnd ( ) : void {
if ( -- count > 0 ) {
2016-11-29 14:53:32 +00:00
return ;
2016-10-20 12:31:17 +00:00
}
2016-11-29 14:53:32 +00:00
log ( ) ;
}
2017-02-08 10:05:50 +00:00
const buildLogPath = path . join ( path . dirname ( path . dirname ( __dirname ) ) , '.build' , 'log' ) ;
try {
fs . mkdirSync ( path . dirname ( buildLogPath ) ) ;
} catch ( err ) {
// ignore
}
2016-11-29 14:53:32 +00:00
function log ( ) : void {
2016-10-20 12:31:17 +00:00
const errors = _ . flatten ( allErrors ) ;
2018-04-05 08:00:50 +00:00
const seen = new Set < string > ( ) ;
errors . map ( err = > {
if ( ! seen . has ( err ) ) {
seen . add ( err ) ;
2019-02-05 21:21:05 +00:00
fancyLog ( ` ${ ansiColors . red ( 'Error' ) } : ${ err } ` ) ;
2018-04-05 08:00:50 +00:00
}
} ) ;
2016-10-20 12:31:17 +00:00
2017-02-08 10:05:50 +00:00
const regex = /^([^(]+)\((\d+),(\d+)\): (.*)$/ ;
const messages = errors
. map ( err = > regex . exec ( err ) )
. filter ( match = > ! ! match )
2018-10-03 00:19:03 +00:00
. map ( x = > x as string [ ] )
2017-05-15 13:59:14 +00:00
. map ( ( [ , path , line , column , message ] ) = > ( { path , line : parseInt ( line ) , column : parseInt ( column ) , message } ) ) ;
2017-02-08 10:05:50 +00:00
2017-02-08 10:06:15 +00:00
try {
fs . writeFileSync ( buildLogPath , JSON . stringify ( messages ) ) ;
} catch ( err ) {
//noop
}
2017-02-08 10:05:50 +00:00
2019-02-05 21:21:05 +00:00
fancyLog ( ` Finished ${ ansiColors . green ( 'compilation' ) } with ${ errors . length } errors after ${ ansiColors . magenta ( ( new Date ( ) . getTime ( ) - startTime ! ) + ' ms' ) } ` ) ;
2016-10-20 12:31:17 +00:00
}
export interface IReporter {
2018-04-05 08:00:50 +00:00
( err : string ) : void ;
2016-10-20 12:31:17 +00:00
hasErrors ( ) : boolean ;
2016-11-29 14:53:32 +00:00
end ( emitError : boolean ) : NodeJS . ReadWriteStream ;
2016-10-20 12:31:17 +00:00
}
2016-10-20 12:59:07 +00:00
export function createReporter ( ) : IReporter {
2018-04-05 08:00:50 +00:00
const errors : string [ ] = [ ] ;
2016-10-20 12:31:17 +00:00
allErrors . push ( errors ) ;
2018-10-14 15:59:27 +00:00
const result = ( err : string ) = > errors . push ( err ) ;
2016-10-20 12:31:17 +00:00
2018-10-14 15:59:27 +00:00
result . hasErrors = ( ) = > errors . length > 0 ;
2016-10-20 12:31:17 +00:00
2018-10-14 15:59:27 +00:00
result . end = ( emitError : boolean ) : NodeJS . ReadWriteStream = > {
errors . length = 0 ;
onStart ( ) ;
2016-10-20 12:31:17 +00:00
2018-10-14 15:59:27 +00:00
return es . through ( undefined , function ( ) {
onEnd ( ) ;
2016-10-20 12:31:17 +00:00
2018-10-14 15:59:27 +00:00
if ( emitError && errors . length > 0 ) {
if ( ! ( errors as any ) . __logged__ ) {
log ( ) ;
}
2018-04-23 13:58:32 +00:00
2018-10-14 15:59:27 +00:00
( errors as any ) . __logged__ = true ;
2018-09-26 12:55:03 +00:00
2018-10-14 15:59:27 +00:00
const err = new Error ( ` Found ${ errors . length } errors ` ) ;
( err as any ) . __reporter__ = true ;
this . emit ( 'error' , err ) ;
} else {
this . emit ( 'end' ) ;
}
} ) ;
} ;
2016-10-20 12:31:17 +00:00
2018-10-14 15:59:27 +00:00
return result ;
2018-04-05 08:00:50 +00:00
}