|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import crypto from "node:crypto"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | +const repoRoot = path.resolve(__dirname, "../.."); |
| 9 | +const baselinePath = path.join(__dirname, "checkPhase24CloseoutExecutionGuard.baseline.json"); |
| 10 | + |
| 11 | +const SOURCE_EXTENSIONS = new Set([".js", ".mjs", ".cjs", ".html", ".json"]); |
| 12 | +const SKIP_DIRS = new Set(["metadata", "shared", "_shared"]); |
| 13 | + |
| 14 | +function toPosix(value) { |
| 15 | + return value.replace(/\\/g, "/"); |
| 16 | +} |
| 17 | + |
| 18 | +function readJson(filePath) { |
| 19 | + return JSON.parse(fs.readFileSync(filePath, "utf8")); |
| 20 | +} |
| 21 | + |
| 22 | +function sha256(text) { |
| 23 | + return crypto.createHash("sha256").update(text, "utf8").digest("hex"); |
| 24 | +} |
| 25 | + |
| 26 | +function normalizeRoadmapStatusMarkers(roadmapText) { |
| 27 | + const lf = roadmapText.replace(/\r\n/g, "\n"); |
| 28 | + return lf.replace(/^([ \t]*[-*]?\s*\[)(?: |\.|x)(\])/gm, "$1#$2"); |
| 29 | +} |
| 30 | + |
| 31 | +function collectSampleFiles(samplesRoot) { |
| 32 | + const out = []; |
| 33 | + const stack = [samplesRoot]; |
| 34 | + while (stack.length > 0) { |
| 35 | + const current = stack.pop(); |
| 36 | + const entries = fs.readdirSync(current, { withFileTypes: true }); |
| 37 | + for (const entry of entries) { |
| 38 | + const full = path.join(current, entry.name); |
| 39 | + if (entry.isDirectory()) { |
| 40 | + const isSamplesRootChild = path.resolve(current) === path.resolve(samplesRoot); |
| 41 | + if (isSamplesRootChild && SKIP_DIRS.has(entry.name)) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + stack.push(full); |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (SOURCE_EXTENSIONS.has(path.extname(entry.name).toLowerCase())) { |
| 48 | + out.push(full); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return out; |
| 53 | +} |
| 54 | + |
| 55 | +function runGuard() { |
| 56 | + if (!fs.existsSync(baselinePath)) { |
| 57 | + throw new Error(`Missing baseline: ${toPosix(path.relative(repoRoot, baselinePath))}`); |
| 58 | + } |
| 59 | + |
| 60 | + const baseline = readJson(baselinePath); |
| 61 | + const roadmapPath = path.join(repoRoot, baseline.roadmapPath); |
| 62 | + const errors = []; |
| 63 | + |
| 64 | + if (!fs.existsSync(roadmapPath)) { |
| 65 | + errors.push(`Roadmap path missing: ${baseline.roadmapPath}`); |
| 66 | + } else { |
| 67 | + const roadmapText = fs.readFileSync(roadmapPath, "utf8"); |
| 68 | + const normalized = normalizeRoadmapStatusMarkers(roadmapText); |
| 69 | + const hash = sha256(normalized); |
| 70 | + if (hash !== baseline.statusInsensitiveSha256) { |
| 71 | + errors.push( |
| 72 | + `Roadmap non-status drift detected at ${baseline.roadmapPath}. Expected ${baseline.statusInsensitiveSha256}, got ${hash}.` |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + const samplesRoot = path.join(repoRoot, "samples"); |
| 78 | + const allowedRoot = path.join(repoRoot, baseline.fullscreenAllowedRoot); |
| 79 | + const patterns = (baseline.fullscreenPatterns || []).map((token) => ({ token, re: new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g") })); |
| 80 | + const hitsOutsideAllowed = []; |
| 81 | + let totalHits = 0; |
| 82 | + |
| 83 | + for (const filePath of collectSampleFiles(samplesRoot)) { |
| 84 | + const source = fs.readFileSync(filePath, "utf8"); |
| 85 | + let matched = false; |
| 86 | + for (const { re } of patterns) { |
| 87 | + if (re.test(source)) { |
| 88 | + matched = true; |
| 89 | + } |
| 90 | + re.lastIndex = 0; |
| 91 | + } |
| 92 | + if (!matched) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + totalHits += 1; |
| 96 | + if (!path.resolve(filePath).startsWith(path.resolve(allowedRoot))) { |
| 97 | + hitsOutsideAllowed.push(toPosix(path.relative(repoRoot, filePath))); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (hitsOutsideAllowed.length > 0) { |
| 102 | + errors.push( |
| 103 | + `Fullscreen policy violation outside ${baseline.fullscreenAllowedRoot}:\n- ${hitsOutsideAllowed.join("\n- ")}` |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + if (errors.length > 0) { |
| 108 | + console.error("PHASE24_CLOSEOUT_EXECUTION_GUARD_FAIL"); |
| 109 | + errors.forEach((item) => console.error(item)); |
| 110 | + process.exitCode = 1; |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + console.log("PHASE24_CLOSEOUT_EXECUTION_GUARD_PASS"); |
| 115 | + console.log(`Roadmap lock path: ${baseline.roadmapPath}`); |
| 116 | + console.log(`Fullscreen sample hits: ${totalHits}`); |
| 117 | + console.log(`Fullscreen outside allowed root: 0`); |
| 118 | +} |
| 119 | + |
| 120 | +runGuard(); |
0 commit comments