forked from milesj/docusaurus-plugin-typedoc-api
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
60 lines (50 loc) · 1.58 KB
/
Copy pathindex.ts
File metadata and controls
60 lines (50 loc) · 1.58 KB
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
import childProcess from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { DocspecTransformer } from './transformation';
import type { DocspecObject } from './types';
export { groupSort } from './utils';
/**
* Processes the Python documentation generated by `pydoc-markdown` and transforms it into a format
* accepted by the TypeDoc JSON generator.
*/
export function processPythonDocs({
pythonModulePath,
moduleShortcutsPath,
outPath,
gitRevision,
}: {
pythonModulePath: string;
moduleShortcutsPath: string;
outPath: string;
gitRevision: string;
}) {
const pydocMarkdownDumpPath = path.join(__dirname, './pydoc-markdown-dump.json');
const out = childProcess.spawnSync('python', [
path.join(__dirname, '..', '..', '..', './python-scripts/docspec-gen/generate_ast.py'),
'-i',
pythonModulePath,
pydocMarkdownDumpPath,
]);
if (out.error) {
throw new Error(`Failed to spawn the generate_ast.py script: ${out.error.message}`);
}
if (out.status !== 0) {
throw new Error(
`The generate_ast.py script failed with exit code ${out.status}:\n${out.stderr.toString()}`,
);
}
const moduleShortcuts = JSON.parse(fs.readFileSync(moduleShortcutsPath, 'utf8')) as Record<
string,
string
>;
const pydocMarkdownDump = JSON.parse(
fs.readFileSync(pydocMarkdownDumpPath, 'utf8'),
) as DocspecObject[];
const docspecTransformer = new DocspecTransformer({
gitRevision,
moduleShortcuts,
});
const typedocApiReference = docspecTransformer.transform(pydocMarkdownDump);
fs.writeFileSync(outPath, JSON.stringify(typedocApiReference, null, 4));
}