forked from TerriaJS/terriajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateDocs.ts
347 lines (296 loc) · 10.6 KB
/
generateDocs.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import fs from "fs";
import { uniqueId } from "lodash-es";
import YAML from "yaml";
import flatten from "../lib/Core/flatten";
import isDefined from "../lib/Core/isDefined";
import markdownToHtml from "../lib/Core/markdownToHtml";
import CatalogMemberFactory from "../lib/Models/Catalog/CatalogMemberFactory";
import { BaseModel } from "../lib/Models/Definition/Model";
import registerCatalogMembers from "../lib/Models/Catalog/registerCatalogMembers";
import Terria from "../lib/Models/Terria";
import { AnyTrait } from "../lib/Traits/Decorators/anyTrait";
import { ModelReferenceArrayTrait } from "../lib/Traits/Decorators/modelReferenceArrayTrait";
import { ModelReferenceTrait } from "../lib/Traits/Decorators/modelReferenceTrait";
import { ObjectArrayTrait } from "../lib/Traits/Decorators/objectArrayTrait";
import { ObjectTrait } from "../lib/Traits/Decorators/objectTrait";
import { PrimitiveArrayTrait } from "../lib/Traits/Decorators/primitiveArrayTrait";
import { PrimitiveTrait } from "../lib/Traits/Decorators/primitiveTrait";
import ModelTraits from "../lib/Traits/ModelTraits";
import Trait from "../lib/Traits/Trait";
/** Get type name for a given Trait */
function markdownFromTraitType(trait: Trait) {
let base = "";
if (trait instanceof PrimitiveTrait || trait instanceof PrimitiveArrayTrait) {
base = trait.type;
} else if (
trait instanceof ObjectTrait ||
trait instanceof ObjectArrayTrait
) {
base = trait.type.name;
} else if (trait instanceof AnyTrait) {
base = "any";
} else if (
trait instanceof ModelReferenceTrait ||
trait instanceof ModelReferenceArrayTrait
) {
base = "ModelReference";
} else {
base = "unknown";
}
return base;
}
/** Render row for a Trait with:
* - name
* - type
* - default value
* - description
*/
function renderTraitRow(property: string, trait: Trait, defaultValue: any) {
let traitType = markdownFromTraitType(trait);
let traitTypeIsArray =
trait instanceof PrimitiveArrayTrait || trait instanceof ObjectArrayTrait;
if (trait instanceof ObjectTrait || trait instanceof ObjectArrayTrait) {
traitType = `<a href="#${traitType.toLocaleLowerCase()}"><code>${
traitType + (traitTypeIsArray ? "[]" : "")
}</code></b>`;
defaultValue = undefined;
} else {
traitType = `<code>${traitType + (traitTypeIsArray ? "[]" : "")}</code>`;
}
// Delete defalut value is it is an empty array
if (
Array.isArray(defaultValue) &&
(defaultValue.length === 0 || defaultValue.every((i) => !isDefined(i)))
)
defaultValue = undefined;
return `
<tr>
<td><code>${property}</code></td>
<td>${traitType}</td>
<td>${defaultValue ? `<code>${defaultValue}</code>` : ""}</td>
<td>${markdownToHtml(trait.description, true)}</td>
</tr>`;
}
/** Render rows for all traits with the given parentTrait */
function renderTraitRows(
parentTrait: string,
model: BaseModel,
showTitle = true
) {
const objectTraits: BaseModel[] = [];
const traitRows = Object.entries(model.traits)
.filter(([property, trait]) => trait.parent.name === parentTrait)
.map(([property, trait]) => {
if (trait instanceof ObjectTrait) {
objectTraits.push((model as any)[property]);
} else if (trait instanceof ObjectArrayTrait) {
objectTraits.push(
new (trait as ObjectArrayTrait<ModelTraits>).modelClass(
uniqueId(),
model.terria
)
);
}
return renderTraitRow(property, trait, (model as any)[property]);
})
.join("\n");
return {
html: `
${showTitle ? `<tr><td colspan=4><b>${parentTrait}</b></td></tr>` : ""}
${traitRows}`,
objectTraits
};
}
// This tracks which traits have been rendered already - so we don't get duplicates
// It is reset for every catalog model
let alreadyRenderedTraits: string[] = [];
/** Render table of traits for given model */
function renderTraitTable(model: BaseModel, recursive = false, depth = 1) {
const rootTraits = model.TraitsClass.name;
// Return nothing if these traits have already been rendered
if (alreadyRenderedTraits.includes(rootTraits)) return {};
alreadyRenderedTraits.push(rootTraits);
// Traits organised by parentTraits
const traits = Object.values(model.traits).reduce<{
[parentTrait: string]: Trait[];
}>((obj, cur) => {
obj[cur.parent.name]
? obj[cur.parent.name].push(cur)
: (obj[cur.parent.name] = [cur]);
return obj;
}, {});
// List of all groups of traits
const otherTraits = Object.keys(traits)
.filter((trait) => trait !== rootTraits)
.sort();
const traitGroups = [rootTraits, ...otherTraits];
const traitGroupRows = traitGroups.map((traits) =>
renderTraitRows(traits, model, traits !== rootTraits)
);
let html = `
${"#".repeat(depth + 1)} ${rootTraits}
<table>
<thead>
<tr>
<th>Trait</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
${traitGroupRows.map((rows) => rows.html).join("\n")}
</tbody>
</table>`;
const objectTraits = flatten(traitGroupRows.map((rows) => rows.objectTraits));
if (recursive) {
html += objectTraits
.map((model) => renderTraitTable(model, true, depth + 1).html)
.filter(isDefined)
.join("\n");
}
return { html, objectTraits };
}
async function processMember(sampleMember: BaseModel, memberName: string) {
let content = `
# ${memberName}
${sampleMember.TraitsClass.description ?? ""}
${
sampleMember.TraitsClass.example
? `
## Example usage
\`\`\`json
${JSON.stringify(sampleMember.TraitsClass.example, null, 2)}
\`\`\`
`
: `\`"type": "${sampleMember.type}"\``
}`;
// Render table of *top-level* traits for the given member
// and reset alreadyRenderedTraits
alreadyRenderedTraits = [];
const mainTraitTable = renderTraitTable(sampleMember);
content += mainTraitTable.html;
// Render object-traits
content += mainTraitTable.objectTraits
?.map((objectTrait) => renderTraitTable(objectTrait, true).html)
.filter(isDefined)
.join("\n");
return content;
}
async function processArray(members: BaseModel[]) {
const typeDetailsNavItems = [];
let catalogItemsContent = "";
let catalogGroupsContent = "";
let catalogFunctionsContent = "";
let catalogReferencesContent = "";
for (let i = 0; i < members.length; i++) {
const sampleMember = members[i];
const memberName = sampleMember.constructor.name;
console.log(memberName, sampleMember.type);
const tableRow = `| [${memberName}](catalog-type-details/${sampleMember.type}.md) | \`${sampleMember.type}\` |\n`;
if (memberName.endsWith("Item")) {
catalogItemsContent += tableRow;
} else if (memberName.endsWith("Group")) {
catalogGroupsContent += tableRow;
} else if (memberName.endsWith("Function")) {
catalogFunctionsContent += tableRow;
} else if (memberName.endsWith("Reference")) {
catalogReferencesContent += tableRow;
} else if (memberName.endsWith("FunctionJob")) {
// Ignore FunctionJobs
} else {
console.error(`${memberName} is not an Item, Group or Function`);
}
typeDetailsNavItems.push({
[memberName]: `connecting-to-data/catalog-type-details/${sampleMember.type}.md`
});
const content = await processMember(sampleMember, memberName);
fs.writeFileSync(
`doc/connecting-to-data/catalog-type-details/${sampleMember.type}.md`,
content
);
}
return {
catalogItemsContent,
catalogGroupsContent,
catalogFunctionsContent,
catalogReferencesContent,
typeDetailsNavItems
};
}
export default async function generateDocs() {
const terria = new Terria();
registerCatalogMembers();
const catalogMembers = CatalogMemberFactory.constructorsArray;
const members = catalogMembers
.map((member) => {
const memberName = member[1];
return new memberName(undefined, terria);
})
.sort(function (a, b) {
if (a.constructor.name < b.constructor.name) return -1;
else if (a.constructor.name > b.constructor.name) return 1;
return 0;
});
const mkDocsConfig = YAML.parse(fs.readFileSync("doc/mkdocs.yml", "utf8"));
console.log("read doc/mkdocs.yml");
const commonContentHeader = `The Type column in the table below indicates the \`"type"\` property to use in the [Initialization File](../customizing/initialization-files.md).
| Name | Type |
|------|------|
`;
const catalogItemsContentHeader =
"A Catalog Item is a dataset or service that can be enabled for display on the map or in a chart. ";
const catalogGroupsContentHeader =
"A Catalog Group is a folder in the TerriaJS catalog that contains [Catalog Items](catalog-items.md), [Catalog Functions](catalog-functions.md), and other groups. ";
const catalogFunctionsContentHeader =
"A Catalog Function is a parameterized service where the user supplies the parameters and gets back some result. ";
const catalogReferencesContentHeader =
"A Catalog Reference can resolve to a Catalog Item, Group or Function. It's mostly used to connect to services that could return a single dataset or a group of datasets. ";
const {
catalogFunctionsContent,
catalogGroupsContent,
catalogItemsContent,
catalogReferencesContent,
typeDetailsNavItems
} = await processArray(members);
// Add entries for all the catalog item/group/function/reference types to type details subsection in mkdocs.yml
const connectingToDataSection = mkDocsConfig.nav
.map((section: any) => section["Connecting to Data"])
.filter((x: any) => x !== undefined)[0];
const typeDetailsSubSection =
connectingToDataSection &&
connectingToDataSection.find(
(subSection: any) => "Catalog Type Details" in subSection
);
if (typeDetailsSubSection === undefined) {
throw new Error(
`Couldn't find "Connecting to Data" → "Catalog Type Details" in mkdocs.yml`
);
}
typeDetailsSubSection["Catalog Type Details"] = typeDetailsNavItems;
fs.writeFileSync("mkdocs.yml", YAML.stringify(mkDocsConfig));
fs.writeFileSync(
"doc/connecting-to-data/catalog-items.md",
catalogItemsContentHeader + commonContentHeader + catalogItemsContent
);
fs.writeFileSync(
"doc/connecting-to-data/catalog-functions.md",
catalogFunctionsContentHeader +
commonContentHeader +
catalogFunctionsContent
);
fs.writeFileSync(
"doc/connecting-to-data/catalog-groups.md",
catalogGroupsContentHeader + commonContentHeader + catalogGroupsContent
);
fs.writeFileSync(
"doc/connecting-to-data/catalog-references.md",
catalogReferencesContentHeader +
commonContentHeader +
catalogReferencesContent
);
}
generateDocs().catch((err) => {
console.error(err);
process.exitCode = 1;
});