|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 19 | +// See LICENSE in the project root for license information. |
| 20 | + |
| 21 | +import * as path from 'path'; |
| 22 | +import * as tsdoc from '@microsoft/tsdoc'; |
| 23 | +import colors from 'colors'; |
| 24 | + |
| 25 | +import { |
| 26 | + CommandLineAction, |
| 27 | + CommandLineStringParameter |
| 28 | +} from '@rushstack/ts-command-line'; |
| 29 | +import { FileSystem } from '@rushstack/node-core-library'; |
| 30 | +import { |
| 31 | + ApiModel, |
| 32 | + ApiItem, |
| 33 | + ApiItemContainerMixin, |
| 34 | + ApiDocumentedItem, |
| 35 | + IResolveDeclarationReferenceResult |
| 36 | +} from '@microsoft/api-extractor-model'; |
| 37 | + |
| 38 | +export interface IBuildApiModelResult { |
| 39 | + apiModel: ApiModel; |
| 40 | + inputFolder: string; |
| 41 | + outputFolder: string; |
| 42 | +} |
| 43 | + |
| 44 | +export abstract class BaseAction extends CommandLineAction { |
| 45 | + private _inputFolderParameter!: CommandLineStringParameter; |
| 46 | + private _outputFolderParameter!: CommandLineStringParameter; |
| 47 | + |
| 48 | + protected onDefineParameters(): void { |
| 49 | + // override |
| 50 | + this._inputFolderParameter = this.defineStringParameter({ |
| 51 | + parameterLongName: '--input-folder', |
| 52 | + parameterShortName: '-i', |
| 53 | + argumentName: 'FOLDER1', |
| 54 | + description: |
| 55 | + `Specifies the input folder containing the *.api.json files to be processed.` + |
| 56 | + ` If omitted, the default is "./input"` |
| 57 | + }); |
| 58 | + |
| 59 | + this._outputFolderParameter = this.defineStringParameter({ |
| 60 | + parameterLongName: '--output-folder', |
| 61 | + parameterShortName: '-o', |
| 62 | + argumentName: 'FOLDER2', |
| 63 | + description: |
| 64 | + `Specifies the output folder where the documentation will be written.` + |
| 65 | + ` ANY EXISTING CONTENTS WILL BE DELETED!` + |
| 66 | + ` If omitted, the default is "./${this.actionName}"` |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + protected buildApiModel(): IBuildApiModelResult { |
| 71 | + const apiModel: ApiModel = new ApiModel(); |
| 72 | + |
| 73 | + const inputFolder: string = this._inputFolderParameter.value || './input'; |
| 74 | + if (!FileSystem.exists(inputFolder)) { |
| 75 | + throw new Error('The input folder does not exist: ' + inputFolder); |
| 76 | + } |
| 77 | + |
| 78 | + const outputFolder: string = |
| 79 | + this._outputFolderParameter.value || `./${this.actionName}`; |
| 80 | + FileSystem.ensureFolder(outputFolder); |
| 81 | + |
| 82 | + for (const filename of FileSystem.readFolder(inputFolder)) { |
| 83 | + if (filename.match(/\.api\.json$/i)) { |
| 84 | + console.log(`Reading ${filename}`); |
| 85 | + const filenamePath: string = path.join(inputFolder, filename); |
| 86 | + apiModel.loadPackage(filenamePath); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + this._applyInheritDoc(apiModel, apiModel); |
| 91 | + |
| 92 | + return { apiModel, inputFolder, outputFolder }; |
| 93 | + } |
| 94 | + |
| 95 | + // TODO: This is a temporary workaround. The long term plan is for API Extractor's DocCommentEnhancer |
| 96 | + // to apply all @inheritDoc tags before the .api.json file is written. |
| 97 | + // See DocCommentEnhancer._applyInheritDoc() for more info. |
| 98 | + private _applyInheritDoc(apiItem: ApiItem, apiModel: ApiModel): void { |
| 99 | + if (apiItem instanceof ApiDocumentedItem) { |
| 100 | + if (apiItem.tsdocComment) { |
| 101 | + const inheritDocTag: tsdoc.DocInheritDocTag | undefined = |
| 102 | + apiItem.tsdocComment.inheritDocTag; |
| 103 | + |
| 104 | + if (inheritDocTag && inheritDocTag.declarationReference) { |
| 105 | + // Attempt to resolve the declaration reference |
| 106 | + const result: IResolveDeclarationReferenceResult = apiModel.resolveDeclarationReference( |
| 107 | + inheritDocTag.declarationReference, |
| 108 | + apiItem |
| 109 | + ); |
| 110 | + |
| 111 | + if (result.errorMessage) { |
| 112 | + console.log( |
| 113 | + colors.yellow( |
| 114 | + `Warning: Unresolved @inheritDoc tag for ${apiItem.displayName}: ` + |
| 115 | + result.errorMessage |
| 116 | + ) |
| 117 | + ); |
| 118 | + } else { |
| 119 | + if ( |
| 120 | + result.resolvedApiItem instanceof ApiDocumentedItem && |
| 121 | + result.resolvedApiItem.tsdocComment && |
| 122 | + result.resolvedApiItem !== apiItem |
| 123 | + ) { |
| 124 | + this._copyInheritedDocs( |
| 125 | + apiItem.tsdocComment, |
| 126 | + result.resolvedApiItem.tsdocComment |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Recurse members |
| 135 | + if (ApiItemContainerMixin.isBaseClassOf(apiItem)) { |
| 136 | + for (const member of apiItem.members) { |
| 137 | + this._applyInheritDoc(member, apiModel); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Copy the content from `sourceDocComment` to `targetDocComment`. |
| 144 | + * This code is borrowed from DocCommentEnhancer as a temporary workaround. |
| 145 | + */ |
| 146 | + private _copyInheritedDocs( |
| 147 | + targetDocComment: tsdoc.DocComment, |
| 148 | + sourceDocComment: tsdoc.DocComment |
| 149 | + ): void { |
| 150 | + targetDocComment.summarySection = sourceDocComment.summarySection; |
| 151 | + targetDocComment.remarksBlock = sourceDocComment.remarksBlock; |
| 152 | + |
| 153 | + targetDocComment.params.clear(); |
| 154 | + for (const param of sourceDocComment.params) { |
| 155 | + targetDocComment.params.add(param); |
| 156 | + } |
| 157 | + for (const typeParam of sourceDocComment.typeParams) { |
| 158 | + targetDocComment.typeParams.add(typeParam); |
| 159 | + } |
| 160 | + targetDocComment.returnsBlock = sourceDocComment.returnsBlock; |
| 161 | + |
| 162 | + targetDocComment.inheritDocTag = undefined; |
| 163 | + } |
| 164 | +} |
0 commit comments