-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
monorepo-api-extractor.ts
236 lines (216 loc) · 6.48 KB
/
monorepo-api-extractor.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/tsdocs
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
CompilerState,
ConsoleMessageId,
Extractor,
ExtractorConfig,
ExtractorLogLevel,
ExtractorMessage,
ExtractorResult,
IConfigFile,
} from '@microsoft/api-extractor';
import debugFactory from 'debug';
import fs from 'fs-extra';
import path from 'path';
import {
DEFAULT_APIDOCS_EXTRACTION_PATH,
ExtractorOptions,
getPackagesWithTsDocs,
LernaPackage,
typeScriptPath,
} from './helper';
const debug = debugFactory('loopback:tsdocs');
/**
* Run api-extractor for a lerna-managed monrepo
*
* @remarks
* The function performs the following steps:
* 1. Discover packages with tsdocs from the monorepo
* 2. Iterate through each package to run `api-extractor`
*
* @param options - Options for running api-extractor
*/
export async function runExtractorForMonorepo(options: ExtractorOptions = {}) {
debug('Extractor options:', options);
options = Object.assign(
{
rootDir: process.cwd(),
apiDocsExtractionPath: DEFAULT_APIDOCS_EXTRACTION_PATH,
typescriptCompilerFolder: typeScriptPath,
tsconfigFilePath: 'tsconfig.build.json',
mainEntryPointFilePath: 'dist/index.d.ts',
},
options,
);
const packages = await getPackagesWithTsDocs(options.rootDir);
/* istanbul ignore if */
if (!packages.length) return;
const lernaRootDir = packages[0].rootPath;
/* istanbul ignore if */
if (!options.silent) {
console.log('Running api-extractor for lerna repo: %s', lernaRootDir);
}
setupApiDocsDirs(lernaRootDir, options);
for (const pkg of packages) {
/* istanbul ignore if */
invokeExtractorForPackage(pkg, options);
}
}
export function runExtractorForPackage(
pkgDir: string = process.cwd(),
options: ExtractorOptions = {},
) {
options = Object.assign(
{
rootDir: pkgDir,
apiDocsExtractionPath: DEFAULT_APIDOCS_EXTRACTION_PATH,
typescriptCompilerFolder: typeScriptPath,
tsconfigFilePath: 'tsconfig.build.json',
mainEntryPointFilePath: 'dist/index.d.ts',
},
options,
);
const pkgJson = require(path.join(pkgDir, 'package.json'));
setupApiDocsDirs(pkgDir, options);
const pkg: LernaPackage = {
private: pkgJson.private,
name: pkgJson.name,
location: pkgDir,
manifestLocation: path.join(pkgDir, 'package.json'),
rootPath: pkgDir,
};
invokeExtractorForPackage(pkg, options);
}
/**
* Run `api-extractor` on a given package
* @param pkg - Package descriptor
* @param options - Options for api extraction
*/
function invokeExtractorForPackage(
pkg: LernaPackage,
options: ExtractorOptions,
) {
if (!options.silent) {
console.log('> %s', pkg.name);
}
debug('Package: %s (%s)', pkg.name, pkg.location);
process.chdir(pkg.location);
const extractorConfig = buildExtractorConfig(pkg, options);
debug('Resolved extractor config:', extractorConfig);
invokeExtractor(extractorConfig, options);
}
/**
* Set up dirs for apidocs
*
* @param lernaRootDir - Root dir of the monorepo
* @param options - Extractor options
*/
function setupApiDocsDirs(lernaRootDir: string, options: ExtractorOptions) {
/* istanbul ignore if */
if (options.dryRun) return;
const apiDocsExtractionPath = options.apiDocsExtractionPath!;
fs.emptyDirSync(path.join(lernaRootDir, `${apiDocsExtractionPath}/models`));
if (!options.apiReportEnabled) return;
fs.ensureDirSync(path.join(lernaRootDir, `${apiDocsExtractionPath}/reports`));
fs.emptyDirSync(
path.join(lernaRootDir, `${apiDocsExtractionPath}/reports-temp`),
);
}
/**
* Build extractor configuration object for the given package
*
* @param pkg - Lerna managed package
* @param options - Extractor options
*/
function createRawExtractorConfig(
pkg: LernaPackage,
options: ExtractorOptions,
) {
const entryPoint = path.join(pkg.location, options.mainEntryPointFilePath!);
const apiDocsExtractionPath = options.apiDocsExtractionPath!;
let configObj: IConfigFile = {
projectFolder: pkg.location,
mainEntryPointFilePath: entryPoint,
apiReport: {
enabled: !!options.apiReportEnabled,
reportFolder: path.join(pkg.rootPath, `${apiDocsExtractionPath}/reports`),
reportTempFolder: path.join(
pkg.rootPath,
`${apiDocsExtractionPath}/reports-temp`,
),
reportFileName: '<unscopedPackageName>.api.md',
},
docModel: {
enabled: true,
apiJsonFilePath: path.join(
pkg.rootPath,
`${apiDocsExtractionPath}/models/<unscopedPackageName>.api.json`,
),
},
messages: {
extractorMessageReporting: {
'ae-missing-release-tag': {
logLevel: ExtractorLogLevel.None,
addToApiReportFile: false,
},
},
},
compiler: {
tsconfigFilePath: options.tsconfigFilePath!,
},
};
/* istanbul ignore if */
if (options.config) {
configObj = Object.assign(configObj, options.config);
}
debug('Extractor config options:', configObj);
return configObj;
}
/**
* Create and prepare the extractor config for invocation
*
* @param pkg - Lerna package
* @param options - Extractor options
*/
function buildExtractorConfig(pkg: LernaPackage, options: ExtractorOptions) {
const configObj: IConfigFile = createRawExtractorConfig(pkg, options);
const extractorConfig = ExtractorConfig.prepare({
configObject: configObj,
configObjectFullPath: '',
packageJsonFullPath: pkg.manifestLocation,
});
return extractorConfig;
}
/**
* Invoke the extractor
*
* @param extractorConfig - Resolved config
* @param options - Extractor options
*/
function invokeExtractor(
extractorConfig: ExtractorConfig,
options: ExtractorOptions,
) {
const compilerState = CompilerState.create(extractorConfig, {
// typescriptCompilerFolder: options.typescriptCompilerFolder,
});
/* istanbul ignore if */
if (options.dryRun) return;
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
typescriptCompilerFolder: options.typescriptCompilerFolder,
localBuild: true,
showVerboseMessages: !options.silent,
messageCallback: (message: ExtractorMessage) => {
if (message.messageId === ConsoleMessageId.ApiReportCreated) {
// This script deletes the outputs for a clean build,
// so don't issue a warning if the file gets created
message.logLevel = ExtractorLogLevel.None;
}
},
compilerState,
});
debug(extractorResult);
}