|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = path.dirname(__filename); |
| 7 | +const repoRoot = path.resolve(__dirname, ".."); |
| 8 | + |
| 9 | +const GAMES_ROOT = path.join(repoRoot, "games"); |
| 10 | +const REPORT_PATH = path.join(repoRoot, "docs/dev/reports/games_template_contract_validation.txt"); |
| 11 | +const MANAGED_CANONICAL_GAMES = ["PacmanLite", "SpaceInvaders"]; |
| 12 | +const REQUIRED_DIRS = ["assets", "game", "entities", "systems", "ui", "debug"]; |
| 13 | +const REQUIRED_INDEX_PATTERNS = [ |
| 14 | + { id: "canvas", test: (text) => /<canvas\b/i.test(text), message: "index.html must include a <canvas> element." }, |
| 15 | + { |
| 16 | + id: "base-layout", |
| 17 | + test: (text) => /\/src\/engine\/ui\/baseLayout\.css/i.test(text), |
| 18 | + message: "index.html must include /src/engine/ui/baseLayout.css." |
| 19 | + } |
| 20 | +]; |
| 21 | +const SOURCE_FILE_EXTENSIONS = new Set([".js", ".mjs", ".cjs", ".html"]); |
| 22 | + |
| 23 | +function toRepoRelative(targetPath) { |
| 24 | + return path.relative(repoRoot, targetPath).replace(/\\/g, "/"); |
| 25 | +} |
| 26 | + |
| 27 | +async function resolveContractTargets() { |
| 28 | + const entries = await fs.readdir(GAMES_ROOT, { withFileTypes: true }); |
| 29 | + const entryNames = new Set(entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name)); |
| 30 | + const targets = []; |
| 31 | + const requiredStaticTargets = ["_template", ...MANAGED_CANONICAL_GAMES]; |
| 32 | + |
| 33 | + for (const requiredTarget of requiredStaticTargets) { |
| 34 | + if (!entryNames.has(requiredTarget)) { |
| 35 | + throw new Error(`Required contract target is missing: games/${requiredTarget}`); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + for (const entry of entries) { |
| 40 | + if (!entry.isDirectory()) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + const gameName = entry.name; |
| 44 | + const gameRoot = path.join(GAMES_ROOT, gameName); |
| 45 | + const gameEntries = await fs.readdir(gameRoot, { withFileTypes: true }); |
| 46 | + const inScope = ( |
| 47 | + gameName === "_template" |
| 48 | + || gameName.endsWith("_next") |
| 49 | + || MANAGED_CANONICAL_GAMES.includes(gameName) |
| 50 | + ); |
| 51 | + if (inScope) { |
| 52 | + targets.push({ |
| 53 | + gameName, |
| 54 | + gameRoot, |
| 55 | + gameEntries |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return targets.sort((a, b) => a.gameName.localeCompare(b.gameName)); |
| 61 | +} |
| 62 | + |
| 63 | +async function listFilesRecursively(rootPath) { |
| 64 | + const output = []; |
| 65 | + const queue = [rootPath]; |
| 66 | + |
| 67 | + while (queue.length > 0) { |
| 68 | + const current = queue.pop(); |
| 69 | + const entries = await fs.readdir(current, { withFileTypes: true }); |
| 70 | + for (const entry of entries) { |
| 71 | + const fullPath = path.join(current, entry.name); |
| 72 | + if (entry.isDirectory()) { |
| 73 | + queue.push(fullPath); |
| 74 | + continue; |
| 75 | + } |
| 76 | + output.push(fullPath); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return output; |
| 81 | +} |
| 82 | + |
| 83 | +function collectQuotedGamePathViolations({ gameName, repoRelativePath, text }) { |
| 84 | + const issues = []; |
| 85 | + const matches = text.matchAll(/["'`](\/games\/([^\/"'`]+)\/[^"'`]*)["'`]/g); |
| 86 | + for (const match of matches) { |
| 87 | + const referencedGame = match[2]; |
| 88 | + const referencedPath = match[1]; |
| 89 | + if (referencedGame !== gameName) { |
| 90 | + issues.push( |
| 91 | + `${repoRelativePath} references another game path (${referencedPath}).` |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + return issues; |
| 96 | +} |
| 97 | + |
| 98 | +async function validateTarget(target) { |
| 99 | + const issues = []; |
| 100 | + const notes = []; |
| 101 | + const entryNames = new Set(target.gameEntries.map((entry) => entry.name)); |
| 102 | + |
| 103 | + for (const requiredDir of REQUIRED_DIRS) { |
| 104 | + if (!entryNames.has(requiredDir)) { |
| 105 | + issues.push(`${target.gameName}: missing required directory ${requiredDir}/.`); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (!entryNames.has("index.html")) { |
| 110 | + issues.push(`${target.gameName}: missing required index.html.`); |
| 111 | + } else { |
| 112 | + const indexPath = path.join(target.gameRoot, "index.html"); |
| 113 | + const indexText = await fs.readFile(indexPath, "utf8"); |
| 114 | + for (const pattern of REQUIRED_INDEX_PATTERNS) { |
| 115 | + if (!pattern.test(indexText)) { |
| 116 | + issues.push(`${target.gameName}: ${pattern.message}`); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + const files = await listFilesRecursively(target.gameRoot); |
| 122 | + for (const filePath of files) { |
| 123 | + const ext = path.extname(filePath).toLowerCase(); |
| 124 | + if (!SOURCE_FILE_EXTENSIONS.has(ext)) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + const text = await fs.readFile(filePath, "utf8"); |
| 128 | + const repoRelativePath = toRepoRelative(filePath); |
| 129 | + const pathIssues = collectQuotedGamePathViolations({ |
| 130 | + gameName: target.gameName, |
| 131 | + repoRelativePath, |
| 132 | + text |
| 133 | + }); |
| 134 | + issues.push(...pathIssues); |
| 135 | + } |
| 136 | + |
| 137 | + if (issues.length === 0) { |
| 138 | + notes.push(`${target.gameName}: structure and shell contract checks passed.`); |
| 139 | + } |
| 140 | + |
| 141 | + return { issues, notes }; |
| 142 | +} |
| 143 | + |
| 144 | +async function main() { |
| 145 | + const issues = []; |
| 146 | + const notes = []; |
| 147 | + |
| 148 | + const targets = await resolveContractTargets(); |
| 149 | + if (targets.length === 0) { |
| 150 | + issues.push("No contract-managed game targets found under games/."); |
| 151 | + } |
| 152 | + |
| 153 | + for (const target of targets) { |
| 154 | + const result = await validateTarget(target); |
| 155 | + issues.push(...result.issues); |
| 156 | + notes.push(...result.notes); |
| 157 | + } |
| 158 | + |
| 159 | + const reportLines = [ |
| 160 | + "BUILD_PR_GAMES_TEMPLATE_CONTRACT_ENFORCEMENT validation report", |
| 161 | + "", |
| 162 | + issues.length === 0 ? "STATUS: PASS" : "STATUS: FAIL", |
| 163 | + "", |
| 164 | + "Targets:", |
| 165 | + ...targets.map((target) => `- games/${target.gameName}`), |
| 166 | + "", |
| 167 | + "Checks:", |
| 168 | + ...notes.map((note) => `- ${note}`), |
| 169 | + "", |
| 170 | + "Issues:", |
| 171 | + ...(issues.length > 0 ? issues.map((issue) => `- ${issue}`) : ["- none"]) |
| 172 | + ]; |
| 173 | + |
| 174 | + await fs.writeFile(REPORT_PATH, `${reportLines.join("\n")}\n`, "utf8"); |
| 175 | + |
| 176 | + if (issues.length > 0) { |
| 177 | + console.error("GAMES_TEMPLATE_CONTRACT_INVALID"); |
| 178 | + issues.forEach((issue) => console.error(`- ${issue}`)); |
| 179 | + process.exitCode = 1; |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + console.log("GAMES_TEMPLATE_CONTRACT_VALID"); |
| 184 | + console.log(`Report: ${toRepoRelative(REPORT_PATH)}`); |
| 185 | +} |
| 186 | + |
| 187 | +await main(); |
0 commit comments