-
Notifications
You must be signed in to change notification settings - Fork 20
/
generate_doc.mjs
114 lines (95 loc) · 3.62 KB
/
generate_doc.mjs
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
import * as fs from 'fs';
import data from './.jsii.tabl.json' assert { type: 'json' };
const GENERATED_MD_PATH = '../website/docs/constructs/library/generated/_';
const IMAGE_FULL_PATH = "website/static/img";
const IMAGE_RELATIVE_PATH = "../static/img";
const LINKS_MAPPING = {
'../storage/README.md#datalakestorage' : '../02-Storage/03-data-lake-storage.mdx',
'#datacatalogdatabase': './data-catalog-database'
};
const COMMENT_REGEXP = new RegExp('\\[\\/\\/\]: # \\((.*)\\)\\n', 'gm');
// select only snippets for readme files
const snippets = Object.fromEntries(
Object.entries(data.snippets)
.filter(([_,v]) => v.location.api.api === 'moduleReadme')
);
let previousSubmodule = '';
let previousStart = 0;
let codeLength = 0;
let fullReadme = {};
let currentMDFile = '';
let lines = [];
for (let key in snippets) {
const snippet = snippets[key];
const typescript = snippet.translations.$.source;
const python = snippet.translations.python.source;
const submodule = snippet.location.api.moduleFqn.replace('@cdklabs/aws-data-solutions-framework.', '');
if (previousSubmodule !== submodule) {
previousSubmodule = submodule;
previousStart = 0;
codeLength = 0;
}
const file = `src/${submodule}/README.md`;
// do not read file on each snippet
if (file !== currentMDFile) {
currentMDFile = file;
const buffer = fs.readFileSync(file);
const str = buffer.toString();
lines = str.split("\n");
}
let line = snippet.location.field.line;
let realLine = line - codeLength;
// console.log(line + ' - ' + realLine);
if (!fullReadme[submodule]) {
fullReadme[submodule] = '';
}
fullReadme[submodule] += lines.slice(previousStart, realLine - 1).join('\n');
fullReadme[submodule] += `
<Tabs>
<TabItem value="typescript" label="TypeScript" default>
\`\`\`typescript
${typescript}
\`\`\`
</TabItem>
<TabItem value="python" label="Python">
\`\`\`python
${python}
\`\`\`
</TabItem>
</Tabs>\n
`;
previousStart = realLine + 1;
codeLength += typescript.split("\n").length + 1;
}
for (let module in fullReadme) {
const moduleReadme = fullReadme[module];
const constructReadmes = moduleReadme.split(COMMENT_REGEXP);
let filename = '';
for (let i = 0; i < constructReadmes.length; i++) {
let constructReadme = constructReadmes[i];
if (constructReadme.startsWith(module)) {
filename = GENERATED_MD_PATH + constructReadme.replace('.', '-') + '.mdx';
console.log(filename + ' exported');
} else if (constructReadme.length !== 0 && filename.length !== 0) {
let constructReadmeLines = constructReadme.split('\n');
constructReadmeLines.shift(); // remove title
constructReadme = constructReadmeLines.join('\n');
constructReadme = `<!-- This file is generated, do not modify directly, update the README.md in framework/src/${module} -->
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
${constructReadme}`;
constructReadme = constructReadme.replaceAll(IMAGE_FULL_PATH, IMAGE_RELATIVE_PATH); // change image path
for (const [key, value] of Object.entries(LINKS_MAPPING)) {
constructReadme = constructReadme.replaceAll(`(${key})`, `(${value})`); // replace internal links
}
try {
fs.chmodSync(filename, 0o744);
} catch (e) {
// file might not exist
}
fs.writeFileSync(filename, constructReadme);
fs.chmodSync(filename, 0o444);
filename = '';
}
}
}