Files
CollabTable/shared/scripts/media/enforce-js-mapping.mjs
T

129 lines
5.1 KiB
JavaScript
Raw Normal View History

2026-09-01 08:43:25 +02:00
/**
* 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(shared extraction): §7.1's 1:1 HTML/JS rule applied to the legacy pre-SvelteKit
* `<project>/public/*.html` + `*.js` pages below. this repo's webui/ is SvelteKit-based (routes
2026-09-01 08:43:25 +02:00
* 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 <script ... src="..."> line from an HTML file
// eslint-disable-next-line no-unused-vars
function removeScriptLine(htmlPath, srcValue) {
let content = readFile(htmlPath);
const escaped = srcValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`[ \t]*<script[^>]*src=["']${escaped}["'][^>]*>[ \t]*\n?`, 'g');
if (!re.test(content)) {
log(` WARN: could not find script src="${srcValue}" in ${path.relative(ROOT, htmlPath)}`);
return;
}
content = content.replace(re, '');
writeFile(htmlPath, content);
log(` HTML: removed script src="${srcValue}"`);
}
// Append content (from init file) to end of another JS file
// eslint-disable-next-line no-unused-vars
function appendContent(targetPath, appendContent) {
const existing = readFile(targetPath);
writeFile(targetPath, existing.trimEnd() + '\n\n' + appendContent.trimStart());
}
// ─── Verification ─────────────────────────────────────────────────────────────
log('\n── Verification ──');
const projects = [
{ name: 'MediaModerator', publicDir: 'MediaModerator/public' },
2026-09-06 22:50:38 +02:00
{ name: 'CraftStudio', publicDir: 'CraftStudio/public' },
{ name: 'CraftStudio Converter', publicDir: 'CraftStudio/public/converter' },
{ name: 'CraftStudio Generator', publicDir: 'CraftStudio/generator/public' },
{ name: 'CraftStudio Downloader', publicDir: 'CraftStudio/downloader/public' },
2026-09-01 08:43:25 +02:00
{ name: 'MediaKiosk', publicDir: 'MediaKiosk/public' },
{ name: 'MediaViewer', publicDir: 'MediaViewer/public' },
2026-09-06 22:50:38 +02:00
{ name: 'ContentScanner', publicDir: 'ContentScanner/public' },
2026-09-01 08:43:25 +02:00
{ name: 'MediaDesktop', publicDir: 'MediaDesktop/public' },
];
let allOk = true;
for (const { name, publicDir } of projects) {
const pd = path.join(ROOT, publicDir);
if (!fs.existsSync(pd)) continue;
const htmlFiles = fs
.readdirSync(pd)
.filter(f => f.endsWith('.html'))
.map(f => f.replace(/\.html$/, ''));
const jsFiles = fs
.readdirSync(pd)
.filter(f => f.endsWith('.js'))
.map(f => f.replace(/\.js$/, ''));
// Expected mapping: name.html → name (index.html → index.js, no special case)
const expectedJs = htmlFiles.map(h => h).sort();
const actualJs = jsFiles.sort();
const missing = expectedJs.filter(j => !actualJs.includes(j));
const extra = actualJs.filter(j => !expectedJs.includes(j));
const ok = missing.length === 0 && extra.length === 0;
if (!ok) allOk = false;
const status = ok ? '✅' : '❌';
log(` ${status} ${name}: HTML=[${htmlFiles.sort().join(', ')}] JS=[${actualJs.join(', ')}]`);
if (missing.length) log(` MISSING JS: ${missing.join(', ')}`);
if (extra.length) log(` EXTRA JS: ${extra.join(', ')}`);
}
log(
allOk
? '\n✅ All projects satisfy the 1:1 HTML↔JS mapping rule.'
: '\n❌ Some projects have violations — review above.'
);