forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAction.ts
More file actions
173 lines (148 loc) · 6.93 KB
/
RunAction.ts
File metadata and controls
173 lines (148 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as colors from 'colors';
import * as os from 'os';
import * as path from 'path';
import {
PackageJsonLookup,
FileSystem,
IPackageJson
} from '@rushstack/node-core-library';
import {
CommandLineAction,
CommandLineStringParameter,
CommandLineFlagParameter
} from '@rushstack/ts-command-line';
import { Extractor, ExtractorResult } from '../api/Extractor';
import { IConfigFile } from '../api/IConfigFile';
import { ApiExtractorCommandLine } from './ApiExtractorCommandLine';
import { ExtractorConfig } from '../api/ExtractorConfig';
export class RunAction extends CommandLineAction {
private _configFileParameter: CommandLineStringParameter;
private _localParameter: CommandLineFlagParameter;
private _verboseParameter: CommandLineFlagParameter;
private _diagnosticsParameter: CommandLineFlagParameter;
private _typescriptCompilerFolder: CommandLineStringParameter;
public constructor(parser: ApiExtractorCommandLine) {
super({
actionName: 'run',
summary: 'Invoke API Extractor on a project',
documentation: 'Invoke API Extractor on a project'
});
}
protected onDefineParameters(): void { // override
this._configFileParameter = this.defineStringParameter({
parameterLongName: '--config',
parameterShortName: '-c',
argumentName: 'FILE',
description: `Use the specified ${ExtractorConfig.FILENAME} file path, rather than guessing its location`
});
this._localParameter = this.defineFlagParameter({
parameterLongName: '--local',
parameterShortName: '-l',
description: 'Indicates that API Extractor is running as part of a local build,'
+ ' e.g. on a developer\'s machine. This disables certain validation that would'
+ ' normally be performed for a ship/production build. For example, the *.api.md'
+ ' report file is automatically copied in a local build.'
});
this._verboseParameter = this.defineFlagParameter({
parameterLongName: '--verbose',
parameterShortName: '-v',
description: 'Show additional informational messages in the output.'
});
this._diagnosticsParameter = this.defineFlagParameter({
parameterLongName: '--diagnostics',
description: 'Show diagnostic messages used for troubleshooting problems with API Extractor.'
+ ' This flag also enables the "--verbose" flag.'
});
this._typescriptCompilerFolder = this.defineStringParameter({
parameterLongName: '--typescript-compiler-folder',
argumentName: 'PATH',
description: 'API Extractor uses its own TypeScript compiler engine to analyze your project. If your project'
+ ' is built with a significantly different TypeScript version, sometimes API Extractor may report compilation'
+ ' errors due to differences in the system typings (e.g. lib.dom.d.ts). You can use the'
+ ' "--typescriptCompilerFolder" option to specify the folder path where you installed the TypeScript package,'
+ ' and API Extractor\'s compiler will use those system typings instead.'
});
}
protected onExecute(): Promise<void> { // override
const lookup: PackageJsonLookup = new PackageJsonLookup();
let configFilename: string;
let typescriptCompilerFolder: string | undefined = this._typescriptCompilerFolder.value;
if (typescriptCompilerFolder) {
typescriptCompilerFolder = path.normalize(typescriptCompilerFolder);
if (FileSystem.exists(typescriptCompilerFolder)) {
typescriptCompilerFolder = lookup.tryGetPackageFolderFor(typescriptCompilerFolder);
const typescriptCompilerPackageJson: IPackageJson | undefined = typescriptCompilerFolder
? lookup.tryLoadPackageJsonFor(typescriptCompilerFolder)
: undefined;
if (!typescriptCompilerPackageJson) {
throw new Error(
`The path specified in the ${this._typescriptCompilerFolder.longName} parameter is not a package.`
);
} else if (typescriptCompilerPackageJson.name !== 'typescript') {
throw new Error(
`The path specified in the ${this._typescriptCompilerFolder.longName} parameter is not a TypeScript`
+ ' compiler package.'
);
}
} else {
throw new Error(
`The path specified in the ${this._typescriptCompilerFolder.longName} parameter does not exist.`
);
}
}
if (this._configFileParameter.value) {
configFilename = path.normalize(this._configFileParameter.value);
if (!FileSystem.exists(configFilename)) {
throw new Error('Config file not found: ' + this._configFileParameter.value);
}
} else {
// Otherwise, figure out which project we're in and look for the config file
// at the project root
const packageFolder: string | undefined = lookup.tryGetPackageFolderFor('.');
// If there is no package, then try the current directory
const baseFolder: string = packageFolder ? packageFolder : process.cwd();
// First try the standard "config" subfolder:
configFilename = path.join(baseFolder, 'config', ExtractorConfig.FILENAME);
if (FileSystem.exists(configFilename)) {
if (FileSystem.exists(path.join(baseFolder, ExtractorConfig.FILENAME))) {
throw new Error(`Found conflicting ${ExtractorConfig.FILENAME} files in "." and "./config" folders`);
}
} else {
// Otherwise try the top-level folder
configFilename = path.join(baseFolder, ExtractorConfig.FILENAME);
if (!FileSystem.exists(configFilename)) {
throw new Error(`Unable to find an ${ExtractorConfig.FILENAME} file`);
}
}
console.log(`Using configuration from ${configFilename}` + os.EOL);
}
const configObjectFullPath: string = path.resolve(configFilename);
const configObject: IConfigFile = ExtractorConfig.loadFile(configObjectFullPath);
const extractorConfig: ExtractorConfig = ExtractorConfig.prepare({
configObject: configObject,
configObjectFullPath: configObjectFullPath,
packageJsonFullPath: lookup.tryGetPackageJsonFilePathFor(configObjectFullPath)
});
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig,
{
localBuild: this._localParameter.value,
showVerboseMessages: this._verboseParameter.value,
showDiagnostics: this._diagnosticsParameter.value,
typescriptCompilerFolder: typescriptCompilerFolder
}
);
if (extractorResult.succeeded) {
console.log(os.EOL + 'API Extractor completed successfully');
} else {
process.exitCode = 1;
if (extractorResult.errorCount > 0) {
console.log(os.EOL + colors.red('API Extractor completed with errors'));
} else {
console.log(os.EOL + colors.yellow('API Extractor completed with warnings'));
}
}
return Promise.resolve();
}
}