|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { getToolById } from "../tools/toolRegistry.js"; |
| 5 | +import { |
| 6 | + PROJECT_MANIFEST_SCHEMA, |
| 7 | + PROJECT_MANIFEST_VERSION, |
| 8 | + serializeProjectManifest, |
| 9 | + validateProjectManifest |
| 10 | +} from "../tools/shared/projectManifestContract.js"; |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = path.dirname(__filename); |
| 14 | +const repoRoot = path.resolve(__dirname, ".."); |
| 15 | + |
| 16 | +const TEMPLATE_MANIFEST_PATH = "templates/starter-project-template/config/starter.project.json"; |
| 17 | +const REPORT_PATH = "docs/dev/reports/starter_project_template_validation.txt"; |
| 18 | + |
| 19 | +function assert(condition, message, issues) { |
| 20 | + if (!condition) { |
| 21 | + issues.push(message); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function pathExists(repoRelativePath) { |
| 26 | + try { |
| 27 | + await fs.access(path.join(repoRoot, repoRelativePath)); |
| 28 | + return true; |
| 29 | + } catch { |
| 30 | + return false; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function main() { |
| 35 | + const issues = []; |
| 36 | + const notes = []; |
| 37 | + const raw = JSON.parse(await fs.readFile(path.join(repoRoot, TEMPLATE_MANIFEST_PATH), "utf8")); |
| 38 | + const validation = validateProjectManifest(raw); |
| 39 | + |
| 40 | + assert(validation.valid, `Project manifest is invalid: ${validation.issues.join(" ")}`, issues); |
| 41 | + assert(validation.manifest.schema === PROJECT_MANIFEST_SCHEMA, "Starter project schema mismatch.", issues); |
| 42 | + assert(validation.manifest.version === PROJECT_MANIFEST_VERSION, "Starter project version mismatch.", issues); |
| 43 | + notes.push("starter project manifest validates against shared project contract"); |
| 44 | + |
| 45 | + const manifest = validation.manifest; |
| 46 | + const assets = Array.isArray(manifest.sharedLibrary?.assets) ? manifest.sharedLibrary.assets : []; |
| 47 | + const palettes = Array.isArray(manifest.sharedLibrary?.palettes) ? manifest.sharedLibrary.palettes : []; |
| 48 | + |
| 49 | + const requiredTypes = ["vector", "tilemap", "parallax", "sprite"]; |
| 50 | + for (const requiredType of requiredTypes) { |
| 51 | + assert(assets.some((entry) => entry?.type === requiredType), `Missing shared ${requiredType} reference in starter project sharedLibrary.assets.`, issues); |
| 52 | + } |
| 53 | + assert(palettes.length >= 1, "Missing shared palette reference in starter project sharedLibrary.palettes.", issues); |
| 54 | + |
| 55 | + for (const entry of [...assets, ...palettes]) { |
| 56 | + assert(typeof entry?.sourcePath === "string" && entry.sourcePath.trim().length > 0, `Missing sourcePath for shared reference ${entry?.id || "unknown"}.`, issues); |
| 57 | + if (typeof entry?.sourcePath === "string" && entry.sourcePath.trim()) { |
| 58 | + assert(await pathExists(entry.sourcePath), `Referenced shared asset/palette path does not exist: ${entry.sourcePath}`, issues); |
| 59 | + } |
| 60 | + } |
| 61 | + notes.push("shared vector/tilemap/parallax/sprite/palette references resolve on disk"); |
| 62 | + |
| 63 | + const requiredTools = [ |
| 64 | + "vector-map-editor", |
| 65 | + "vector-asset-studio", |
| 66 | + "tile-map-editor", |
| 67 | + "parallax-editor", |
| 68 | + "sprite-editor", |
| 69 | + "asset-browser", |
| 70 | + "palette-browser" |
| 71 | + ]; |
| 72 | + for (const toolId of requiredTools) { |
| 73 | + assert(manifest.tools && typeof manifest.tools[toolId] === "object", `Missing starter tool payload for ${toolId}.`, issues); |
| 74 | + } |
| 75 | + notes.push("starter project includes payloads for all active first-class tools"); |
| 76 | + |
| 77 | + assert(Boolean(manifest.tools?.["vector-map-editor"]?.snapshot?.documentData), "Vector Map Editor payload is missing snapshot.documentData.", issues); |
| 78 | + assert(typeof manifest.tools?.["vector-asset-studio"]?.svgText === "string", "Vector Asset Studio payload is missing svgText.", issues); |
| 79 | + assert(Boolean(manifest.tools?.["tile-map-editor"]?.documentModel?.assetRefs?.tilemapId), "Tilemap Studio payload is missing tilemap asset reference.", issues); |
| 80 | + assert(Array.isArray(manifest.tools?.["parallax-editor"]?.documentModel?.assetRefs?.parallaxSourceIds), "Parallax Scene Studio payload is missing parallax source ids.", issues); |
| 81 | + assert(Boolean(manifest.tools?.["sprite-editor"]?.project?.assetRefs?.spriteId), "Sprite Editor payload is missing sprite asset reference.", issues); |
| 82 | + notes.push("cross-tool loading payloads are structurally present"); |
| 83 | + |
| 84 | + const roundTripped = JSON.parse(serializeProjectManifest(manifest)); |
| 85 | + assert(roundTripped.schema === PROJECT_MANIFEST_SCHEMA, "Serialized starter project lost schema.", issues); |
| 86 | + assert(roundTripped.name === manifest.name, "Serialized starter project changed project name.", issues); |
| 87 | + assert(Array.isArray(roundTripped.sharedLibrary?.assets), "Serialized starter project lost sharedLibrary assets.", issues); |
| 88 | + notes.push("starter project serializes and reloads through shared project serializer"); |
| 89 | + |
| 90 | + const spriteEditor = getToolById("sprite-editor"); |
| 91 | + const legacySprite = getToolById("sprite-editor-old-keep"); |
| 92 | + assert(spriteEditor?.active === true && spriteEditor?.visibleInToolsList === true, "Sprite Editor must remain first-class and visible.", issues); |
| 93 | + assert(legacySprite?.active !== true && legacySprite?.visibleInToolsList !== true, "SpriteEditor_old_keep must remain hidden legacy.", issues); |
| 94 | + notes.push("sprite tool visibility rules remain intact"); |
| 95 | + |
| 96 | + const toolsIndex = await fs.readFile(path.join(repoRoot, "tools/index.html"), "utf8"); |
| 97 | + assert(!/samples\//i.test(toolsIndex), "tools/index.html must remain tool-only and sample-free.", issues); |
| 98 | + notes.push("tools/index.html remains tool-only and sample-free"); |
| 99 | + |
| 100 | + const reportLines = [ |
| 101 | + "BUILD_PR_STARTER_PROJECT_TEMPLATE validation report", |
| 102 | + "", |
| 103 | + issues.length === 0 ? "STATUS: PASS" : "STATUS: FAIL", |
| 104 | + "", |
| 105 | + "Checks:", |
| 106 | + ...notes.map((note) => `- ${note}`), |
| 107 | + "", |
| 108 | + "Issues:", |
| 109 | + ...(issues.length > 0 ? issues.map((issue) => `- ${issue}`) : ["- none"]) |
| 110 | + ]; |
| 111 | + |
| 112 | + await fs.writeFile(path.join(repoRoot, REPORT_PATH), `${reportLines.join("\n")}\n`, "utf8"); |
| 113 | + |
| 114 | + if (issues.length > 0) { |
| 115 | + console.error("STARTER_PROJECT_TEMPLATE_INVALID"); |
| 116 | + issues.forEach((issue) => console.error(`- ${issue}`)); |
| 117 | + process.exitCode = 1; |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + console.log("STARTER_PROJECT_TEMPLATE_VALID"); |
| 122 | + console.log(`Report: ${REPORT_PATH}`); |
| 123 | +} |
| 124 | + |
| 125 | +await main(); |
0 commit comments