|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | + |
| 3 | +function asCanvas(value) { |
| 4 | + if (typeof value === 'string') return JSON.parse(value); |
| 5 | + return value; |
| 6 | +} |
| 7 | + |
| 8 | +export function validateCanvasEntry(canvasInput, nodeId) { |
| 9 | + const canvas = asCanvas(canvasInput); |
| 10 | + const errors = []; |
| 11 | + const warnings = []; |
| 12 | + |
| 13 | + if (canvas.version !== 'v0') { |
| 14 | + warnings.push('canvas.version should be "v0".'); |
| 15 | + } |
| 16 | + if (!Array.isArray(canvas.nodes)) { |
| 17 | + return { ok: false, errors: ['canvas.nodes must be an array.'], warnings }; |
| 18 | + } |
| 19 | + |
| 20 | + const node = canvas.nodes.find((candidate) => candidate.id === nodeId); |
| 21 | + if (!node) { |
| 22 | + return { ok: false, errors: [`Missing canvas node ${nodeId}.`], warnings }; |
| 23 | + } |
| 24 | + |
| 25 | + if (!['page', 'popup', 'hud'].includes(node.type)) { |
| 26 | + errors.push('node.type must be page, popup, or hud.'); |
| 27 | + } |
| 28 | + if (!node.title) { |
| 29 | + errors.push('node.title is required.'); |
| 30 | + } |
| 31 | + if (!node.pos || typeof node.pos.x !== 'number' || typeof node.pos.y !== 'number') { |
| 32 | + errors.push('node.pos.x and node.pos.y are required numbers.'); |
| 33 | + } |
| 34 | + if (['page', 'popup', 'hud'].includes(node.type) && !node.entry) { |
| 35 | + errors.push('node.entry is required for page, popup, and hud nodes.'); |
| 36 | + } |
| 37 | + if (node.type === 'popup' && !node.parent_id) { |
| 38 | + errors.push('popup nodes should declare parent_id.'); |
| 39 | + } |
| 40 | + if (node.entry && /__studio_runtime__|assets\/main\.scene/.test(node.entry)) { |
| 41 | + errors.push('node.entry must point to the authored page/popup/HUD source, not a runtime scene.'); |
| 42 | + } |
| 43 | + |
| 44 | + return { ok: errors.length === 0, errors, warnings, node }; |
| 45 | +} |
| 46 | + |
| 47 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 48 | + const [path, nodeId] = process.argv.slice(2); |
| 49 | + if (!path || !nodeId) { |
| 50 | + console.error('Usage: node <path-to-this-skill>/scripts/validate-canvas-entry.mjs <canvas.json> <node-id>'); |
| 51 | + process.exit(2); |
| 52 | + } |
| 53 | + try { |
| 54 | + const result = validateCanvasEntry(await readFile(path, 'utf8'), nodeId); |
| 55 | + console.log(JSON.stringify(result, null, 2)); |
| 56 | + process.exit(result.ok ? 0 : 1); |
| 57 | + } catch (error) { |
| 58 | + console.error(error.message); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | +} |
0 commit comments