|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/25/2026 |
| 5 | +
|
| 6 | +normalize-games-presentation.mjs |
| 7 | +
|
| 8 | +How to run: |
| 9 | +1) npm run normalize:games-presentation |
| 10 | +2) Reload /games/index.html to refresh the Class dropdown from engineClassesUsed |
| 11 | +*/ |
| 12 | + |
| 13 | +import fs from "node:fs"; |
| 14 | +import path from "node:path"; |
| 15 | + |
| 16 | +const ROOT = process.cwd(); |
| 17 | +const GAMES_DIR = path.join(ROOT, "games"); |
| 18 | +const METADATA_PATH = path.join(GAMES_DIR, "metadata", "games.index.metadata.json"); |
| 19 | + |
| 20 | +function readFile(filePath) { |
| 21 | + return fs.readFileSync(filePath, "utf8"); |
| 22 | +} |
| 23 | + |
| 24 | +function writeFile(filePath, content) { |
| 25 | + fs.writeFileSync(filePath, content, "utf8"); |
| 26 | +} |
| 27 | + |
| 28 | +function normalizeWhitespace(value) { |
| 29 | + return String(value || "") |
| 30 | + .replace(/\s+/g, " ") |
| 31 | + .trim(); |
| 32 | +} |
| 33 | + |
| 34 | +function normalizeEngineReference(rawRef) { |
| 35 | + const text = normalizeWhitespace(rawRef).replace(/\\/g, "/"); |
| 36 | + if (!text) { |
| 37 | + return ""; |
| 38 | + } |
| 39 | + |
| 40 | + const marker = "src/engine/"; |
| 41 | + const markerIndex = text.indexOf(marker); |
| 42 | + const normalized = markerIndex >= 0 ? text.slice(markerIndex + marker.length) : text; |
| 43 | + const cleaned = normalized |
| 44 | + .replace(/\.js$/i, "") |
| 45 | + .replace(/^\/+|\/+$/g, "") |
| 46 | + .replace(/\/+/g, "/"); |
| 47 | + if (!cleaned) { |
| 48 | + return ""; |
| 49 | + } |
| 50 | + |
| 51 | + const segments = cleaned.split("/").filter(Boolean); |
| 52 | + if (segments.length >= 2 && segments[segments.length - 1] === segments[segments.length - 2]) { |
| 53 | + segments.pop(); |
| 54 | + } |
| 55 | + |
| 56 | + return `engine/${segments.join("/")}`; |
| 57 | +} |
| 58 | + |
| 59 | +function isThemeEngineClass(value) { |
| 60 | + const normalized = normalizeEngineReference(value); |
| 61 | + if (!normalized) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + const leaf = normalized.split("/").pop() || ""; |
| 65 | + return /^theme/i.test(leaf); |
| 66 | +} |
| 67 | + |
| 68 | +function walkJsFiles(dirPath) { |
| 69 | + const out = []; |
| 70 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 71 | + for (const entry of entries) { |
| 72 | + const fullPath = path.join(dirPath, entry.name); |
| 73 | + if (entry.isDirectory()) { |
| 74 | + if (entry.name === "assets" || entry.name === "node_modules") { |
| 75 | + continue; |
| 76 | + } |
| 77 | + out.push(...walkJsFiles(fullPath)); |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (entry.isFile() && /\.(m?js)$/i.test(entry.name)) { |
| 81 | + out.push(fullPath); |
| 82 | + } |
| 83 | + } |
| 84 | + return out; |
| 85 | +} |
| 86 | + |
| 87 | +function parseImportSymbols(specifierText) { |
| 88 | + const text = normalizeWhitespace(specifierText); |
| 89 | + if (!text) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + if (text.startsWith("{") && text.endsWith("}")) { |
| 94 | + return text |
| 95 | + .slice(1, -1) |
| 96 | + .split(",") |
| 97 | + .map((part) => normalizeWhitespace(part)) |
| 98 | + .filter(Boolean) |
| 99 | + .map((part) => normalizeWhitespace(part.split(/\s+as\s+/i)[0])) |
| 100 | + .filter(Boolean); |
| 101 | + } |
| 102 | + |
| 103 | + if (text.startsWith("*")) { |
| 104 | + const match = text.match(/\*\s+as\s+([A-Za-z_$][\w$]*)/); |
| 105 | + return match ? [match[1]] : []; |
| 106 | + } |
| 107 | + |
| 108 | + if (text.includes(",")) { |
| 109 | + const [defaultPart, namedPart] = text.split(",", 2); |
| 110 | + const symbols = []; |
| 111 | + const defaultSymbol = normalizeWhitespace(defaultPart); |
| 112 | + if (defaultSymbol) { |
| 113 | + symbols.push(defaultSymbol); |
| 114 | + } |
| 115 | + if (namedPart && namedPart.includes("{")) { |
| 116 | + symbols.push(...parseImportSymbols(namedPart.slice(namedPart.indexOf("{")))); |
| 117 | + } |
| 118 | + return symbols; |
| 119 | + } |
| 120 | + |
| 121 | + return [text]; |
| 122 | +} |
| 123 | + |
| 124 | +function collectFromImportSource(importSource, symbols, refs) { |
| 125 | + const source = String(importSource || "").replace(/\\/g, "/"); |
| 126 | + const marker = "src/engine/"; |
| 127 | + const markerIndex = source.indexOf(marker); |
| 128 | + if (markerIndex < 0) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const afterMarker = source.slice(markerIndex + marker.length).replace(/^\//, ""); |
| 133 | + if (!afterMarker) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + const modulePath = afterMarker.replace(/\.js$/i, ""); |
| 138 | + if (!modulePath) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (!symbols || symbols.length === 0) { |
| 143 | + refs.add(normalizeEngineReference(`src/engine/${modulePath}`)); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + for (const symbol of symbols) { |
| 148 | + const cleanSymbol = normalizeWhitespace(symbol); |
| 149 | + if (!cleanSymbol) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + refs.add(normalizeEngineReference(`src/engine/${modulePath}/${cleanSymbol}`)); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +function collectEngineClassReferencesFromJs(gameDir) { |
| 157 | + const files = walkJsFiles(gameDir); |
| 158 | + const refs = new Set(); |
| 159 | + |
| 160 | + for (const filePath of files) { |
| 161 | + const source = readFile(filePath); |
| 162 | + const importRegex = /(?:^|\n)\s*import\s+([\s\S]*?)\s+from\s+["']([^"']+)["']/g; |
| 163 | + const exportRegex = /(?:^|\n)\s*export\s+[\s\S]*?\s+from\s+["']([^"']+)["']/g; |
| 164 | + const bareImportRegex = /(?:^|\n)\s*import\s+["']([^"']+)["']/g; |
| 165 | + |
| 166 | + let match; |
| 167 | + while ((match = importRegex.exec(source)) !== null) { |
| 168 | + const specifier = match[1]; |
| 169 | + const importSource = match[2]; |
| 170 | + collectFromImportSource(importSource, parseImportSymbols(specifier), refs); |
| 171 | + } |
| 172 | + |
| 173 | + while ((match = exportRegex.exec(source)) !== null) { |
| 174 | + const importSource = match[1]; |
| 175 | + collectFromImportSource(importSource, [], refs); |
| 176 | + } |
| 177 | + |
| 178 | + while ((match = bareImportRegex.exec(source)) !== null) { |
| 179 | + const importSource = match[1]; |
| 180 | + collectFromImportSource(importSource, [], refs); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return [...refs] |
| 185 | + .filter(Boolean) |
| 186 | + .filter((value) => !isThemeEngineClass(value)) |
| 187 | + .sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" })); |
| 188 | +} |
| 189 | + |
| 190 | +function loadMetadata() { |
| 191 | + if (!fs.existsSync(METADATA_PATH)) { |
| 192 | + throw new Error("Missing metadata file: " + path.relative(ROOT, METADATA_PATH)); |
| 193 | + } |
| 194 | + |
| 195 | + let parsed; |
| 196 | + try { |
| 197 | + parsed = JSON.parse(readFile(METADATA_PATH)); |
| 198 | + } catch (error) { |
| 199 | + throw new Error("Invalid metadata JSON: " + error.message); |
| 200 | + } |
| 201 | + |
| 202 | + if (!parsed || typeof parsed !== "object" || !Array.isArray(parsed.games)) { |
| 203 | + throw new Error('Metadata must contain a "games" array.'); |
| 204 | + } |
| 205 | + return parsed; |
| 206 | +} |
| 207 | + |
| 208 | +function toGameDirFromHref(href) { |
| 209 | + const normalizedHref = String(href || "").replace(/\\/g, "/").trim(); |
| 210 | + if (!normalizedHref.startsWith("/games/") || !normalizedHref.endsWith("/index.html")) { |
| 211 | + return ""; |
| 212 | + } |
| 213 | + return path.join(ROOT, normalizedHref.slice(1, -"/index.html".length)); |
| 214 | +} |
| 215 | + |
| 216 | +function normalizeGamesPresentation() { |
| 217 | + const metadata = loadMetadata(); |
| 218 | + let updatedCount = 0; |
| 219 | + |
| 220 | + for (const game of metadata.games) { |
| 221 | + const gameDir = toGameDirFromHref(game?.href); |
| 222 | + if (!gameDir || !fs.existsSync(gameDir)) { |
| 223 | + continue; |
| 224 | + } |
| 225 | + |
| 226 | + const engineClassesUsed = collectEngineClassReferencesFromJs(gameDir); |
| 227 | + const previous = JSON.stringify(Array.isArray(game.engineClassesUsed) ? game.engineClassesUsed : []); |
| 228 | + const next = JSON.stringify(engineClassesUsed); |
| 229 | + if (previous !== next) { |
| 230 | + game.engineClassesUsed = engineClassesUsed; |
| 231 | + updatedCount += 1; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + writeFile(METADATA_PATH, JSON.stringify(metadata, null, 2) + "\n"); |
| 236 | + return { updatedCount, totalGames: metadata.games.length }; |
| 237 | +} |
| 238 | + |
| 239 | +function main() { |
| 240 | + const result = normalizeGamesPresentation(); |
| 241 | + console.log(`OK updated=${result.updatedCount} games=${result.totalGames}`); |
| 242 | +} |
| 243 | + |
| 244 | +try { |
| 245 | + main(); |
| 246 | +} catch (error) { |
| 247 | + console.error("FAIL " + error.message); |
| 248 | + process.exit(1); |
| 249 | +} |
0 commit comments