Skip to content

Commit 9682ac2

Browse files
authored
Merge pull request microsoft#1225 from Microsoft/octogonz/ae-tsconfigfilepath
[api-extractor] Add a new setting "tsconfigFilePath"
2 parents 83917c9 + eab5e3e commit 9682ac2

10 files changed

Lines changed: 117 additions & 33 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class CompilerState {
5252
let tsconfig: {} | undefined = extractorConfig.overrideTsconfig;
5353
if (!tsconfig) {
5454
// If it wasn't overridden, then load it from disk
55-
tsconfig = JsonFile.load(path.join(extractorConfig.projectFolder, 'tsconfig.json'));
55+
tsconfig = JsonFile.load(extractorConfig.tsconfigFilePath);
5656
}
5757

5858
const commandLine: ts.ParsedCommandLine = ts.parseJsonConfigFileContent(

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

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ export interface IExtractorConfigPrepareOptions {
9292
interface IExtractorConfigParameters {
9393
projectFolder: string;
9494
packageJson: INodePackageJson | undefined;
95-
packageJsonFullPath: string | undefined;
95+
packageFolder: string | undefined;
9696
mainEntryPointFile: string;
97+
tsconfigFilePath: string;
9798
overrideTsconfig: { } | undefined;
9899
skipLibCheck: boolean;
99100
apiReportEnabled: boolean;
@@ -142,14 +143,17 @@ export class ExtractorConfig {
142143
public readonly packageJson: INodePackageJson | undefined;
143144

144145
/**
145-
* The absolute path of the file that package.json was loaded from, or undefined if API Extractor was invoked without
146-
* a package.json file.
146+
* The absolute path of the folder containing the package.json file for the working package, or undefined
147+
* if API Extractor was invoked without a package.json file.
147148
*/
148-
public readonly packageJsonFullPath: string | undefined;
149+
public readonly packageFolder: string | undefined;
149150

150151
/** {@inheritDoc IConfigFile.mainEntryPointFile} */
151152
public readonly mainEntryPointFile: string;
152153

154+
/** {@inheritDoc IConfigCompiler.tsconfigFilePath} */
155+
public readonly tsconfigFilePath: string;
156+
153157
/** {@inheritDoc IConfigCompiler.overrideTsconfig} */
154158
public readonly overrideTsconfig: { } | undefined;
155159

@@ -192,8 +196,9 @@ export class ExtractorConfig {
192196
private constructor(parameters: IExtractorConfigParameters) {
193197
this.projectFolder = parameters.projectFolder;
194198
this.packageJson = parameters.packageJson;
195-
this.packageJsonFullPath = parameters.packageJsonFullPath;
199+
this.packageFolder = parameters.packageFolder;
196200
this.mainEntryPointFile = parameters.mainEntryPointFile;
201+
this.tsconfigFilePath = parameters.tsconfigFilePath;
197202
this.overrideTsconfig = parameters.overrideTsconfig;
198203
this.skipLibCheck = parameters.skipLibCheck;
199204
this.apiReportEnabled = parameters.apiReportEnabled;
@@ -270,8 +275,8 @@ export class ExtractorConfig {
270275
do {
271276
// Check if this file was already processed.
272277
if (visitedPaths.has(currentConfigFilePath)) {
273-
throw new Error(`The API Extractor config files contain a cycle. "${currentConfigFilePath}"`
274-
+ ` is included twice. Please check the "extends" values in config files.`);
278+
throw new Error(`The API Extractor "extends" setting contains a cycle.`
279+
+ ` This file is included twice: "${currentConfigFilePath}"`);
275280
}
276281
visitedPaths.add(currentConfigFilePath);
277282

@@ -342,6 +347,13 @@ export class ExtractorConfig {
342347
'mainEntryPointFile', configFile.mainEntryPointFile, currentConfigFolderPath);
343348
}
344349

350+
if (configFile.compiler) {
351+
if (configFile.compiler.tsconfigFilePath) {
352+
configFile.compiler.tsconfigFilePath = ExtractorConfig._resolveConfigFileRelativePath(
353+
'tsconfigFilePath', configFile.compiler.tsconfigFilePath, currentConfigFolderPath);
354+
}
355+
}
356+
345357
if (configFile.apiReport) {
346358
if (configFile.apiReport.reportFolder) {
347359
configFile.apiReport.reportFolder = ExtractorConfig._resolveConfigFileRelativePath(
@@ -412,27 +424,31 @@ export class ExtractorConfig {
412424

413425
if (options.configObjectFullPath) {
414426
if (!path.isAbsolute(options.configObjectFullPath)) {
415-
throw new Error('configObjectFullPath must be an absolute path');
427+
throw new Error('The "configObjectFullPath" setting must be an absolute path');
416428
}
417429
}
418430

419431
ExtractorConfig.jsonSchema.validateObject(configObject, filenameForErrors);
420432

421-
let packageJson: INodePackageJson | undefined = undefined;
422433
const packageJsonFullPath: string | undefined = options.packageJsonFullPath;
434+
let packageFolder: string | undefined = undefined;
435+
let packageJson: INodePackageJson | undefined = undefined;
436+
423437
if (packageJsonFullPath) {
424438
if (!/.json$/i.test(packageJsonFullPath)) {
425439
// Catch common mistakes e.g. where someone passes a folder path instead of a file path
426-
throw new Error('The packageJsonFullPath does not have a .json file extension');
440+
throw new Error('The "packageJsonFullPath" setting does not have a .json file extension');
427441
}
428442
if (!path.isAbsolute(packageJsonFullPath)) {
429-
throw new Error('packageJsonFullPath must be an absolute path');
443+
throw new Error('The "packageJsonFullPath" setting must be an absolute path');
430444
}
431445

432446
if (!options.packageJson) {
433447
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
434448
packageJson = packageJsonLookup.loadNodePackageJson(packageJsonFullPath);
435449
}
450+
451+
packageFolder = path.dirname(packageJsonFullPath);
436452
}
437453

438454
try {
@@ -444,14 +460,16 @@ export class ExtractorConfig {
444460

445461
if (!configObject.projectFolder) {
446462
// A merged configuration should have this
447-
throw new Error('The "projectFolder" field is missing');
463+
throw new Error('The "projectFolder" setting is missing');
448464
}
449465

450466
let projectFolder: string;
451467
if (configObject.projectFolder.trim() === '<lookup>') {
452468
if (!options.configObjectFullPath) {
453-
throw new Error('The "<lookup>" token cannot be expanded because configObjectFullPath was not specified');
469+
throw new Error('The "projectFolder" setting uses the "<lookup>" token, but it cannot be expanded because'
470+
+ ' the "configObjectFullPath" setting was not specified');
454471
}
472+
455473
// "The default value for `projectFolder` is the token `<lookup>`, which means the folder is determined
456474
// by traversing parent folders, starting from the folder containing api-extractor.json, and stopping
457475
// at the first folder that contains a tsconfig.json file. If a tsconfig.json file cannot be found in
@@ -466,7 +484,7 @@ export class ExtractorConfig {
466484
}
467485
const parentFolder: string = path.dirname(currentFolder);
468486
if (parentFolder === '' || parentFolder === currentFolder) {
469-
throw new Error('The projectFolder was set to "<lookup>", but a tsconfig.json file cannot be'
487+
throw new Error('The "projectFolder" setting uses the "<lookup>" token, but a tsconfig.json file cannot be'
470488
+ ' found in this folder or any parent folder.');
471489
}
472490
currentFolder = parentFolder;
@@ -475,7 +493,7 @@ export class ExtractorConfig {
475493
ExtractorConfig._rejectAnyTokensInPath(configObject.projectFolder, 'projectFolder');
476494

477495
if (!FileSystem.exists(configObject.projectFolder)) {
478-
throw new Error('The specified projectFolder does not exist: ' + configObject.projectFolder);
496+
throw new Error('The specified "projectFolder" path does not exist: ' + configObject.projectFolder);
479497
}
480498

481499
projectFolder = configObject.projectFolder;
@@ -494,17 +512,29 @@ export class ExtractorConfig {
494512

495513
if (!configObject.mainEntryPointFile) {
496514
// A merged configuration should have this
497-
throw new Error('mainEntryPointFile is missing');
515+
throw new Error('The "mainEntryPointFile" setting is missing');
498516
}
499517
const mainEntryPointFile: string = ExtractorConfig._resolvePathWithTokens('mainEntryPointFile',
500518
configObject.mainEntryPointFile, tokenContext);
501519

502520
if (!ExtractorConfig.hasDtsFileExtension(mainEntryPointFile)) {
503-
throw new Error('The mainEntryPointFile is not a declaration file: ' + mainEntryPointFile);
521+
throw new Error('The "mainEntryPointFile" value is not a declaration file: ' + mainEntryPointFile);
504522
}
505523

506524
if (!FileSystem.exists(mainEntryPointFile)) {
507-
throw new Error('The mainEntryPointFile does not exist: ' + mainEntryPointFile);
525+
throw new Error('The "mainEntryPointFile" path does not exist: ' + mainEntryPointFile);
526+
}
527+
528+
const tsconfigFilePath: string = ExtractorConfig._resolvePathWithTokens('tsconfigFilePath',
529+
configObject.compiler.tsconfigFilePath, tokenContext);
530+
531+
if (configObject.compiler.overrideTsconfig === undefined) {
532+
if (!tsconfigFilePath) {
533+
throw new Error('Either the "tsconfigFilePath" or "overrideTsconfig" setting must be specified');
534+
}
535+
if (!FileSystem.exists(tsconfigFilePath)) {
536+
throw new Error('The file referenced by "tsconfigFilePath" does not exist: ' + tsconfigFilePath);
537+
}
508538
}
509539

510540
let apiReportEnabled: boolean = false;
@@ -518,11 +548,11 @@ export class ExtractorConfig {
518548

519549
if (!reportFilename) {
520550
// A merged configuration should have this
521-
throw new Error('reportFilename is missing');
551+
throw new Error('The "reportFilename" setting is missing');
522552
}
523553
if (reportFilename.indexOf('/') >= 0 || reportFilename.indexOf('\\') >= 0) {
524554
// A merged configuration should have this
525-
throw new Error(`The reportFilename contains invalid characters: "${reportFilename}"`);
555+
throw new Error(`The "reportFilename" setting contains invalid characters: "${reportFilename}"`);
526556
}
527557

528558
const reportFolder: string = ExtractorConfig._resolvePathWithTokens('reportFolder',
@@ -552,11 +582,11 @@ export class ExtractorConfig {
552582

553583
if (tsdocMetadataFilePath.trim() === '<lookup>') {
554584
if (!packageJson) {
555-
throw new Error('The "<lookup>" token cannot be used with compiler.projectFolder because'
556-
+ 'the "packageJson" option was not provided');
585+
throw new Error('The "<lookup>" token cannot be used with the "tsdocMetadataFilePath" setting because'
586+
+ ' the "packageJson" option was not provided');
557587
}
558588
if (!packageJsonFullPath) {
559-
throw new Error('The "<lookup>" token cannot be used with compiler.projectFolder because'
589+
throw new Error('The "<lookup>" token cannot be used with "tsdocMetadataFilePath" because'
560590
+ 'the "packageJsonFullPath" option was not provided');
561591
}
562592
tsdocMetadataFilePath = PackageMetadataManager.resolveTsdocMetadataPath(
@@ -569,7 +599,8 @@ export class ExtractorConfig {
569599
}
570600

571601
if (!tsdocMetadataFilePath) {
572-
throw new Error('The tsdocMetadata.enabled was specified, but tsdocMetadataFilePath is not specified');
602+
throw new Error('The "tsdocMetadata.enabled" setting is enabled,'
603+
+ ' but "tsdocMetadataFilePath" is not specified');
573604
}
574605
}
575606
}
@@ -592,8 +623,9 @@ export class ExtractorConfig {
592623
return new ExtractorConfig({
593624
projectFolder: projectFolder,
594625
packageJson,
595-
packageJsonFullPath,
626+
packageFolder,
596627
mainEntryPointFile,
628+
tsconfigFilePath,
597629
overrideTsconfig: configObject.compiler.overrideTsconfig,
598630
skipLibCheck: !!configObject.compiler.skipLibCheck,
599631
apiReportEnabled,
@@ -641,12 +673,12 @@ export class ExtractorConfig {
641673

642674
if (value.indexOf(projectFolderToken) >= 0) {
643675
// If after all replacements, "<projectFolder>" appears somewhere in the string, report an error
644-
throw new Error(`The ${fieldName} value incorrectly uses the "<projectFolder>" token.`
676+
throw new Error(`The "${fieldName}" value incorrectly uses the "<projectFolder>" token.`
645677
+ ` It must appear at the start of the string.`);
646678
}
647679

648680
if (value.indexOf('<lookup>') >= 0) {
649-
throw new Error(`The ${fieldName} value incorrectly uses the "<lookup>" token`);
681+
throw new Error(`The "${fieldName}" value incorrectly uses the "<lookup>" token`);
650682
}
651683
ExtractorConfig._rejectAnyTokensInPath(value, fieldName);
652684
}
@@ -673,8 +705,8 @@ export class ExtractorConfig {
673705
const tokenRegExp: RegExp = /(\<[^<]*?\>)/;
674706
const match: RegExpExecArray | null = tokenRegExp.exec(value);
675707
if (match) {
676-
throw new Error(`The ${fieldName} value contains an unrecognized token "${match[1]}"`);
708+
throw new Error(`The "${fieldName}" value contains an unrecognized token "${match[1]}"`);
677709
}
678-
throw new Error(`The ${fieldName} value contains extra token characters ("<" or ">"): ${value}`);
710+
throw new Error(`The "${fieldName}" value contains extra token characters ("<" or ">"): ${value}`);
679711
}
680712
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ import { ExtractorLogLevel } from './ExtractorLogLevel';
1212
* @public
1313
*/
1414
export interface IConfigCompiler {
15+
/**
16+
* Specifies the path to the tsconfig.json file to be used by API Extractor when analyzing the project.
17+
*
18+
* @remarks
19+
* The path is resolved relative to the folder of the config file that contains the setting; to change this,
20+
* prepend a folder token such as `<projectFolder>`.
21+
*
22+
* Note: This setting will be ignored if `overrideTsconfig` is used.
23+
*/
24+
tsconfigFilePath?: string;
1525

1626
/**
1727
* Provides already parsed tsconfig.json contents.

‎apps/api-extractor/src/collector/Collector.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ export class Collector {
9595
throw new Error('Unable to load file: ' + extractorConfig.mainEntryPointFile);
9696
}
9797

98-
if (!extractorConfig.packageJsonFullPath || !extractorConfig.packageJson) {
98+
if (!extractorConfig.packageFolder || !extractorConfig.packageJson) {
9999
// TODO: We should be able to analyze projects that don't have any package.json.
100100
// The ExtractorConfig class is already designed to allow this.
101101
throw new Error('Unable to find a package.json file for the project being analyzed');
102102
}
103103

104104
this.workingPackage = new WorkingPackage({
105-
packageFolder: extractorConfig.packageJsonFullPath,
105+
packageFolder: extractorConfig.packageFolder,
106106
packageJson: extractorConfig.packageJson,
107107
entryPointSourceFile
108108
});

‎apps/api-extractor/src/schemas/api-extractor-defaults.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"projectFolder": "<lookup>",
55

66
"compiler": {
7+
"tsconfigFilePath": "<projectFolder>/tsconfig.json",
78
"skipLibCheck": false
89
},
910

‎apps/api-extractor/src/schemas/api-extractor-template.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151
* Determines how the TypeScript compiler engine will be invoked by API Extractor.
5252
*/
5353
"compiler": {
54+
/**
55+
* Specifies the path to the tsconfig.json file to be used by API Extractor when analyzing the project.
56+
*
57+
* The path is resolved relative to the folder of the config file that contains the setting; to change this,
58+
* prepend a folder token such as "<projectFolder>".
59+
*
60+
* Note: This setting will be ignored if "overrideTsconfig" is used.
61+
*
62+
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
63+
* DEFAULT VALUE: "<projectFolder>/tsconfig.json"
64+
*/
65+
// "tsconfigFilePath": "<projectFolder>/tsconfig.json",
66+
5467
/**
5568
* Provides compiler configuration that will be used instead of reading the tsconfig.json file from disk.
5669
* The object must conform to the TypeScript tsconfig schema:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"description": "Determines how the TypeScript compiler engine will be invoked by API Extractor.",
2828
"type": "object",
2929
"properties": {
30+
"tsconfigFilePath": {
31+
"description": " Specifies the path to the tsconfig.json file to be used by API Extractor when analyzing the project. The path is resolved relative to the folder of the config file that contains the setting; to change this, prepend a folder token such as \"<projectFolder>\". Note: This setting will be ignored if \"overrideTsconfig\" is used.",
32+
"type": "string"
33+
},
3034
"overrideTsconfig": {
3135
"description": "Provides already parsed tsconfig.json contents conforming to the TypeScript tsconfig schema: http://json.schemastore.org/tsconfig If omitted, then the tsconfig.json file will be read from the \"projectFolder\".",
3236
"type": "object"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Add a new api-extractor.json setting \"tsconfigFilePath\" for customizing the tsconfig.json path",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Replace ExtractorConfig.packageJsonFullPath with ExtractorConfig.packageFolder",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export class ExtractorConfig {
5151
readonly mainEntryPointFile: string;
5252
readonly messages: IExtractorMessagesConfig;
5353
readonly overrideTsconfig: {} | undefined;
54+
readonly packageFolder: string | undefined;
5455
readonly packageJson: INodePackageJson | undefined;
55-
readonly packageJsonFullPath: string | undefined;
5656
static prepare(options: IExtractorConfigPrepareOptions): ExtractorConfig;
5757
readonly projectFolder: string;
5858
readonly publicTrimmedFilePath: string;
@@ -61,6 +61,7 @@ export class ExtractorConfig {
6161
readonly rollupEnabled: boolean;
6262
readonly skipLibCheck: boolean;
6363
readonly testMode: boolean;
64+
readonly tsconfigFilePath: string;
6465
readonly tsdocMetadataEnabled: boolean;
6566
readonly tsdocMetadataFilePath: string;
6667
readonly untrimmedFilePath: string;
@@ -150,6 +151,7 @@ export interface IConfigApiReport {
150151
export interface IConfigCompiler {
151152
overrideTsconfig?: {};
152153
skipLibCheck?: boolean;
154+
tsconfigFilePath?: string;
153155
}
154156

155157
// @public

0 commit comments

Comments
 (0)