Skip to content

Commit 969f4fc

Browse files
committed
Api extractor changes to support extends feature in api extractor config file
1 parent 6005260 commit 969f4fc

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

apps/api-extractor/src/cli/RunAction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as colors from 'colors';
55
import * as os from 'os';
66
import * as path from 'path';
77
import {
8-
JsonFile,
98
PackageJsonLookup,
109
FileSystem,
1110
IPackageJson
@@ -136,7 +135,7 @@ export class RunAction extends CommandLineAction {
136135
console.log(`Using configuration from ${configFilename}` + os.EOL + os.EOL);
137136
}
138137

139-
const config: IExtractorConfig = JsonFile.loadAndValidate(configFilename, Extractor.jsonSchema);
138+
const config: IExtractorConfig = Extractor.loadConfigObject(configFilename);
140139
const extractor: Extractor = new Extractor(
141140
config,
142141
{

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,50 @@ export class Extractor {
164164
* @param options - IExtractor options.
165165
*/
166166
public static processProjectFromConfigFile(jsonConfigFile: string, options?: IExtractorOptions): void {
167-
const configObject: IExtractorConfig = JsonFile.loadAndValidate(jsonConfigFile, Extractor.jsonSchema);
167+
const configObject: IExtractorConfig = this.loadConfigObject(jsonConfigFile);
168168
const extractor: Extractor = new Extractor(configObject, options);
169169
extractor.processProject();
170170
}
171171

172+
/**
173+
* Loads the api extractor config file in Extractor Config object.
174+
* @param jsonConfigFile - Path to api extractor json config file.
175+
*/
176+
public static loadConfigObject(jsonConfigFile: string): IExtractorConfig {
177+
// Set to keep track of config files which have been processed.
178+
const pathSet: Set<string> = new Set<string>();
179+
// Get absolute path of config file.
180+
let currentConfigFilePath: string = path.resolve(jsonConfigFile);
181+
pathSet.add(currentConfigFilePath);
182+
183+
let extractorConfig: IExtractorConfig = JsonFile.load(jsonConfigFile);
184+
185+
while (extractorConfig.extends) {
186+
// Populate the api extractor config path defined in extends relative to current config path.
187+
currentConfigFilePath = path.resolve(path.dirname(currentConfigFilePath), extractorConfig.extends);
188+
189+
// Check if this file was already processed.
190+
if (pathSet.has(currentConfigFilePath)) {
191+
throw new Error('The api extractor config files contains a cycle. '
192+
+ currentConfigFilePath + ' is being processed again. '
193+
+ 'Please check the extends values in config files.');
194+
}
195+
pathSet.add(currentConfigFilePath);
196+
197+
// Remove extends property from config for current config.
198+
delete extractorConfig.extends;
199+
200+
// Load the extractor config defined in extends property.
201+
const baseConfig: IExtractorConfig = JsonFile.load(currentConfigFilePath);
202+
lodash.merge(baseConfig, extractorConfig);
203+
extractorConfig = baseConfig;
204+
}
205+
206+
// Validate if the extractor config generated adheres to schema.
207+
Extractor.jsonSchema.validateObject(extractorConfig, jsonConfigFile);
208+
return extractorConfig;
209+
}
210+
172211
private static _applyConfigDefaults(config: IExtractorConfig): IExtractorConfig {
173212
// Use the provided config to override the defaults
174213
const normalized: IExtractorConfig = lodash.merge(

apps/api-extractor/src/extractor/IExtractorConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ export interface IExtractorDtsRollupConfig {
261261
* @public
262262
*/
263263
export interface IExtractorConfig {
264+
extends?: string;
265+
264266
/**
265267
* Determines how the TypeScript compiler will be invoked.
266268
* The compiler.configType selects the type of configuration;

apps/api-extractor/src/extractor/api-extractor.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"type": "string"
99
},
1010

11+
"extends": {
12+
"description": "Optional field. Path to json config file from which config should be extended",
13+
"type": "string"
14+
},
15+
1116
"compiler": {
1217
"description": "Determines how the TypeScript compiler will be invoked. The compiler.configType selects the type of configuration. Different options are available according to the configuration type.",
1318
"type": "object",

common/reviews/api/api-extractor.api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Extractor {
1818
analyzeProject(options?: IAnalyzeProjectOptions): void;
1919
static generateFilePathsForAnalysis(inputFilePaths: string[]): string[];
2020
static jsonSchema: JsonSchema;
21+
static loadConfigObject(jsonConfigFile: string): IExtractorConfig;
2122
processProject(options?: IAnalyzeProjectOptions): boolean;
2223
static processProjectFromConfigFile(jsonConfigFile: string, options?: IExtractorOptions): void;
2324
}
@@ -194,6 +195,8 @@ interface IExtractorConfig {
194195
compiler: IExtractorTsconfigCompilerConfig | IExtractorRuntimeCompilerConfig;
195196
// @beta
196197
dtsRollup?: IExtractorDtsRollupConfig;
198+
// (undocumented)
199+
extends?: string;
197200
policies?: IExtractorPoliciesConfig;
198201
project: IExtractorProjectConfig;
199202
validationRules?: IExtractorValidationRulesConfig;

0 commit comments

Comments
 (0)