-
Notifications
You must be signed in to change notification settings - Fork 1
/
genDoc.js
208 lines (187 loc) · 6.81 KB
/
genDoc.js
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
const dc = require('./typedoc-json/doc.json');
// const docReplace = require('./typedoc-json/docReplace.json');
const fs = require('fs');
const path = require('path');
function compare(a, b) {
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
}
function analyzeParamType(fn, paramType) {
if (paramType.type === 'array') {
return `${analyzeParamType(fn, paramType.elementType)}[]`;
} else if (paramType.type === 'intrinsic' || paramType.type === 'reference') {
return paramType.name;
} else if (paramType.type === 'union') {
return paramType.types.map((tp) => analyzeParamType(fn, tp)).join(' | ');
} else if (paramType.type === 'tuple') {
return '[' + paramType.elements.map((tp) => analyzeParamType(fn, tp)).join(', ') + ']';
} else {
/**
* TODO: Update unrecognized param type here
*/
console.log('param type requires updating:', paramType);
console.log('in function:', fn.module + '.' + fn.name);
return paramType.type;
}
}
function addDoc(mod, docs) {
const moduleDict = {}
function getModule(moduleName) {
if (moduleDict[moduleName]) return moduleDict[moduleName];
const moduleDoc = {};
moduleDoc['id'] = moduleName;
moduleDoc['name'] = moduleName;
moduleDoc['func'] = [];
moduleDict[moduleName] = moduleDoc
docs.push(moduleDoc);
return moduleDict[moduleName]
}
for (const func of mod.children) {
if (func.name === 'InlineFuncs') { continue; }
const fn = {};
fn['id'] = func.id;
fn['name'] = func.name;
if (!func.sources || !func.sources[0] || !func.sources[0].fileName) {
continue
}
const sourceName = func.sources[0].fileName.split('/')[1].split('.')[0]
let moduleDoc = getModule(sourceName)
fn['module'] = sourceName;
if (!func['signatures']) { continue; }
if (func['signatures'][0].comment) {
const cmmt = func['signatures'][0].comment;
fn['description'] = cmmt.shortText;
if (cmmt.tags) {
for (const fnTag of cmmt.tags) {
if (fnTag.tag === 'summary') { fn['summary'] = fnTag.text;
} else {
if (fn[fnTag.tag]) {
fn[fnTag.tag].push(fnTag.text);
} else {
fn[fnTag.tag] = [fnTag.text];
}
}
}
}
fn['returns'] = cmmt.returns;
if (fn['returns']) { fn['returns'] = fn['returns'].trim(); }
}
fn['parameters'] = [];
if (func['signatures'][0].parameters) {
for (const param of func['signatures'][0].parameters) {
let namecheck = true;
const constList = ['__constList__', '__model__', '__input__'];
if (constList.indexOf(param.name) !== -1) {
namecheck = false;
}
if (!namecheck) {
fn['parameters'].push(undefined);
continue;
}
const pr = {};
pr['name'] = param.name;
if (param.comment) {
pr['description'] = param.comment.shortText || param.comment.text;
}
pr['type'] = analyzeParamType(fn, param.type);
fn['parameters'].push(pr);
}
}
moduleDoc.func.push(fn);
moduleDoc.func.sort(compare);
}
}
function genModuleDocs(docs) {
let count = 0;
let indexString = ''
for (const mod of docs) {
if (mod.name[0] === '_') { continue; }
let mdString = `# ${mod.name.toUpperCase()} \n \n`;
indexString += `# ${mod.name.toUpperCase()} \n`
if (mod.description) {
mdString += mod.description + ' \n \n \n'
indexString += mod.description + ' \n'
}
indexString += `link: [${mod.name}.md](${mod.name}.md) \n \n \n`
for (const func of mod.func) {
fnString = ``;
fnString += `**Description:** ${func.description} \n \n`;
if (func.parameters && func.parameters.length > 0) {
fnString += `**Parameters:** \n`;
for (const param of func.parameters) {
if (!param) {continue; }
const paramName = param.name.replace(/_/g, '\\_')
fnString += ` * *${paramName}:* ${param.description} \n`;
}
}
if (func.returns) {
fnString += ` \n**Returns:** ${func.returns} \n`;
}
if (func.example) {
fnString += `**Examples:** \n`;
for (const i in func.example) {
if (!func.example[i]) {continue; }
fnString += ` * ${func.example[i]} \n`;
if (func.example_info) {
fnString += ` ${func.example_info[i]} \n`;
}
}
}
fnString = `## ${func.name} \n \n \n` + fnString + ` \n \n`;
mdString += fnString
}
count += 1;
let countStr = count.toString();
if (countStr.length === 1) {
countStr = '0' + countStr;
}
// mdString = replaceText(mdString.replace(/\\n/g, '\n'));
fs.writeFile(`./documentation/${mod.name}.md`, mdString, function(err) {
if (err) {
return console.log(err);
}
console.log(`successfully saved ${mod.name}.md`);
});
}
fs.writeFile(`./documentation/index.md`, indexString, function(err) {
if (err) {
return console.log(err);
}
console.log(`successfully saved index.md`);
});
}
// const doc = dc.default;
const doc = dc;
const moduleDocs = [];
// for (const mod of doc.children) {
// console.log(mod)
// const modName = mod.name;
// if (modName.substring(0, 1) === '_' || modName === 'index' || modName === 'categorization') {
// continue;
// }
// addDoc(mod, modName, moduleDocs);
// }
addDoc(doc, moduleDocs);
fs.mkdirSync('./documentation', { recursive: true }, (err) => {
if (err) throw err;
});
fs.readdirSync('./documentation', (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlinkSync(path.join('./documentation', file), err => {
if (err) throw err;
});
}
});
genModuleDocs(moduleDocs)
// fs.writeFile(`./typedoc-json/doc.json`, replaceText(JSON.stringify(dc, null, 2)), function(err) {
// if (err) {
// return console.log(err);
// }
// console.log(`successfully saved doc.json`);
// });