@@ -17,9 +17,10 @@ import { ExtractorContext } from '../ExtractorContext';
1717import { ILogger } from './ILogger' ;
1818import ApiJsonGenerator from '../generators/ApiJsonGenerator' ;
1919import ApiFileGenerator from '../generators/ApiFileGenerator' ;
20+ import { MonitoredLogger } from './MonitoredLogger' ;
2021
2122/**
22- * Options for {@link Extractor.analyzeProject }.
23+ * Options for {@link Extractor.processProject }.
2324 * @public
2425 */
2526export interface IAnalyzeProjectOptions {
@@ -79,11 +80,11 @@ export class Extractor {
7980 logError : ( message : string ) => console . error ( colors . red ( message ) )
8081 } ;
8182
82- private _config : IExtractorConfig ;
83- private _program : ts . Program ;
84- private _localBuild : boolean ;
85- private _logger : ILogger ;
86- private _absoluteRootFolder : string ;
83+ private readonly _config : IExtractorConfig ;
84+ private readonly _program : ts . Program ;
85+ private readonly _localBuild : boolean ;
86+ private readonly _monitoredLogger : MonitoredLogger ;
87+ private readonly _absoluteRootFolder : string ;
8788
8889 private static _applyConfigDefaults ( config : IExtractorConfig ) : IExtractorConfig {
8990 // Use the provided config to override the defaults
@@ -93,17 +94,18 @@ export class Extractor {
9394 return normalized ;
9495 }
9596
96- public constructor ( config : IExtractorConfig , options ?: IExtractorOptions ) {
97+ public constructor ( config : IExtractorConfig , options ?: IExtractorOptions ) {
98+ let mergedLogger : ILogger ;
9799 if ( options && options . customLogger ) {
98- this . _logger = lodash . merge ( lodash . cloneDeep ( Extractor . _defaultLogger ) ,
99- options . customLogger ) ;
100+ mergedLogger = lodash . merge ( lodash . clone ( Extractor . _defaultLogger ) , options . customLogger ) ;
100101 } else {
101- this . _logger = Extractor . _defaultLogger ;
102+ mergedLogger = Extractor . _defaultLogger ;
102103 }
104+ this . _monitoredLogger = new MonitoredLogger ( mergedLogger ) ;
103105
104106 this . _config = Extractor . _applyConfigDefaults ( config ) ;
105107
106- this . _logger . logVerbose ( 'API Extractor Config: ' + JSON . stringify ( this . _config ) ) ;
108+ this . _monitoredLogger . logVerbose ( 'API Extractor Config: ' + JSON . stringify ( this . _config ) ) ;
107109
108110 if ( ! options ) {
109111 options = { } ;
@@ -160,8 +162,21 @@ export class Extractor {
160162
161163 /**
162164 * Invokes the API Extractor engine, using the configuration that was passed to the constructor.
165+ * @deprecated Use {@link Extractor.processProject} instead.
163166 */
164167 public analyzeProject ( options ?: IAnalyzeProjectOptions ) : void {
168+ this . processProject ( options ) ;
169+ }
170+
171+ /**
172+ * Invokes the API Extractor engine, using the configuration that was passed to the constructor.
173+ * @param options - provides additional runtime state that is NOT part of the API Extractor
174+ * config file.
175+ * @returns true if there were no errors or warnings; false if the tool chain should fail the build
176+ */
177+ public processProject ( options ?: IAnalyzeProjectOptions ) : boolean {
178+ this . _monitoredLogger . resetCounters ( ) ;
179+
165180 if ( ! options ) {
166181 options = { } ;
167182 }
@@ -178,7 +193,7 @@ export class Extractor {
178193 const context : ExtractorContext = new ExtractorContext ( {
179194 program : this . _program ,
180195 entryPointFile : path . resolve ( this . _absoluteRootFolder , projectConfig . entryPointSourceFile ) ,
181- logger : this . _logger ,
196+ logger : this . _monitoredLogger ,
182197 policies : this . _config . policies
183198 } ) ;
184199
@@ -199,7 +214,7 @@ export class Extractor {
199214 const jsonGenerator : ApiJsonGenerator = new ApiJsonGenerator ( ) ;
200215 const apiJsonFilename : string = path . join ( outputFolder , packageBaseName + '.api.json' ) ;
201216
202- this . _logger . logVerbose ( 'Writing: ' + apiJsonFilename ) ;
217+ this . _monitoredLogger . logVerbose ( 'Writing: ' + apiJsonFilename ) ;
203218 jsonGenerator . writeJsonFile ( apiJsonFilename , context ) ;
204219 }
205220
@@ -228,30 +243,34 @@ export class Extractor {
228243 if ( ! ApiFileGenerator . areEquivalentApiFileContents ( actualApiReviewContent , expectedApiReviewContent ) ) {
229244 if ( ! this . _localBuild ) {
230245 // For production, issue a warning that will break the CI build.
231- this . _logger . logWarning ( 'You have changed the public API signature for this project.'
246+ this . _monitoredLogger . logWarning ( 'You have changed the public API signature for this project.'
232247 // @microsoft /gulp-core-build seems to run JSON.stringify() on the error messages for some reason,
233248 // so try to avoid escaped characters:
234249 + ` Please overwrite ${ expectedApiReviewShortPath } with a`
235250 + ` copy of ${ actualApiReviewShortPath } `
236251 + ' and then request an API review. See the Git repository README.md for more info.' ) ;
237252 } else {
238253 // For a local build, just copy the file automatically.
239- this . _logger . logWarning ( 'You have changed the public API signature for this project.'
254+ this . _monitoredLogger . logWarning ( 'You have changed the public API signature for this project.'
240255 + ` Updating ${ expectedApiReviewShortPath } ` ) ;
241256
242257 fsx . writeFileSync ( expectedApiReviewPath , actualApiReviewContent ) ;
243258 }
244259 } else {
245- this . _logger . logVerbose ( `The API signature is up to date: ${ actualApiReviewShortPath } ` ) ;
260+ this . _monitoredLogger . logVerbose ( `The API signature is up to date: ${ actualApiReviewShortPath } ` ) ;
246261 }
247262 } else {
248263 // NOTE: This warning seems like a nuisance, but it has caught genuine mistakes.
249264 // For example, when projects were moved into category folders, the relative path for
250265 // the API review files ended up in the wrong place.
251- this . _logger . logError ( `The API review file has not been set up. Do this by copying ${ actualApiReviewShortPath } `
266+ this . _monitoredLogger . logError ( `The API review file has not been set up.`
267+ + ` Do this by copying ${ actualApiReviewShortPath } `
252268 + ` to ${ expectedApiReviewShortPath } and committing it.` ) ;
253269 }
254270 }
271+
272+ // If there were any errors or warnings, then fail the build
273+ return ( this . _monitoredLogger . errorCount + this . _monitoredLogger . warningCount ) === 0 ;
255274 }
256275
257276 private _getShortFilePath ( absolutePath : string ) : string {
0 commit comments