-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-docs.js
472 lines (425 loc) · 14.4 KB
/
build-docs.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* eslint-disable no-console */
"use strict";
const _ = require("lodash");
const fsp = require("fs").promises;
const fse = require("fs-extra");
const path = require("path");
const { exec } = require("child_process");
const Logger = require("../src/logger");
const ServicesWorker = require("../src/services-worker");
const Storage = require("../src/storage/connection");
const ServiceStorage = require("../src/storage");
const axios = require("axios");
const config = require("../src/config");
main().catch((err) => console.error(err));
async function main() {
const db = await Storage.connect();
ServiceStorage.init(new Logger("services"), db);
await build();
}
let INPUT_TYPES = undefined;
async function build() {
const services = new ServicesWorker(new Logger("netsblox:build-docs"));
await services.load(); // needed for docs
INPUT_TYPES = await getLoadedTypes(); // needed for other functions
await compileDocs(services);
}
async function hasDirectory(dir, subdir) {
return (await fsp.readdir(dir)).includes(subdir) &&
(await fsp.lstat(path.join(dir, subdir))).isDirectory();
}
function isObject(type) {
return type && type.name && type.name.toLowerCase() === "object";
}
function linkType(dispName) {
return `\`${dispName} </docs/fundamentals/rpc-arg-types.html#${dispName}>\`__`;
}
function getTypeString(type, link = false) {
if (link === true) {
const links = [];
getTypeString(type, links);
return links.join(" | ");
}
if (type.name === undefined) {
const rawName = type.toString();
const ty = INPUT_TYPES[rawName];
if (link && ty) {
const res = linkType(ty.displayName || rawName);
if (!link.includes(res)) link.push(res);
}
return rawName;
}
const params = type.params || [];
const name = (INPUT_TYPES[type.name] || {}).displayName || type.name;
if (link) {
if (INPUT_TYPES[type.name]) {
const res = linkType(name);
if (!link.includes(res)) link.push(res);
}
for (const sub of params) getTypeString(sub, link);
} else {
if (isObject(type)) return "Object";
return params.length
? `${name}<${params.map((x) => getTypeString(x)).join(", ")}>`
: name;
}
}
function getParamString(param, link = false) {
if (link) return param.type ? getTypeString(param.type, true) : "";
const str = param.type
? `${param.name}: ${getTypeString(param.type)}`
: param.name;
return param.optional ? `${str}?` : str;
}
async function loadSubservice(path) {
try {
const root = config.ServerURL;
return await axios.get(`${root}${path}`);
} catch (err) {
if (err.code === "ECONNREFUSED") {
const msg = process.env.SERVER_URL
? `Unable to connect to ${process.env.SERVER_URL}. Is this the correct address?`
: "Unable to connect to services server. Please set the SERVER_URL environment variable and retry.";
throw new Error(msg);
} else if (config.ServerURL) {
const msg =
`${config.ServerURL} is not a valid address for NetsBlox services. ` +
"It should be set to the services server such as https://services.netsblox.org";
throw new Error(msg);
}
throw err;
}
}
async function getLoadedTypes() {
const res = await loadSubservice("/input-types");
return res.data;
}
async function getLoadedServices() {
const res = await loadSubservice("/");
return res.data.map((s) => s.name);
}
const SERVICE_FILTERS = {
all: () => true,
nodeprecated: (name, meta) => !meta.tags.includes("deprecated"),
fsonly: (name, meta) => meta.servicePath,
};
function getServiceFilter(
loadedServices,
filterString = "fsonly,nodeprecated",
) {
const isServiceLoaded = (name) => loadedServices.includes(name);
const filters = filterString.split(",").map((s) => s.trim()).filter((s) =>
s.length
).map((s) => SERVICE_FILTERS[s]);
filters.unshift(isServiceLoaded);
return (name, meta) => filters.every((f) => f(name, meta));
}
const SERVICE_DIR_REGEX = /(.*)\/.*\.js/;
const DOCS_PATH = path.join(__dirname, "..", "docs");
const GENERATED_PATH = path.join(DOCS_PATH, "_generated");
const SERVICES_PATH = path.join(GENERATED_PATH, "services");
function getCategories(obj) {
const cats = obj.categories;
if (!cats || !cats.length) return ["index"];
for (let i = 0; i < cats.length; ++i) {
if (cats[i].length === 0) cats[i] = "index";
}
return cats;
}
function updateCategories(categories, name, obj) {
for (const category of getCategories(obj)) {
const cat = categories[category];
if (cat) cat.items.push(name);
else categories[category] = { description: undefined, items: [name] };
}
}
function sortCategories(categories) {
for (const category in categories) {
categories[category].items.sort();
}
}
function trimText(str) {
str = (str || "").trim();
return str.length ? str : undefined;
}
function getRPCsMeta(service) {
const categories = { index: { description: undefined, items: [] } };
const rpcs = {};
for (const rpcName in service.rpcs) {
const rpc = service.rpcs[rpcName];
if (rpc.deprecated) continue;
updateCategories(categories, rpcName, rpc);
rpcs[rpcName] = {
description: trimText(rpc.rawDescription),
args: (rpc.args || []).map((arg) => {
return {
decl: getParamString(arg, false),
declLink: getParamString(arg, true),
description: trimText(arg.rawDescription),
fields: !isObject(arg.type) || !(arg.type.params || []).length
? undefined
: arg.type.params
.filter((f) => !f.tags.includes("deprecated")).map((field) => {
return {
decl: getParamString(field, false),
declLink: getParamString(field, true),
description: trimText(field.rawDescription),
};
}),
};
}),
returns: rpc.returns
? {
type: getTypeString(rpc.returns.type, false),
typeLink: getTypeString(rpc.returns.type, true),
description: trimText(rpc.returns.rawDescription),
}
: undefined,
};
}
sortCategories(categories);
return { categories, rpcs };
}
function getMeta(services, serviceFilter) {
const categories = { index: { description: undefined, items: [] } };
const servicesMeta = {};
const apiKeys = {};
for (const serviceName in services.metadata) {
const service = services.metadata[serviceName];
if (!serviceFilter(serviceName, service)) continue;
updateCategories(categories, serviceName, service);
servicesMeta[serviceName] = {
path: service.servicePath,
description: trimText(service.rawDescription),
rpcs: getRPCsMeta(service),
};
apiKeys[serviceName] = service.apiKey;
}
sortCategories(categories);
return {
description: undefined,
categories,
services: servicesMeta,
apiKeys,
};
}
async function loadCategoryContent(rootPath, categoryName, isServiceCategory) {
if ((await fsp.readdir(rootPath)).includes(`${categoryName}.rst`)) {
return await fsp.readFile(path.join(rootPath, `${categoryName}.rst`), {
encoding: "utf8",
});
}
const content = `<%= name %><%= description %><%= ${
isServiceCategory ? "services" : "rpcs"
} %>`;
await fsp.writeFile(path.join(rootPath, `${categoryName}.rst`), content);
return content;
}
async function copyServiceDocs(serviceName, service) {
const indexContent =
"<%= name %><%= description %><%= categories %><%= rpcs %>";
const dest = path.join(SERVICES_PATH, serviceName);
let needsDir = true;
if (service.path) {
const serviceDir = service.path.match(SERVICE_DIR_REGEX)[1];
if (await hasDirectory(serviceDir, "docs")) {
const src = path.join(serviceDir, "docs");
const [files] = await Promise.all([
fsp.readdir(src),
fse.copy(src, dest),
]);
needsDir = false;
if (files.includes("index.rst")) return;
}
}
if (needsDir) await fsp.mkdir(dest);
await fsp.writeFile(path.join(dest, "index.rst"), indexContent);
}
function buildRPCString(serviceName, rpcName, rpc) {
let str = `.. function:: ${serviceName}.${rpcName}(${
rpc.args.map((x) => x.decl).join(", ")
})\n\n`;
if (rpc.description) {
str += `${
rpc.description.split("\n").map((s) => ` ${s}`).join("\n")
}\n\n`;
}
if (rpc.args.length) {
str += " **Arguments:**\n\n";
for (const arg of rpc.args) {
const desc = arg.description ? `- ${arg.description}` : "";
str += ` - \`\`${arg.decl}\`\` (${arg.declLink}) ${desc}\n`;
if (!arg.fields) continue;
str += "\n";
for (const field of arg.fields) {
const desc = field.description ? `- ${field.description}` : "";
str += ` - \`\`${field.decl}\`\` (${field.declLink}) ${desc}\n`;
}
str += "\n";
}
str += "\n";
}
if (rpc.returns) {
const desc = rpc.returns.description ? `- ${rpc.returns.description}` : "";
str +=
` **Returns:** \`\`${rpc.returns.type}\`\` (${rpc.returns.typeLink}) ${desc}\n\n`;
}
return str;
}
const RESOLVE_FILE_REGEX = /\.rst$/;
async function recursiveResolveCopy(from, to, vars) {
const info = await fsp.lstat(from);
if (info.isDirectory()) {
await fsp.mkdir(to);
const files = await fsp.readdir(from);
return await Promise.all(files.map((file) => {
return recursiveResolveCopy(
path.join(from, file),
path.join(to, file),
vars,
);
}));
}
if (!from.match(RESOLVE_FILE_REGEX)) {
return await fsp.copyFile(from, to);
}
const content = _.template(await fsp.readFile(from, { encoding: "utf-8" }))(
vars,
);
await fsp.writeFile(to, content);
}
async function cleanRoot() {
const docsFiles = new Set(await fsp.readdir(DOCS_PATH));
if (docsFiles.has("_generated")) {
await fsp.rm(GENERATED_PATH, { recursive: true });
docsFiles.delete("_generated");
}
await fsp.mkdir(GENERATED_PATH);
await fsp.mkdir(SERVICES_PATH);
return docsFiles;
}
async function compileDocs(services) {
const rootDocs = await cleanRoot();
const loadedServices = await getLoadedServices();
const serviceFilter = getServiceFilter(
loadedServices,
process.env.DOCS_SERVICE_FILTER,
);
const meta = getMeta(services, serviceFilter);
const servicesString =
"\n\n.. toctree::\n :maxdepth: 2\n :titlesonly:\n :caption: Services\n\n " +
(Object.keys(meta.categories).concat(meta.categories.index.items)).filter(
(s) => s !== "index",
).sort().map((item) => {
const isCategory = !!meta.categories[item];
return isCategory ? `services/${item}.rst` : `services/${item}/index.rst`;
}).join("\n ") + "\n\n";
for (const serviceName in meta.services) {
const service = meta.services[serviceName];
await copyServiceDocs(serviceName, service);
const categories = Object.keys(service.rpcs.categories).sort();
const catsString =
"\n\n.. toctree::\n :maxdepth: 2\n :titlesonly:\n :caption: RPC Categories\n\n" +
categories.filter((s) => s !== "index").map((s) => ` ${s}.rst\n`).join(
"",
) + "\n\n";
for (const categoryName of categories) {
const category = service.rpcs.categories[categoryName];
const rpcsPieces = category.items.map((s) =>
buildRPCString(serviceName, s, service.rpcs.rpcs[s])
);
const rpcsString = rpcsPieces.length
? "\n\nRPCS\n----\n\n" + rpcsPieces.join("\n") + "\n\n"
: "\n\n";
const name = categoryName === "index" ? serviceName : categoryName;
let content = _.template(
await loadCategoryContent(
path.join(SERVICES_PATH, serviceName),
categoryName,
false,
),
)({
name: `\n\n${name}\n${"=".repeat(name.length)}\n\n`,
description: `\n\n${
(categoryName === "index" ? service : category).description || ""
}\n\n`,
categories: catsString,
rpcs: rpcsString,
});
await fsp.writeFile(
path.join(SERVICES_PATH, serviceName, `${categoryName}.rst`),
content,
);
}
}
for (const categoryName in meta.categories) {
if (categoryName === "index") continue;
const category = meta.categories[categoryName];
const servString =
"\n\n.. toctree::\n :maxdepth: 2\n :titlesonly:\n :caption: Services\n\n" +
category.items.map((s) => ` ${s}/index.rst\n`).join("") + "\n\n";
let content = _.template(
await loadCategoryContent(SERVICES_PATH, categoryName, true),
)({
name: `\n\n${categoryName}\n${"=".repeat(categoryName.length)}\n\n`,
description: `\n\n${category.description || ""}\n\n`,
services: servString,
});
await fsp.writeFile(
path.join(SERVICES_PATH, `${categoryName}.rst`),
content,
);
}
const resolveVars = {
services: servicesString,
apiKeys: meta.apiKeys,
inputTypes: INPUT_TYPES,
};
await Promise.all(
Array.from(rootDocs).map((file) => {
return recursiveResolveCopy(
path.join(DOCS_PATH, file),
path.join(GENERATED_PATH, file),
resolveVars,
);
}),
);
// the reason for not using promisify is so that we still get the stderr/stdout on failure so user can see what the issue was
await new Promise((resolve, reject) => {
exec(
"make clean && make html",
{ cwd: GENERATED_PATH },
async (error, stdout, stderr) => {
const { ignored, important } = splitErrors(stderr);
if (error || important.length) {
reject(
Error(
`failed to compile docs:\nerror: ${
error ? error : ""
}\n\nimportant:\n${important}\n\nignored:\n${ignored}\n\nstdout:\n${stdout}`,
),
);
} else if (!await hasDirectory(GENERATED_PATH, "_build")) {
reject(Error(`failed to find docs build directory`));
} else if (
!await hasDirectory(path.join(GENERATED_PATH, "_build"), "html")
) {
reject(Error(`failed to find html in docs build directory`));
} else {
console.log(`compiled docs:`, stdout);
resolve();
}
},
);
});
}
const IGNORED_WARNINGS = [
"WARNING: duplicate function description",
];
function splitErrors(str) {
const isIgnored = (e) => IGNORED_WARNINGS.some((p) => e.includes(p));
const errors = str.split("\n").map((s) => s.trim()).filter((s) => s.length);
const [ignored, important] = _.partition(errors, isIgnored);
return { ignored: ignored.join("\n"), important: important.join("\n") };
}
/* eslint-enable no-console */