/** * enforce-js-mapping.mjs * Enforces the 1:1 HTML→JS naming rule per §7.1: * public/ must contain exactly as many .js files as .html files, * with matching names (index.html → index.js; all others name.html → name.js). * * Run: node scripts/shared/enforce-js-mapping.mjs * * TODO(MediaEngine extraction): §7.1's 1:1 HTML/JS rule applied to the legacy pre-SvelteKit * `/public/*.html` + `*.js` pages below. MediaEngine's webui/ is SvelteKit-based (routes * under webui/src/routes, no public/*.html+*.js pairs), and none of the other project directories * listed below live inside this repo anymore (they're now separate repos). The existsSync guard * per project means this currently no-ops safely (nothing found to check). Left as-is rather than * inventing a SvelteKit-equivalent rule — remove or repurpose if useful. */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.resolve(__dirname, '..', '..'); const log = (...args) => console.log('[enforce-js-mapping]', ...args); function readFile(p) { return fs.readFileSync(p, 'utf8'); } function writeFile(p, content) { fs.writeFileSync(p, content, 'utf8'); } // eslint-disable-next-line no-unused-vars function moveFile(src, dst) { fs.mkdirSync(path.dirname(dst), { recursive: true }); fs.renameSync(src, dst); } // eslint-disable-next-line no-unused-vars function deleteFile(p) { if (fs.existsSync(p)) fs.unlinkSync(p); } // eslint-disable-next-line no-unused-vars function ensureDir(p) { fs.mkdirSync(p, { recursive: true }); } // Replace a src path in an HTML file (exact string match on the attribute value) // eslint-disable-next-line no-unused-vars function replaceSrc(htmlPath, oldSrc, newSrc) { let content = readFile(htmlPath); // Match both quoted variants, relative and absolute const escaped = oldSrc.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const re = new RegExp(`(src=["'])${escaped}(["'])`, 'g'); if (!re.test(content)) { log(` WARN: could not find src="${oldSrc}" in ${path.relative(ROOT, htmlPath)}`); return; } content = content.replace(re, `$1${newSrc}$2`); writeFile(htmlPath, content); log(` HTML: ${oldSrc} → ${newSrc}`); } // Remove an entire