-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
monorepo-api-extractor.ts
280 lines (259 loc) · 7.76 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright IBM Corp. and LoopBack contributors 2019,2020. 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,
ExtractorMessageId,
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,
LernaPackage,
getPackagesWithTsDocs,
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.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);
const errors: Record<string, unknown> = {};
for (const pkg of packages) {
// TODO: api-extractor failed to generate apidocs for the repos below.
// Excluding them for now
// https://github.com/loopbackio/loopback-next/issues/10205
if (
pkg.name === '@loopback/typeorm' ||
pkg.name === '@loopback/boot' ||
pkg.name === '@loopback/express' ||
pkg.name === '@loopback/repository' ||
pkg.name === '@loopback/service-proxy'
)
continue;
/* istanbul ignore if */
const err = invokeExtractorForPackage(pkg, options);
if (err != null) {
if (options.ignoreErrors) {
errors[pkg.name] = err;
} else {
throw err;
}
}
}
if (Object.keys(errors).length === 0) return;
console.error(
'****************************************' +
'****************************************',
);
for (const p in errors) {
const err = errors[p] as {message: string};
console.error('%s: %s', p, err?.message ?? err);
}
console.error(
'****************************************' +
'****************************************',
);
}
export function runExtractorForPackage(
pkgDir: string = process.cwd(),
options: ExtractorOptions = {},
) {
options = Object.assign(
{
rootDir: pkgDir,
apiDocsExtractionPath: DEFAULT_APIDOCS_EXTRACTION_PATH,
typescriptCompilerFolder: typeScriptPath,
tsconfigFilePath: 'tsconfig.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,
};
const err = invokeExtractorForPackage(pkg, options);
if (err == null) return;
if (!options.ignoreErrors) {
throw err;
}
console.error(err);
}
/**
* 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);
try {
invokeExtractor(extractorConfig, options);
} catch (err) {
debug('Error in extracting API docs for %s', pkg.name, err);
return err;
}
}
/**
* 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: {
[ExtractorMessageId.MissingReleaseTag]: {
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);
}