-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-embedded-docs.mjs
More file actions
141 lines (112 loc) · 4.02 KB
/
build-embedded-docs.mjs
File metadata and controls
141 lines (112 loc) · 4.02 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
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
import { promises as fs } from "node:fs";
import path from "node:path";
import vm from "node:vm";
const root = process.cwd();
const DOC_FILE_MAP = {
readmeMarkdown: "README.md",
resultsMarkdown: "RESULTS.md",
howToImplementMarkdown: "HOW_TO_IMPLEMENT.md",
aquaPromptMarkdown: "AQUA_PROMPT.md"
};
function toPosix(value) {
return value.split(path.sep).join("/");
}
function normalizeRelativePath(value) {
return toPosix(String(value || "").replace(/^\.\//, ""));
}
function formatBytes(bytes) {
if (!Number.isFinite(bytes) || bytes < 1024) {
return `${bytes} B`;
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)} KB`;
}
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function inferArtifactType(fileName) {
const extension = path.extname(fileName).slice(1).toLowerCase();
return extension || "file";
}
async function readRequiredFile(filePath, label) {
try {
return await fs.readFile(filePath, "utf8");
} catch (error) {
throw new Error(`Missing required ${label}: ${toPosix(path.relative(root, filePath))}`);
}
}
async function collectFilesRecursively(directory) {
const entries = await fs.readdir(directory, { withFileTypes: true });
const files = [];
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
files.push(...(await collectFilesRecursively(fullPath)));
continue;
}
if (entry.isFile()) {
files.push(fullPath);
}
}
return files;
}
async function collectResultsArtifacts(directory) {
const files = await collectFilesRecursively(directory);
return Promise.all(
files.map(async (filePath) => {
const stats = await fs.stat(filePath);
const relativePath = toPosix(path.relative(root, filePath));
return {
path: relativePath,
name: path.basename(filePath),
type: inferArtifactType(filePath),
sizeBytes: stats.size,
sizeLabel: formatBytes(stats.size)
};
})
);
}
async function loadCapsulesFromDataFile() {
const source = await fs.readFile(path.join(root, "capsules-data.js"), "utf8");
const context = { window: {}, console };
vm.createContext(context);
vm.runInContext(source, context, { filename: "capsules-data.js" });
if (!Array.isArray(context.window.CAPSULES)) {
throw new Error("capsules-data.js did not define window.CAPSULES");
}
return context.window.CAPSULES;
}
async function build() {
const capsules = await loadCapsulesFromDataFile();
const bundledDocs = {};
for (const capsule of capsules) {
const capsuleCodeDir = path.join(root, capsule.directory, "code");
const reviewPath = path.join(root, normalizeRelativePath(capsule.reviewPath));
const resultsDir = path.join(capsuleCodeDir, "results");
const docPayload = {};
for (const [field, fileName] of Object.entries(DOC_FILE_MAP)) {
docPayload[field] = await readRequiredFile(
path.join(capsuleCodeDir, fileName),
`${capsule.id} ${fileName}`
);
}
docPayload.reviewMarkdown = await readRequiredFile(reviewPath, `${capsule.id} review markdown`);
docPayload.resultsArtifacts = await collectResultsArtifacts(resultsDir);
const inventoryPaths = new Set(docPayload.resultsArtifacts.map((artifact) => normalizeRelativePath(artifact.path)));
for (const featuredArtifact of capsule.featuredArtifacts || []) {
const normalized = normalizeRelativePath(featuredArtifact);
if (!inventoryPaths.has(normalized)) {
throw new Error(
`Featured artifact "${featuredArtifact}" for ${capsule.id} was not found under ${toPosix(
path.relative(root, resultsDir)
)}`
);
}
}
bundledDocs[capsule.id] = docPayload;
}
const serialized = JSON.stringify(bundledDocs, null, 2)
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
await fs.writeFile(path.join(root, "capsule-docs.js"), `window.CAPSULE_DOCS = ${serialized};\n`, "utf8");
}
await build();