-
Notifications
You must be signed in to change notification settings - Fork 11
/
documentation.ts
316 lines (272 loc) · 9.93 KB
/
documentation.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import * as child from 'child_process';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as glob from 'glob-promise';
import * as reflect from 'jsii-reflect';
import { TargetLanguage } from 'jsii-rosetta';
import { transliterateAssembly } from 'jsii-rosetta/lib/commands/transliterate';
import { Markdown } from '../render/markdown';
import { PythonTranspile } from '../transpile/python';
import { Transpile, Language, TranspiledType } from '../transpile/transpile';
import { TypeScriptTranspile } from '../transpile/typescript';
import { ApiReference } from './api-reference';
import { Readme } from './readme';
/**
* Options for rendering a `Documentation` object.
*/
export interface RenderOptions {
/**
* Include a generated api reference in the documentation.
*
* @default true
*/
readonly apiReference?: boolean;
/**
* Include the user defined README.md in the documentation.
*
* @default true
*/
readonly readme?: boolean;
/**
* Generate documentation only for a specific submodule.
*
* @default - Documentation is generated for the root module only.
*/
readonly submodule?: string;
/**
* How should links to types be rendered.
*
* @default '#{fqn}'
*/
readonly linkFormatter?: (type: TranspiledType) => string;
}
/**
* Options for creating a `Documentation` object.
*/
export interface DocumentationOptions {
/**
* Which language to generate docs for.
*
* @default Language.TYPESCRIPT
*/
readonly language?: Language;
/**
* Whether to ignore missing fixture files that will prevent transliterating
* some code snippet examples.
*
* @default true
*/
readonly loose?: boolean;
}
/**
* Options for creating a `Documentation` object using the `fromLocalPackage` function.
*/
export interface ForLocalPackageDocumentationOptions extends DocumentationOptions {
/**
* A local directory containing jsii assembly files that will
* comprise the type-system.
*
* @default - the root package directory will be used.
*/
readonly assembliesDir?: string;
}
export interface ForPackageDocumentationOptions extends DocumentationOptions {
/**
* The name of the package to be installed.
*
* If the target you supply points to a local file, this is required.
* Otherwise, it will be derived from the target.
*/
readonly name?: string;
}
/**
* Render documentation pages for a jsii library.
*/
export class Documentation {
/**
* Create a `Documentation` object from a package installable by npm.
*
* @param target - The target to install. This can either be a local path or a registry identifier (e.g <name>@<version>)
* @param options - Additional options.
*/
public static async forPackage(target: string, options: ForPackageDocumentationOptions = {}): Promise<Documentation> {
return withTempDir(async (workdir: string) => {
if (await fs.pathExists(target) && !options.name) {
throw new Error('\'options.name\' must be provided when installing local packages.');
}
const name = options?.name ?? extractPackageName(target);
// npm7 is needed so that we also install peerDependencies - they are needed to construct
// the full type system.
console.log('Installing npm7...');
await spawn('npm', ['install', 'npm@7'], {
cwd: workdir,
shell: true,
stdio: ['ignore', 'inherit', 'inherit'],
});
console.log(`Installing package ${target}`);
await spawn(path.join(workdir, 'node_modules', '.bin', 'npm'), [
'install',
// this is critical from a security perspective to prevent
// code execution as part of the install command using npm hooks. (e.g postInstall)
'--ignore-scripts',
// ensures npm does not insert anything in $PATH
'--no-bin-links',
'--no-save',
target,
], {
cwd: workdir,
shell: true,
stdio: ['ignore', 'inherit', 'inherit'],
});
return Documentation.forProject(path.join(workdir, 'node_modules', name), { ...options, assembliesDir: workdir } );
});
}
/**
* Create a `Documentation` object from a local directory containing a node project.
*
* @param root - The local directory path. Must contain a package.json file.
* @param options - Additional options.
*/
public static async forProject(root: string, options: ForLocalPackageDocumentationOptions = {}): Promise<Documentation> {
const manifestPath = path.join(root, 'package.json');
if (!(await fs.pathExists(manifestPath))) {
throw new Error(`Unable to locate ${manifestPath}`);
}
// normally the assemblies are located in subdirectories
// of the root package dir (i.e ./node_modules)
const assembliesDir = options?.assembliesDir ?? root;
const { name } = JSON.parse(await fs.readFile(manifestPath, 'utf-8'));
return Documentation.forAssembly(name, assembliesDir, {
language: options?.language,
loose: options?.loose,
});
}
/**
* Create a `Documentation` object for a specific assembly from a directory of assemblies.
*
* @param assemblyName - The assembly name.
* @param assembliesDir - The directory containing the assemblies that comprise the type-system.
* @param options - Additional options.
*/
public static async forAssembly(assemblyName: string, assembliesDir: string, options?: DocumentationOptions): Promise<Documentation> {
return withTempDir(async (workdir: string) => {
// always better not to operate on an externally provided directory
await fs.copy(assembliesDir, workdir);
let transpile, language;
switch (options?.language ?? Language.TYPESCRIPT) {
case Language.PYTHON:
language = TargetLanguage.PYTHON;
transpile = new PythonTranspile();
break;
case Language.TYPESCRIPT:
transpile = new TypeScriptTranspile();
break;
default:
throw new Error(`Unsupported language: ${options?.language}. Supported languages are ${Object.values(Language)}`);
}
const assembly = await createAssembly(assemblyName, workdir, options?.loose ?? true, language);
return new Documentation(assembly, transpile);
});
}
private constructor(
public readonly assembly: reflect.Assembly,
private readonly transpile: Transpile,
) {
}
/**
* Generate markdown.
*/
public render(options?: RenderOptions): Markdown {
const submodule = options?.submodule ? this.findSubmodule(this.assembly, options.submodule) : undefined;
const documentation = new Markdown();
if (options?.readme ?? true) {
const readme = new Readme(this.transpile, this.assembly, submodule);
documentation.section(readme.render());
}
if (options?.apiReference ?? true) {
const apiReference = new ApiReference(this.transpile, this.assembly, options?.linkFormatter ?? ((t: TranspiledType) => `#${t.fqn}`), submodule);
documentation.section(apiReference.render());
}
return documentation;
}
/**
* Lookup a submodule by a submodule name.
*/
private findSubmodule(assembly: reflect.Assembly, submodule: string): reflect.Submodule {
const submodules = assembly.submodules.filter(
(s) => s.name === submodule,
);
if (submodules.length === 0) {
throw new Error(`Submodule ${submodule} not found in assembly ${assembly.name}@${assembly.version}`);
}
if (submodules.length > 1) {
throw new Error(`Found multiple submodules with name: ${submodule} in assembly ${assembly.name}@${assembly.version}`);
}
return submodules[0];
}
}
async function withTempDir<T>(work: (workdir: string) => Promise<T>): Promise<T> {
const workdir = await fs.mkdtemp(path.join(os.tmpdir(), path.sep));
const cwd = process.cwd();
try {
process.chdir(workdir);
// wait for the work to be completed before
// we cleanup the work environment.
return await work(workdir);
} finally {
process.chdir(cwd);
await fs.remove(workdir);
}
}
async function createAssembly(name: string, tsDir: string, loose: boolean, language?: TargetLanguage): Promise<reflect.Assembly> {
console.log(`Creating assembly in ${language ?? 'ts'} for ${name} from ${tsDir} (loose: ${loose})`);
const ts = new reflect.TypeSystem();
for (let dotJsii of await glob.promise(`${tsDir}/**/.jsii`)) {
// we only transliterate the top level assembly and not the entire type-system.
// note that the only reason to translate dependant assemblies is to show code examples
// for expanded python arguments - which we don't to right now anyway.
// we don't want to make any assumption of the directory structure, so this is the most
// robuse way to detect the root assembly.
const spec = JSON.parse(await fs.readFile(dotJsii, 'utf-8'));
if (language && spec.name === name) {
const packageDir = path.dirname(dotJsii);
await transliterateAssembly([packageDir], [language], { loose });
dotJsii = path.join(packageDir, `.jsii.${language}`);
}
await ts.load(dotJsii);
}
return ts.findAssembly(name);
}
async function spawn(command: string, args?: ReadonlyArray<string>, options?: child.SpawnOptions) {
return new Promise<void>((ok, ko) => {
const p = child.spawn(command, args, options);
p.once('error', ko);
p.once('close', (code, signal) => {
if (code === 0) {
ok();
} else {
ko(`'${command}' command ${code != null ? `exited with code ${code}` : `was terminated by signal ${signal}`}`);
}
});
});
}
export function extractPackageName(spec: string) {
const firstAt = spec.indexOf('@');
if (firstAt === 0) {
const lastAt = spec.indexOf('@', firstAt + 1);
if (lastAt === -1) {
// @aws-cdk/aws-ecr
return spec;
} else {
// @aws-cdk/aws-ecr@2.0.0
return spec.substring(0, lastAt);
}
}
if (firstAt > 0) {
// aws-cdk-lib@2.0.0
return spec.substring(0, firstAt);
}
// aws-cdk-lib
return spec;
}