-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathgenerate_docs.js
More file actions
77 lines (65 loc) · 2.31 KB
/
generate_docs.js
File metadata and controls
77 lines (65 loc) · 2.31 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// @ts-check
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import initWasm, { generate_docs } from "./lib/web/qsc_wasm.js";
const scriptDirPath = dirname(fileURLToPath(import.meta.url));
const docsDirPath = join(scriptDirPath, "docs");
if (!existsSync(docsDirPath)) {
mkdirSync(docsDirPath);
}
// Initialize wasm before calling any exported functions
const wasmPath = join(scriptDirPath, "lib", "web", "qsc_wasm_bg.wasm");
const wasmBytes = readFileSync(wasmPath);
await initWasm({ module_or_path: wasmBytes });
// 'filename' will be of the format 'namespace/api.md' (except for 'toc.yaml')
// 'metadata' will be the metadata that will appear at the top of the file
// 'contents' will contain the non-metadata markdown expected
/** @type {Array<{filename: string; metadata: string; contents: string}>} */
const docs = generate_docs();
if (!docs || !docs.length) {
throw new Error("No docs generated");
}
var today = new Date();
var dd = String(today.getDate()).padStart(2, "0");
var mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
var yyyy = today.getFullYear();
var today_str = mm + "/" + dd + "/" + yyyy;
docs.forEach((doc) => {
// If the filename contains a /, then we need to create the directory
const parts = doc.filename.split("/");
let fullPath = "";
switch (parts.length) {
case 1:
if (doc.filename !== "toc.yml" && doc.filename !== "index.md") {
throw new Error(`Invalid filename: ${doc.filename}`);
} else {
fullPath = join(docsDirPath, doc.filename);
}
break;
case 2: {
// Create the directory of the first part
const dirName = join(docsDirPath, parts[0]);
if (!existsSync(dirName)) {
mkdirSync(dirName);
}
fullPath = join(dirName, parts[1]);
break;
}
default:
throw new Error(`Invalid file path: ${doc.filename}`);
}
var contents = "";
if (doc.filename === "toc.yml") {
contents = doc.contents;
} else {
contents =
doc.metadata.replace("ms.date: {TIMESTAMP}", `ms.date: ${today_str}`) +
"\n\n" +
doc.contents;
}
writeFileSync(fullPath, contents);
});
console.log("Done");