@microsoft/api-extractor
Version:
Validate, document, and review the exported API for a TypeScript library
111 lines • 6.14 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
const colors = require("colors");
const os = require("os");
const path = require("path");
const node_core_library_1 = require("@microsoft/node-core-library");
const ts_command_line_1 = require("@microsoft/ts-command-line");
const Extractor_1 = require("../extractor/Extractor");
const AE_CONFIG_FILENAME = 'api-extractor.json';
class RunAction extends ts_command_line_1.CommandLineAction {
constructor(parser) {
super({
actionName: 'run',
summary: 'Invoke API Extractor on a project',
documentation: 'Invoke API Extractor on a project'
});
this._parser = parser;
}
onDefineParameters() {
this._configFileParameter = this.defineStringParameter({
parameterLongName: '--config',
parameterShortName: '-c',
argumentName: 'FILE',
description: `Use the specified ${AE_CONFIG_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.ts'
+ ' review file is automatically copied in a local build.'
});
this._typescriptCompilerFolder = this.defineStringParameter({
parameterLongName: '--typescript-compiler-folder',
argumentName: 'PATH',
description: 'By default API Extractor uses its own TypeScript compiler version to analyze your project.'
+ ' This can often cause compiler errors due to incompatibilities between different TS versions.'
+ ' Use "--typescript-compiler-folder" to specify the folder path for your compiler version.'
});
this._skipLibCheck = this.defineFlagParameter({
parameterLongName: '--skip-lib-check',
description: 'This flag causes the typechecker to be invoked with the --skipLibCheck option. This option is not'
+ ' recommended and may cause API Extractor to produce incomplete or incorrect declarations, but it '
+ ' may be required when dependencies contain declarations that are incompatible with the TypeScript engine'
+ ' that API Extractor uses for its analysis. If this option is used, it is strongly recommended that broken'
+ ' dependencies be fixed or upgraded.'
});
}
onExecute() {
const lookup = new node_core_library_1.PackageJsonLookup();
let configFilename;
let typescriptCompilerFolder = this._typescriptCompilerFolder.value;
if (typescriptCompilerFolder) {
typescriptCompilerFolder = path.normalize(typescriptCompilerFolder);
if (node_core_library_1.FileSystem.exists(typescriptCompilerFolder)) {
typescriptCompilerFolder = lookup.tryGetPackageFolderFor(typescriptCompilerFolder);
const typescriptCompilerPackageJson = 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 (!node_core_library_1.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 = lookup.tryGetPackageFolderFor('.');
if (packageFolder) {
configFilename = path.join(packageFolder, AE_CONFIG_FILENAME);
}
else {
// If there is no package, then try the current directory
configFilename = path.join(process.cwd(), AE_CONFIG_FILENAME);
}
if (!node_core_library_1.FileSystem.exists(configFilename)) {
throw new Error(`Unable to find an ${AE_CONFIG_FILENAME} file`);
}
console.log(`Using configuration from ${configFilename}` + os.EOL + os.EOL);
}
const config = node_core_library_1.JsonFile.loadAndValidate(configFilename, Extractor_1.Extractor.jsonSchema);
const extractor = new Extractor_1.Extractor(config, {
localBuild: this._localParameter.value,
typescriptCompilerFolder: typescriptCompilerFolder,
skipLibCheck: this._skipLibCheck.value
});
if (!extractor.processProject()) {
console.log(os.EOL + colors.yellow('API Extractor completed with errors or warnings'));
process.exitCode = 1;
}
return Promise.resolve();
}
}
exports.RunAction = RunAction;
//# sourceMappingURL=RunAction.js.map