Skip to content

Commit 5e51997

Browse files
committed
Update ApiExtractorTask to use the api-extractor config file.
1 parent 5e62c38 commit 5e51997

File tree

3 files changed

+19
-215
lines changed

3 files changed

+19
-215
lines changed

core-build/gulp-core-build-typescript/src/ApiExtractorTask.ts

Lines changed: 17 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,18 @@
22
// See LICENSE in the project root for license information.
33

44
import * as path from 'path';
5+
import { IBuildConfig } from '@microsoft/gulp-core-build';
56
import {
67
JsonFile,
78
FileSystem
89
} from '@microsoft/node-core-library';
9-
import { ApiExtractorRunner as TApiExtractorRunner } from '@microsoft/rush-stack-compiler-2.7';
1010
import { IExtractorConfig, IExtractorOptions } from '@microsoft/api-extractor';
11+
import { ApiExtractorRunner as TApiExtractorRunner } from '@microsoft/rush-stack-compiler-2.7';
1112

1213
import { RSCTask, IRSCTaskConfig } from './RSCTask';
1314

1415
/** @public */
1516
export interface IApiExtractorTaskConfig extends IRSCTaskConfig {
16-
/**
17-
* Indicates whether the task should be run.
18-
*/
19-
enabled: boolean;
20-
21-
/**
22-
* The file path of the exported entry point, relative to the project folder.
23-
*
24-
* Example: "lib/index.d.ts"
25-
*/
26-
entry?: string;
27-
28-
/**
29-
* The file path of the folder containing API files to be reviewed, relative to
30-
* the project folder. This is part of an API review workflow: During a build,
31-
* the ApiExtractorTask will output an API file, e.g. "my-project/temp/my-project.api.ts".
32-
* It will then compare this file against the last reviewed file,
33-
* e.g. "../api-review/my-project.api.ts" (assuming that apiReviewFolder is "../api-review").
34-
* If the files are different, the build will fail with an error message that instructs
35-
* the developer to update the approved file, and then commit it to Git. When they
36-
* create a Pull Request, a VSO branch policy will look for changes under "api-review/*"
37-
* and require signoff from the appropriate reviewers.
38-
*
39-
* Example: "config" (for a standalone project)
40-
* Example: "../../common/api-review" (for a Git repository with Rush)
41-
*/
42-
apiReviewFolder?: string;
43-
44-
/**
45-
* The file path of the folder containing the *.api.json output file containing
46-
* API information. The default location is in the “dist” folder,
47-
* e.g. my-project/dist/my-project.api.json. This file should be published as part
48-
* of the NPM package. When building other projects that depend on this package,
49-
* api-extractor will look for this file in the node_modules folder and use it as an input.
50-
* The *.api.json file is also consumed by a tool at
51-
* https://github.com/SharePoint/ts-spec-gen that generates an online API documentation.
52-
*/
53-
apiJsonFolder?: string;
54-
55-
/**
56-
* If true, then API Extractor will generate *.d.ts rollup files for this project.
57-
* @beta
58-
*/
59-
generateDtsRollup?: boolean;
60-
61-
/**
62-
* Only used if generateDtsRollup=true. If dtsRollupTrimming=true, then API Extractor will
63-
* generate separate *.d.ts rollup files for internal, beta, and public release types;
64-
* otherwise a single *.d.ts file will be generated with no trimming.
65-
* @beta
66-
*/
67-
dtsRollupTrimming: boolean;
68-
69-
/**
70-
* This setting is only used if dtsRollupTrimming is true.
71-
* It indicates the folder where "npm publish" will be run for an internal release.
72-
* The default value is "./dist/internal".
73-
*
74-
* @beta
75-
* An internal release will contain all definitions that are reachable from the entry point.
76-
*/
77-
publishFolderForInternal?: string;
78-
79-
/**
80-
* This setting is only used if dtsRollupTrimming is true.
81-
* It indicates the folder where "npm publish" will be run for a beta release.
82-
* The default value is "./dist/beta".
83-
*
84-
* @beta
85-
* A beta release will contain all definitions that are reachable from the entry point,
86-
* except definitions marked as \@alpha or \@internal.
87-
*/
88-
publishFolderForBeta?: string;
89-
90-
/**
91-
* This setting is only used if dtsRollupTrimming is true.
92-
* It indicates the folder where "npm publish" will be run for a public release.
93-
* The default value is "./dist/public".
94-
*
95-
* @beta
96-
* A public release will contain all definitions that are reachable from the entry point,
97-
* except definitions marked as \@beta, \@alpha, or \@internal.
98-
*/
99-
publishFolderForPublic?: string;
100-
101-
/**
102-
* This option causes the typechecker to be invoked with the --skipLibCheck option. This option is not
103-
* recommended and may cause API Extractor to produce incomplete or incorrect declarations, but it
104-
* may be required when dependencies contain declarations that are incompatible with the TypeScript engine
105-
* that API Extractor uses for its analysis. If this option is used, it is strongly recommended that broken
106-
* dependencies be fixed or upgraded.
107-
*/
108-
skipLibCheck?: boolean;
10917
}
11018

11119
/**
@@ -118,85 +26,31 @@ export class ApiExtractorTask extends RSCTask<IApiExtractorTaskConfig> {
11826
constructor() {
11927
super(
12028
'api-extractor',
121-
{
122-
enabled: false,
123-
entry: undefined,
124-
apiReviewFolder: undefined,
125-
apiJsonFolder: undefined
126-
}
29+
{}
12730
);
12831
}
12932

13033
public loadSchema(): Object {
13134
return JsonFile.load(path.resolve(__dirname, 'schemas', 'api-extractor.schema.json'));
13235
}
13336

134-
public isEnabled(): boolean {
135-
return !!this.taskConfig.enabled;
37+
public isEnabled(buildConfig: IBuildConfig): boolean {
38+
return FileSystem.exists(this._getApiExtractorConfigFilePath(buildConfig.rootPath));
13639
}
13740

13841
public executeTask(): Promise<void> {
13942
this.initializeRushStackCompiler();
14043

141-
if (!this._validateConfiguration()) {
142-
return Promise.resolve();
143-
}
144-
145-
if (!this.taskConfig.entry) {
146-
return Promise.reject(new Error('entry must be defined'));
147-
}
148-
149-
if (!this.taskConfig.apiJsonFolder) {
150-
return Promise.reject(new Error('apiJsonFolder must be defined'));
151-
}
152-
153-
if (!this.taskConfig.apiReviewFolder) {
154-
return Promise.reject(new Error('apiReviewFolder must be defined'));
155-
}
156-
157-
let entryPointFile: string;
158-
if (this.taskConfig.entry === 'src/index.ts') {
159-
// backwards compatibility for legacy projects that used *.ts files as their entry point
160-
entryPointFile = path.join(this.buildConfig.rootPath, 'lib/index.d.ts');
161-
} else {
162-
entryPointFile = path.join(this.buildConfig.rootPath, this.taskConfig.entry);
163-
}
164-
165-
const extractorConfig: IExtractorConfig = {
166-
compiler: {
167-
configType: 'tsconfig',
168-
rootFolder: this.buildConfig.rootPath
169-
},
170-
project: {
171-
entryPointSourceFile: entryPointFile
172-
},
173-
apiReviewFile: {
174-
enabled: true,
175-
apiReviewFolder: this.taskConfig.apiReviewFolder,
176-
tempFolder: path.join(this.buildConfig.rootPath, this.buildConfig.tempFolder)
177-
},
178-
apiJsonFile: {
179-
enabled: true,
180-
outputFolder: this.taskConfig.apiJsonFolder
181-
}
182-
};
183-
184-
if (this.taskConfig.generateDtsRollup) {
185-
extractorConfig.dtsRollup = {
186-
enabled: true,
187-
trimming: !!this.taskConfig.dtsRollupTrimming,
188-
publishFolderForInternal: this.taskConfig.publishFolderForInternal,
189-
publishFolderForBeta: this.taskConfig.publishFolderForBeta,
190-
publishFolderForPublic: this.taskConfig.publishFolderForPublic
191-
};
192-
}
193-
19444
const extractorOptions: IExtractorOptions = {
195-
localBuild: !this.buildConfig.production,
196-
skipLibCheck: this.taskConfig.skipLibCheck
45+
localBuild: !this.buildConfig.production
19746
};
19847

199-
const apiExtractorRunner: TApiExtractorRunner = new this._rushStackCompiler.ApiExtractorRunner(
48+
const ApiExtractorRunner: typeof TApiExtractorRunner = this._rushStackCompiler.ApiExtractorRunner;
49+
const extractorConfig: IExtractorConfig = ApiExtractorRunner.apiExtractor.Extractor.loadConfigObject(
50+
this._getApiExtractorConfigFilePath(this.buildConfig.rootPath)
51+
);
52+
53+
const apiExtractorRunner: TApiExtractorRunner = new ApiExtractorRunner(
20054
extractorConfig,
20155
extractorOptions,
20256
this.buildFolder,
@@ -206,22 +60,11 @@ export class ApiExtractorTask extends RSCTask<IApiExtractorTaskConfig> {
20660
return apiExtractorRunner.invoke();
20761
}
20862

209-
private _validateConfiguration(): boolean {
210-
if (!this.taskConfig.entry) {
211-
this.logError('Missing or empty "entry" field in api-extractor.json');
212-
return false;
213-
}
214-
215-
if (!this.taskConfig.apiReviewFolder) {
216-
this.logError('Missing or empty "apiReviewFolder" field in api-extractor.json');
217-
return false;
218-
}
219-
220-
if (!FileSystem.exists(this.taskConfig.entry)) {
221-
this.logError(`Entry file ${this.taskConfig.entry} does not exist.`);
222-
return false;
223-
}
63+
protected _getConfigFilePath(): string {
64+
return path.join('.', 'config', 'gcb-api-extractor.json'); // There aren't config options specific to this task
65+
}
22466

225-
return true;
67+
private _getApiExtractorConfigFilePath(rootPath: string): string {
68+
return path.resolve(rootPath, 'config', 'api-extractor.json');
22669
}
22770
}

core-build/gulp-core-build-typescript/src/schemas/api-extractor.schema.json

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,6 @@
99
"$schema": {
1010
"description": "Part of the JSON Schema standard, this optional keyword declares the URL of the schema that the file conforms to. Editors may download the schema and use it to perform syntax highlighting.",
1111
"type": "string"
12-
},
13-
14-
"enabled": {
15-
"description": "Indicates whether the task should be run",
16-
"type": "boolean"
17-
},
18-
"entry": {
19-
"description": "The file path of the exported entry point, relative to the project folder",
20-
"type": "string"
21-
},
22-
"apiReviewFolder": {
23-
"description": "The file path of the folder containing already approved API files, relative to the project folder.",
24-
"type": "string"
25-
},
26-
"apiJsonFolder": {
27-
"description": "The file path of the folder containing the *.api.json output file containing API information, relative to the project folder.",
28-
"type": "string"
29-
},
30-
"generateDtsRollup": {
31-
"description": "If true, then API Extractor will generate *.d.ts rollup files for this project.",
32-
"type": "boolean"
33-
},
34-
"dtsRollupTrimming": {
35-
"description": "Only used if generateDtsRollup=true. If dtsRollupTrimming=true, then API Extractor will generate separate *.d.ts rollup files for internal, beta, and public release types; otherwise a single *.d.ts file will be generated with no trimming.",
36-
"type": "boolean"
37-
},
38-
"publishFolderForInternal": {
39-
"description": "This setting is only used if \"dtsRollupTrimming\" is true. It indicates the folder where \"npm publish\" will be run for an internal release. The default value is \"./dist/internal\". An internal release will contain all definitions that are reachable from the entry point.",
40-
"type": "string"
41-
},
42-
"publishFolderForBeta": {
43-
"description": "This setting is only used if \"dtsRollupTrimming\" is true. It indicates the folder where \"npm publish\" will be run for a beta release. The default value is \"./dist/beta\". A beta release will contain all definitions that are reachable from the entry point, except definitions marked as @alpha or @internal.",
44-
"type": "string"
45-
},
46-
"publishFolderForPublic": {
47-
"description": "This setting is only used if \"dtsRollupTrimming\" is true. It indicates the folder where \"npm publish\" will be run for a public release. The default value is \"./dist/public\". A public release will contain all definitions that are reachable from the entry point, except definitions marked as @beta, @alpha, or @internal.",
48-
"type": "string"
49-
},
50-
"skipLibCheck": {
51-
"description": "This option causes the typechecker to be invoked with the --skipLibCheck option. This option is not recommended and may cause API Extractor to produce incomplete or incorrect declarations, but it may be required when dependencies contain declarations that are incompatible with the TypeScript engine that API Extractor uses for its analysis. If this option is used, it is strongly recommended that broken dependencies be fixed or upgraded.",
52-
"type": "boolean"
5312
}
5413
},
5514
"required": [ "enabled" ],

stack/rush-stack-compiler-shared/src/shared/ApiExtractorRunner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
IExtractorOptions,
88
IExtractorConfig
99
} from '@microsoft/api-extractor';
10+
import * as ApiExtractor from '@microsoft/api-extractor';
1011

1112
import { RushStackCompilerBase } from './RushStackCompilerBase';
1213
import { ToolPaths } from './ToolPaths';
@@ -18,6 +19,7 @@ import { ToolPaths } from './ToolPaths';
1819
* @beta
1920
*/
2021
export class ApiExtractorRunner extends RushStackCompilerBase {
22+
public static apiExtractor: typeof ApiExtractor = ApiExtractor;
2123
private _extractorConfig: IExtractorConfig;
2224
private _extractorOptions: IExtractorOptions;
2325

0 commit comments

Comments
 (0)