11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4- import lodash = require( 'lodash' ) ;
5- import colors = require( 'colors' ) ;
6-
74import {
85 FileSystem ,
96 NewlineKind ,
107 PackageJsonLookup ,
118 IPackageJson
129} from '@microsoft/node-core-library' ;
1310import { ExtractorConfig } from './ExtractorConfig' ;
14- import { ILogger } from './ILogger' ;
1511import { Collector } from '../collector/Collector' ;
1612import { DtsRollupGenerator , DtsRollupKind } from '../generators/DtsRollupGenerator' ;
17- import { MonitoredLogger } from './MonitoredLogger' ;
1813import { ApiModelGenerator } from '../generators/ApiModelGenerator' ;
1914import { ApiPackage } from '@microsoft/api-extractor-model' ;
2015import { ReviewFileGenerator } from '../generators/ReviewFileGenerator' ;
@@ -23,6 +18,8 @@ import { ValidationEnhancer } from '../enhancers/ValidationEnhancer';
2318import { DocCommentEnhancer } from '../enhancers/DocCommentEnhancer' ;
2419import { CompilerState } from './CompilerState' ;
2520import { ExtractorMessage } from './ExtractorMessage' ;
21+ import { MessageRouter } from '../collector/MessageRouter' ;
22+ import { ConsoleMessageId } from './ConsoleMessageId' ;
2623
2724/**
2825 * Runtime options for Extractor.
@@ -36,12 +33,6 @@ export interface IExtractorInvokeOptions {
3633 */
3734 compilerState ?: CompilerState ;
3835
39- /**
40- * Allows the caller to customize how API Extractor's errors, warnings, and informational logging is processed.
41- * If omitted, the output will be printed to the console.
42- */
43- customLogger ?: Partial < ILogger > ;
44-
4536 /**
4637 * Indicates that API Extractor is running as part of a local build, e.g. on developer's
4738 * machine. This disables certain validation that would normally be performed
@@ -52,6 +43,11 @@ export interface IExtractorInvokeOptions {
5243 */
5344 localBuild ?: boolean ;
5445
46+ /**
47+ * If true, API Extractor will include {@link ExtractorLogLevel.Verbose} messages in its output.
48+ */
49+ showVerboseMessages ?: boolean ;
50+
5551 /**
5652 * By default API Extractor uses its own TypeScript compiler version to analyze your project.
5753 * This can often cause compiler errors due to incompatibilities between different TS versions.
@@ -99,12 +95,18 @@ export class ExtractorResult {
9995 public readonly succeeded : boolean ;
10096
10197 /**
102- * Reports the number of times that {@link ILogger.logError} was called.
98+ * Reports the number of errors encountered during analysis.
99+ *
100+ * @remarks
101+ * This does not count exceptions, where unexpected issues prematurely abort the operation.
103102 */
104103 public readonly errorCount : number ;
105104
106105 /**
107- * Reports the number of times that {@link ILogger.logWarning} was called.
106+ * Reports the number of warnings encountered during analysis.
107+ *
108+ * @remarks
109+ * This does not count warnings that are emitted in the API report file.
108110 */
109111 public readonly warningCount : number ;
110112
@@ -141,13 +143,6 @@ export class Extractor {
141143 return PackageJsonLookup . loadOwnPackageJson ( __dirname ) ;
142144 }
143145
144- private static _defaultLogger : ILogger = {
145- logVerbose : ( message : string ) => console . log ( '(Verbose) ' + message ) ,
146- logInfo : ( message : string ) => console . log ( message ) ,
147- logWarning : ( message : string ) => console . warn ( colors . yellow ( message ) ) ,
148- logError : ( message : string ) => console . error ( colors . red ( message ) )
149- } ;
150-
151146 /**
152147 * Load the api-extractor.json config file from the specified path, and then invoke API Extractor.
153148 */
@@ -166,18 +161,8 @@ export class Extractor {
166161 options = { } ;
167162 }
168163
169- let mergedLogger : ILogger ;
170- if ( options && options . customLogger ) {
171- mergedLogger = lodash . merge ( lodash . clone ( Extractor . _defaultLogger ) , options . customLogger ) ;
172- } else {
173- mergedLogger = Extractor . _defaultLogger ;
174- }
175- const monitoredLogger : MonitoredLogger = new MonitoredLogger ( mergedLogger ) ;
176-
177164 const localBuild : boolean = options . localBuild || false ;
178165
179- monitoredLogger . resetCounters ( ) ;
180-
181166 let compilerState : CompilerState | undefined ;
182167 if ( options . compilerState ) {
183168 compilerState = options . compilerState ;
@@ -187,10 +172,13 @@ export class Extractor {
187172
188173 const collector : Collector = new Collector ( {
189174 program : compilerState . program ,
190- logger : monitoredLogger ,
175+ messageCallback : options . messageCallback ,
191176 extractorConfig : extractorConfig
192177 } ) ;
193178
179+ const messageRouter : MessageRouter = collector . messageRouter ;
180+ messageRouter . showVerboseMessages = ! ! options . showVerboseMessages ;
181+
194182 collector . analyze ( ) ;
195183
196184 DocCommentEnhancer . analyze ( collector ) ;
@@ -200,7 +188,7 @@ export class Extractor {
200188 const apiPackage : ApiPackage = modelBuilder . buildApiPackage ( ) ;
201189
202190 if ( extractorConfig . docModelEnabled ) {
203- monitoredLogger . logVerbose ( 'Writing: ' + extractorConfig . apiJsonFilePath ) ;
191+ messageRouter . logVerbose ( ConsoleMessageId . WritingDocModelFile , 'Writing: ' + extractorConfig . apiJsonFilePath ) ;
204192 apiPackage . saveToJsonFile ( extractorConfig . apiJsonFilePath , {
205193 toolPackage : Extractor . packageName ,
206194 toolVersion : Extractor . version ,
@@ -233,15 +221,17 @@ export class Extractor {
233221 if ( ! ReviewFileGenerator . areEquivalentApiFileContents ( actualApiReviewContent , expectedApiReviewContent ) ) {
234222 if ( ! localBuild ) {
235223 // For production, issue a warning that will break the CI build.
236- monitoredLogger . logWarning ( 'You have changed the public API signature for this project.'
224+ messageRouter . logWarning ( ConsoleMessageId . ApiReportNotCopied ,
225+ 'You have changed the public API signature for this project.'
237226 // @microsoft /gulp-core-build seems to run JSON.stringify() on the error messages for some reason,
238227 // so try to avoid escaped characters:
239228 + ` Please overwrite ${ expectedApiReviewShortPath } with a`
240229 + ` copy of ${ actualApiReviewShortPath } `
241230 + ' and then request an API review. See the Git repository README.md for more info.' ) ;
242231 } else {
243232 // For a local build, just copy the file automatically.
244- monitoredLogger . logWarning ( 'You have changed the public API signature for this project.'
233+ messageRouter . logWarning ( ConsoleMessageId . ApiReportCopied ,
234+ 'You have changed the public API signature for this project.'
245235 + ` Updating ${ expectedApiReviewShortPath } ` ) ;
246236
247237 FileSystem . writeFile ( expectedApiReviewPath , actualApiReviewContent , {
@@ -250,64 +240,55 @@ export class Extractor {
250240 } ) ;
251241 }
252242 } else {
253- monitoredLogger . logVerbose ( `The API signature is up to date: ${ actualApiReviewShortPath } ` ) ;
243+ messageRouter . logVerbose ( ConsoleMessageId . ApiReportUnchanged ,
244+ `The API signature is up to date: ${ actualApiReviewShortPath } ` ) ;
254245 }
255246 } else {
256247 // NOTE: This warning seems like a nuisance, but it has caught genuine mistakes.
257248 // For example, when projects were moved into category folders, the relative path for
258249 // the API review files ended up in the wrong place.
259- monitoredLogger . logError ( `The API review file has not been set up.`
250+ messageRouter . logError ( ConsoleMessageId . ApiReportMissing , `The API review file has not been set up.`
260251 + ` Do this by copying ${ actualApiReviewShortPath } `
261252 + ` to ${ expectedApiReviewShortPath } and committing it.` ) ;
262253 }
263254 }
264255
265256 if ( extractorConfig . rollupEnabled ) {
266- Extractor . _generateRollupDtsFile ( collector , monitoredLogger ,
267- extractorConfig . publicTrimmedFilePath ,
268- DtsRollupKind . PublicRelease ) ;
269-
270- Extractor . _generateRollupDtsFile ( collector , monitoredLogger ,
271- extractorConfig . betaTrimmedFilePath ,
272- DtsRollupKind . BetaRelease ) ;
273-
274- Extractor . _generateRollupDtsFile ( collector , monitoredLogger ,
275- extractorConfig . untrimmedFilePath ,
276- DtsRollupKind . InternalRelease ) ;
257+ Extractor . _generateRollupDtsFile ( collector , extractorConfig . publicTrimmedFilePath , DtsRollupKind . PublicRelease ) ;
258+ Extractor . _generateRollupDtsFile ( collector , extractorConfig . betaTrimmedFilePath , DtsRollupKind . BetaRelease ) ;
259+ Extractor . _generateRollupDtsFile ( collector , extractorConfig . untrimmedFilePath , DtsRollupKind . InternalRelease ) ;
277260 }
278261
279262 if ( extractorConfig . tsdocMetadataEnabled ) {
280263 // Write the tsdoc-metadata.json file for this project
281264 PackageMetadataManager . writeTsdocMetadataFile ( extractorConfig . tsdocMetadataFilePath ) ;
282265 }
283266
284- // Show out all the messages that we collected during analysis
285- collector . messageRouter . reportMessagesToLogger ( monitoredLogger , collector . workingPackage . packageFolder ) ;
267+ // Show all the messages that we collected during analysis
268+ messageRouter . handleRemainingNonConsoleMessages ( ) ;
286269
287270 // Determine success
288271 let succeeded : boolean ;
289272 if ( localBuild ) {
290273 // For a local build, fail if there were errors (but ignore warnings)
291- succeeded = monitoredLogger . errorCount === 0 ;
274+ succeeded = messageRouter . errorCount === 0 ;
292275 } else {
293276 // For a production build, fail if there were any errors or warnings
294- succeeded = monitoredLogger . errorCount + monitoredLogger . warningCount === 0 ;
277+ succeeded = messageRouter . errorCount + messageRouter . warningCount === 0 ;
295278 }
296279
297280 return new ExtractorResult ( {
298281 compilerState,
299282 extractorConfig,
300283 succeeded,
301- errorCount : monitoredLogger . errorCount ,
302- warningCount : monitoredLogger . warningCount
284+ errorCount : messageRouter . errorCount ,
285+ warningCount : messageRouter . warningCount
303286 } ) ;
304287 }
305288
306- private static _generateRollupDtsFile ( collector : Collector , monitoredLogger : MonitoredLogger ,
307- outputPath : string , dtsKind : DtsRollupKind ) : void {
308-
289+ private static _generateRollupDtsFile ( collector : Collector , outputPath : string , dtsKind : DtsRollupKind ) : void {
309290 if ( outputPath !== '' ) {
310- monitoredLogger . logVerbose ( `Writing package typings: ${ outputPath } ` ) ;
291+ collector . messageRouter . logVerbose ( ConsoleMessageId . WritingDtsRollup , `Writing package typings: ${ outputPath } ` ) ;
311292 DtsRollupGenerator . writeTypingsFile ( collector , outputPath , dtsKind ) ;
312293 }
313294 }
0 commit comments