Skip to content

Commit b8d9506

Browse files
committed
Write a header at the top of the .api.json file
1 parent 623187d commit b8d9506

5 files changed

Lines changed: 78 additions & 12 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
JsonFile,
1111
JsonSchema,
1212
Path,
13-
FileSystem
13+
FileSystem,
14+
IPackageJson,
15+
FileConstants
1416
} from '@microsoft/node-core-library';
1517
import {
1618
IExtractorConfig,
@@ -99,6 +101,32 @@ export class Extractor {
99101
public static jsonSchema: JsonSchema = JsonSchema.fromFile(
100102
path.join(__dirname, '../schemas/api-extractor.schema.json'));
101103

104+
/**
105+
* Returns the version number of the API Extractor NPM package.
106+
*/
107+
public static get version(): string {
108+
return Extractor._getPackageJson().version;
109+
}
110+
111+
/**
112+
* Returns the package name of the API Extractor NPM package.
113+
*/
114+
public static get packageName(): string {
115+
return Extractor._getPackageJson().name;
116+
}
117+
118+
private static _getPackageJson(): IPackageJson {
119+
if (Extractor._apiExtractorPackageJson === undefined) {
120+
const packageJsonFilename: string = path.resolve(path.join(
121+
__dirname, '..', '..', FileConstants.PackageJson)
122+
);
123+
Extractor._apiExtractorPackageJson = JsonFile.load(packageJsonFilename) as IPackageJson;
124+
}
125+
return Extractor._apiExtractorPackageJson;
126+
}
127+
128+
private static _apiExtractorPackageJson: IPackageJson | undefined;
129+
102130
private static _defaultConfig: Partial<IExtractorConfig> = JsonFile.load(path.join(__dirname,
103131
'../schemas/api-extractor-defaults.json'));
104132

‎apps/api-extractor/src/api/model/ApiPackage.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,46 @@ import { ApiItem, ApiItemKind, IApiItemJson } from './ApiItem';
55
import { ApiItemContainerMixin, IApiItemContainerMixinOptions } from '../mixins/ApiItemContainerMixin';
66
import { JsonFile } from '@microsoft/node-core-library';
77
import { ApiDocumentedItem, IApiDocumentedItemOptions } from './ApiDocumentedItem';
8+
import { Extractor } from '../Extractor';
89

910
export interface IApiPackageOptions extends
1011
IApiItemContainerMixinOptions,
1112
IApiDocumentedItemOptions {
1213
}
1314

15+
export enum ApiJsonSchemaVersion {
16+
/**
17+
* The initial release.
18+
*/
19+
V_1000 = 1000
20+
}
21+
22+
export interface IApiPackageMetadataJson {
23+
/**
24+
* The NPM package name for the tool that wrote the *.api.json file.
25+
* For informational purposes only.
26+
*/
27+
toolPackage: string;
28+
/**
29+
* The NPM package version for the tool that wrote the *.api.json file.
30+
* For informational purposes only.
31+
*/
32+
toolVersion: string;
33+
34+
/**
35+
* The *.api.json schema version. Used for determining whether the file format is
36+
* supported, and for backwards compatibility.
37+
*/
38+
schemaVersion: ApiJsonSchemaVersion;
39+
}
40+
41+
export interface IApiPackageJson extends IApiItemJson {
42+
/**
43+
* A file header that stores metadata about the tool that wrote the *.api.json file.
44+
*/
45+
metadata: IApiPackageMetadataJson;
46+
}
47+
1448
export class ApiPackage extends ApiItemContainerMixin(ApiDocumentedItem) {
1549
public static loadFromJsonFile(apiJsonFilename: string): ApiPackage {
1650
const jsonObject: { } = JsonFile.load(apiJsonFilename);
@@ -32,7 +66,13 @@ export class ApiPackage extends ApiItemContainerMixin(ApiDocumentedItem) {
3266
}
3367

3468
public saveToJsonFile(apiJsonFilename: string): void {
35-
const jsonObject: { } = { };
69+
const jsonObject: IApiPackageJson = {
70+
metadata: {
71+
toolPackage: Extractor.packageName,
72+
toolVersion: Extractor.version,
73+
schemaVersion: ApiJsonSchemaVersion.V_1000
74+
}
75+
} as IApiPackageJson;
3676
this.serializeInto(jsonObject);
3777
JsonFile.save(jsonObject, apiJsonFilename);
3878
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33

44
import * as os from 'os';
55
import * as colors from 'colors';
6-
import * as path from 'path';
7-
8-
import { FileConstants } from '@microsoft/node-core-library';
96

107
import { ApiExtractorCommandLine } from './cli/ApiExtractorCommandLine';
8+
import { Extractor } from './api/Extractor';
119

12-
const myPackageJsonFilename: string = path.resolve(path.join(
13-
__dirname, '..', FileConstants.PackageJson)
14-
);
15-
const myPackageJson: { version: string } = require(myPackageJsonFilename);
16-
17-
console.log(os.EOL + colors.bold(`api-extractor ${myPackageJson.version} `
10+
console.log(os.EOL + colors.bold(`api-extractor ${Extractor.version} `
1811
+ colors.cyan(' - http://aka.ms/extractor') + os.EOL));
1912

2013
const parser: ApiExtractorCommandLine = new ApiExtractorCommandLine();

‎apps/api-extractor/tslint.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "@microsoft/rush-stack-compiler/includes/tslint.json"
2+
"extends": "@microsoft/rush-stack-compiler/includes/tslint.json",
3+
"rules": {
4+
"member-ordering": false
5+
}
36
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class Extractor {
66
analyzeProject(options?: IAnalyzeProjectOptions): void;
77
static generateFilePathsForAnalysis(inputFilePaths: string[]): string[];
88
static jsonSchema: JsonSchema;
9+
static readonly packageName: string;
910
processProject(options?: IAnalyzeProjectOptions): boolean;
11+
static readonly version: string;
1012
}
1113

1214
// @public

0 commit comments

Comments
 (0)