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 * as path from 'path' ;
45import {
56 FileSystem ,
67 NewlineKind ,
78 PackageJsonLookup ,
89 IPackageJson
910} from '@microsoft/node-core-library' ;
11+
1012import { ExtractorConfig } from './ExtractorConfig' ;
1113import { Collector } from '../collector/Collector' ;
1214import { DtsRollupGenerator , DtsRollupKind } from '../generators/DtsRollupGenerator' ;
@@ -227,15 +229,15 @@ export class Extractor {
227229 const expectedApiReportContent : string = FileSystem . readFile ( expectedApiReportPath ) ;
228230
229231 if ( ! ReviewFileGenerator . areEquivalentApiFileContents ( actualApiReportContent , expectedApiReportContent ) ) {
232+ apiReportChanged = true ;
233+
230234 if ( ! localBuild ) {
231- // For production, issue a warning that will break the CI build.
235+ // For a production build , issue a warning that will break the CI build.
232236 messageRouter . logWarning ( ConsoleMessageId . ApiReportNotCopied ,
233237 'You have changed the public API signature for this project.'
234- // @microsoft /gulp-core-build seems to run JSON.stringify() on the error messages for some reason,
235- // so try to avoid escaped characters:
236- + ` Please overwrite ${ expectedApiReportShortPath } with a`
237- + ` copy of ${ actualApiReportShortPath } `
238- + ' and then request an API review. See the Git repository README.md for more info.' ) ;
238+ + ` Please copy the file "${ actualApiReportShortPath } " to "${ expectedApiReportShortPath } ",`
239+ + ` or perform a local build which does this automatically.`
240+ + ` See the Git repo documentation for more info.` ) ;
239241 } else {
240242 // For a local build, just copy the file automatically.
241243 messageRouter . logWarning ( ConsoleMessageId . ApiReportCopied ,
@@ -247,19 +249,43 @@ export class Extractor {
247249 convertLineEndings : NewlineKind . CrLf
248250 } ) ;
249251 }
250-
251- apiReportChanged = true ;
252- } else {
252+ } else {
253253 messageRouter . logVerbose ( ConsoleMessageId . ApiReportUnchanged ,
254254 `The API signature is up to date: ${ actualApiReportShortPath } ` ) ;
255255 }
256256 } else {
257- // NOTE: This warning seems like a nuisance, but it has caught genuine mistakes.
258- // For example, when projects were moved into category folders, the relative path for
259- // the API report files ended up in the wrong place.
260- messageRouter . logError ( ConsoleMessageId . ApiReportMissing , `The API report file has not been set up.`
261- + ` Do this by copying ${ actualApiReportShortPath } `
262- + ` to ${ expectedApiReportShortPath } and committing it.` ) ;
257+ // The target file does not exist, so we are setting up the API review file for the first time.
258+ //
259+ // NOTE: People sometimes make a mistake where they move a project and forget to update the "reportFolder"
260+ // setting, which causes a new file to silently get written to the wrong place. This can be confusing.
261+ // Thus we treat the initial creation of the file specially.
262+ apiReportChanged = true ;
263+
264+ if ( ! localBuild ) {
265+ // For a production build, issue a warning that will break the CI build.
266+ messageRouter . logWarning ( ConsoleMessageId . ApiReportNotCopied ,
267+ 'You have changed the public API signature for this project.'
268+ + ` Please copy the file "${ actualApiReportShortPath } " to "${ expectedApiReportShortPath } ",`
269+ + ` or perform a local build which does this automatically.`
270+ + ` See the Git repo documentation for more info.` ) ;
271+ } else {
272+ messageRouter . logWarning ( ConsoleMessageId . ApiReportCreated ,
273+ 'The API report file was missing, so a new file was created. Please add this file to Git:\n'
274+ + expectedApiReportPath
275+ ) ;
276+
277+ const expectedApiReportFolder : string = path . dirname ( expectedApiReportPath ) ;
278+ if ( ! FileSystem . exists ( expectedApiReportFolder ) ) {
279+ messageRouter . logError ( ConsoleMessageId . ApiReportFolderMissing ,
280+ 'Unable to create the API report file. Please make sure the target folder exists:\n'
281+ + expectedApiReportFolder
282+ ) ;
283+ } else {
284+ FileSystem . writeFile ( expectedApiReportPath , actualApiReportContent , {
285+ convertLineEndings : NewlineKind . CrLf
286+ } ) ;
287+ }
288+ }
263289 }
264290 }
265291
0 commit comments