Skip to content

Commit 9a78e44

Browse files
committed
Automatically create the report file if it's missing
1 parent 14359ec commit 9a78e44

3 files changed

Lines changed: 54 additions & 22 deletions

File tree

‎apps/api-extractor/src/api/ConsoleMessageId.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const enum ConsoleMessageId {
2828
WritingDtsRollup = 'console-writing-dts-rollup',
2929

3030
/**
31-
* "You have changed the public API signature for this project. Please overwrite ___ with a
32-
* copy of ___ and then request an API review. See the Git repository README.md for more info."
31+
* "You have changed the public API signature for this project. Please copy the file ___ to ___,
32+
* or perform a local build which does this automatically. See the Git repo documentation for more info."
3333
*/
3434
ApiReportNotCopied = 'console-api-report-not-copied',
3535

@@ -39,12 +39,17 @@ export const enum ConsoleMessageId {
3939
ApiReportCopied = 'console-api-report-copied',
4040

4141
/**
42-
* "The API signature is up to date: ___"
42+
* "The API report is up to date: ___"
4343
*/
4444
ApiReportUnchanged = 'console-api-report-unchanged',
4545

4646
/**
47-
* "The API report file has not been set up. Do this by copying ___ to ___ and committing it."
47+
* "The API report file was missing, so a new file was created. Please add this file to Git: ___"
4848
*/
49-
ApiReportMissing = 'console-api-report-missing'
49+
ApiReportCreated = 'console-api-report-created',
50+
51+
/**
52+
* "Unable to create the API report file. Please make sure the target folder exists: ___"
53+
*/
54+
ApiReportFolderMissing = 'console-api-report-folder-missing'
5055
}

‎apps/api-extractor/src/api/Extractor.ts‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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';
45
import {
56
FileSystem,
67
NewlineKind,
78
PackageJsonLookup,
89
IPackageJson
910
} from '@microsoft/node-core-library';
11+
1012
import { ExtractorConfig } from './ExtractorConfig';
1113
import { Collector } from '../collector/Collector';
1214
import { 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

‎common/reviews/api/api-extractor.api.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export class CompilerState {
1818
// @public
1919
export const enum ConsoleMessageId {
2020
ApiReportCopied = "console-api-report-copied",
21-
ApiReportMissing = "console-api-report-missing",
21+
ApiReportCreated = "console-api-report-created",
22+
ApiReportFolderMissing = "console-api-report-folder-missing",
2223
ApiReportNotCopied = "console-api-report-not-copied",
2324
ApiReportUnchanged = "console-api-report-unchanged",
2425
FoundTSDocMetadata = "console-found-tsdoc-metadata",
@@ -77,7 +78,7 @@ export const enum ExtractorLogLevel {
7778
// @public
7879
export class ExtractorMessage {
7980
// Warning: (ae-forgotten-export) The symbol "IExtractorMessageOptions" needs to be exported by the entry point index.d.ts
80-
//
81+
//
8182
// @internal
8283
constructor(options: IExtractorMessageOptions);
8384
readonly category: ExtractorMessageCategory;

0 commit comments

Comments
 (0)