-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-content.js
More file actions
206 lines (169 loc) · 6.59 KB
/
build-content.js
File metadata and controls
206 lines (169 loc) · 6.59 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
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
#!/usr/bin/env node
/**
* CoReasoning Lab — Content Build Script
* =======================================
* Reads YAML files from content/{lang}/ and compiles them into JS files
* that can be loaded via <script> tags (file:// compatible).
*
* Usage:
* node build-content.js # one-shot build
* node build-content.js --watch # rebuild on changes
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const { globSync } = require('glob');
// ── Paths ──────────────────────────────────────────────────────────
const CONTENT_DIR = path.join(__dirname, 'content');
const OUTPUT_DIR = path.join(__dirname, 'screens', 'content-compiled');
// Subdirectories whose files become keyed entries (filename → object)
const COLLECTION_DIRS = ['challenges', 'courses', 'prompts', 'instructions', 'subjects'];
// Top-level YAML files whose content is used directly
const SINGLE_FILES = {
'ui-labels.yaml': 'uiLabels',
'institutions.yaml': 'institutions',
'users.yaml': 'users',
'scenarios.yaml': 'scenarios'
};
// ── Helpers ────────────────────────────────────────────────────────
/**
* Convert a kebab-case filename to a camelCase key for JS output.
* 'deep-learning-101' → 'deep-learning-101' (kept as-is for content keys)
*/
function fileKey(filePath) {
return path.basename(filePath, path.extname(filePath));
}
/**
* Safely read and parse a YAML file. Returns null on error.
*/
function readYaml(filePath) {
try {
const raw = fs.readFileSync(filePath, 'utf8');
return yaml.load(raw);
} catch (err) {
console.error(` [WARN] Failed to parse ${filePath}: ${err.message}`);
return null;
}
}
// ── Build one language ─────────────────────────────────────────────
function buildLanguage(lang) {
const langDir = path.join(CONTENT_DIR, lang);
if (!fs.existsSync(langDir)) return null;
const result = {};
// 1. Single-file types
for (const [filename, propName] of Object.entries(SINGLE_FILES)) {
const filePath = path.join(langDir, filename);
if (fs.existsSync(filePath)) {
const data = readYaml(filePath);
if (data !== null) {
result[propName] = data;
}
}
}
// 2. Collection directories
for (const dirName of COLLECTION_DIRS) {
const dirPath = path.join(langDir, dirName);
if (!fs.existsSync(dirPath)) continue;
const collection = {};
// Use globSync to find all .yaml and .yml files in the directory
const pattern = path.join(dirPath, '*.{yaml,yml}').replace(/\\/g, '/');
const files = globSync(pattern);
for (const filePath of files) {
const key = fileKey(filePath);
const data = readYaml(filePath);
if (data !== null) {
collection[key] = data;
}
}
// Convert directory name to camelCase property name
result[dirName] = collection;
}
return result;
}
// ── Write compiled output ──────────────────────────────────────────
function writeCompiledJS(lang, data) {
const outPath = path.join(OUTPUT_DIR, `${lang}.js`);
const json = JSON.stringify(data, null, 2);
const content = [
'// Auto-generated \u2014 do not edit. Run: npm run build',
'window.CONTENT = window.CONTENT || {};',
`window.CONTENT[${JSON.stringify(lang)}] = ${json};`,
''
].join('\n');
fs.writeFileSync(outPath, content, 'utf8');
console.log(` \u2713 ${outPath}`);
}
function writeAllJS(languages) {
const outPath = path.join(OUTPUT_DIR, 'all.js');
const lines = [
'// Auto-generated \u2014 do not edit. Run: npm run build',
'// Aggregates all language bundles into window.CONTENT.',
'window.CONTENT = window.CONTENT || {};',
''
];
for (const lang of languages) {
const perLangPath = `${lang}.js`;
// Use document.write to load each language file synchronously
// This works with file:// protocol and ensures ordering
lines.push(
`document.write('<script src="' + (document.currentScript ? document.currentScript.src.replace(/all\\.js$/, '') : 'content-compiled/') + '${perLangPath}"><\\/script>');`
);
}
lines.push('');
fs.writeFileSync(outPath, lines.join('\n'), 'utf8');
console.log(` \u2713 ${outPath}`);
}
// ── Main build ─────────────────────────────────────────────────────
function build() {
console.log('Building content...');
// Ensure output directory exists
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
// Discover all language directories
const langDirs = fs.readdirSync(CONTENT_DIR, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => d.name)
.sort();
if (langDirs.length === 0) {
console.log(' No language directories found in content/. Nothing to build.');
return;
}
const builtLanguages = [];
for (const lang of langDirs) {
const data = buildLanguage(lang);
if (data !== null && Object.keys(data).length > 0) {
writeCompiledJS(lang, data);
builtLanguages.push(lang);
} else {
console.log(` [SKIP] ${lang}/ — no YAML files found`);
}
}
if (builtLanguages.length > 0) {
writeAllJS(builtLanguages);
}
console.log(`Done. Built ${builtLanguages.length} language(s): ${builtLanguages.join(', ') || '(none)'}`);
}
// ── Watch mode ─────────────────────────────────────────────────────
function watch() {
console.log('Watching content/ for changes... (Ctrl+C to stop)\n');
build();
let debounceTimer = null;
const rebuildDebounced = () => {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
console.log('\n--- Change detected ---');
build();
}, 300);
};
// Watch recursively
fs.watch(CONTENT_DIR, { recursive: true }, (eventType, filename) => {
if (filename && (filename.endsWith('.yaml') || filename.endsWith('.yml'))) {
rebuildDebounced();
}
});
}
// ── Entry point ────────────────────────────────────────────────────
if (process.argv.includes('--watch')) {
watch();
} else {
build();
}