Skip to content

Commit fe225d2

Browse files
committed
Add an "api-extractor init" command that writes out api-extractor-template.json
1 parent 4bdaef3 commit fe225d2

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ import { CommandLineParser, CommandLineFlagParameter } from '@microsoft/ts-comma
88
import { InternalError } from '@microsoft/node-core-library';
99

1010
import { RunAction } from './RunAction';
11+
import { InitAction } from './InitAction';
1112

1213
export class ApiExtractorCommandLine extends CommandLineParser {
1314
private _debugParameter: CommandLineFlagParameter;
1415

1516
constructor() {
1617
super({
1718
toolFilename: 'api-extractor',
18-
toolDescription: 'This is an experimental command line interface for the API Extractor tool.'
19+
toolDescription: 'API Extractor helps you build better TypeScript libraries. It analyzes the main entry'
20+
+ ' point for your package, collects the inventory of exported declarations, and then generates three kinds'
21+
+ ' of output: an API report file (.api.md) to facilitate reviews, a declaration rollup (.d.ts) to be'
22+
+ ' published with your NPM package, and a doc model file (.api.json) to be used with a documentation'
23+
+ ' tool such as api-documenter. For details, please visit the web site.'
1924
});
2025
this._populateActions();
2126
}
@@ -46,6 +51,7 @@ export class ApiExtractorCommandLine extends CommandLineParser {
4651
}
4752

4853
private _populateActions(): void {
54+
this.addAction(new InitAction(this));
4955
this.addAction(new RunAction(this));
5056
}
5157
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as colors from 'colors';
5+
import * as path from 'path';
6+
import { FileSystem } from '@microsoft/node-core-library';
7+
import { CommandLineAction } from '@microsoft/ts-command-line';
8+
9+
import { ApiExtractorCommandLine } from './ApiExtractorCommandLine';
10+
11+
const AE_CONFIG_FILENAME: string = 'api-extractor.json';
12+
13+
export class InitAction extends CommandLineAction {
14+
15+
constructor(parser: ApiExtractorCommandLine) {
16+
super({
17+
actionName: 'init',
18+
summary: `Create an ${AE_CONFIG_FILENAME} config file`,
19+
documentation: `Use this command when setting up API Extractor for a new project. It writes an`
20+
+ ` ${AE_CONFIG_FILENAME} config file template with code comments that describe all the settings.`
21+
+ ` The file will be written in the current directory.`
22+
});
23+
}
24+
25+
protected onDefineParameters(): void { // override
26+
// No parameters yet
27+
}
28+
29+
protected onExecute(): Promise<void> { // override
30+
const inputFilePath: string = path.resolve(__dirname, '../schemas/api-extractor-template.json');
31+
const outputFilePath: string = path.resolve(AE_CONFIG_FILENAME);
32+
33+
if (FileSystem.exists(outputFilePath)) {
34+
console.log(colors.red('The output file already exists:'));
35+
console.log('\n ' + outputFilePath + '\n');
36+
throw new Error('Unable to write output file');
37+
}
38+
39+
console.log(colors.green('Writing file: ') + outputFilePath);
40+
FileSystem.copyFile({
41+
sourcePath: inputFilePath,
42+
destinationPath: outputFilePath
43+
});
44+
45+
console.log('\nThe recommended location for this file is in the project\'s "config" subfolder,\n'
46+
+ 'or else in the top-level folder with package.json.');
47+
48+
return Promise.resolve();
49+
}
50+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
*
123123
* DEFAULT VALUE: "./temp/<packageBaseName>.api.json"
124124
*/
125-
"apiJsonFilePath": "./temp/<packageBaseName>.api.json"
125+
// "apiJsonFilePath": "./temp/<packageBaseName>.api.json"
126126
},
127127

128128
/**
@@ -146,7 +146,7 @@
146146
*
147147
* DEFAULT VALUE: "./dist/<packageBaseName>.d.ts"
148148
*/
149-
"untrimmedFilePath": "./dist/<packageBaseName>.d.ts",
149+
// "untrimmedFilePath": "./dist/<packageBaseName>.d.ts",
150150

151151
/**
152152
* Specifies the output path for a .d.ts rollup file to be generated with trimming for a "beta" release.

0 commit comments

Comments
 (0)