Files
shared/scripts/media/enforce-js-mapping.mjs
T
Gabriel20xxandClaude Sonnet 5 ee59571552 Rename ProjectConfigurator, restructure Projects workspace, retire MediaCollection naming
- Rename MediaConfig -> ProjectConfigurator throughout; drop the Parameters
  tab/page; rename the Mounts tab to Projects and restructure it into three
  sections: Central services, Main services, Third-party services
- Add a self-describe contract (GET /api/info returning name/tabs/mountTypes)
  and a server-side discovery proxy (POST /api/discover-service) so adding a
  main service only requires a URL
- Add ProjectConfigurator HA: multiple instances now sync their settings with
  each other (GET /api/settings/export, POST /api/settings/sync, a periodic
  background puller), last-write-wins per key by updated_at
- Add a generic, project-agnostic file-storage API on CarrierNode
  (GET/POST/DELETE /api/mount/{project}/{type}[/*]) driven by the same
  configured_mounts data the Projects workspace already writes, usable by any
  registered project, not just the media family
- Add last_login_at tracking to the shared auth Store/User (schema, queries,
  UpdateLastLogin) and surface it through the shared web-ui ManagedUser type
  and UserManagement component
- Retire the "MediaCollection" name: Go module -> repos.gfranz.ch/Gabriel20xx/shared/media-backend,
  binary -> mediabackend, session cookie default -> "session", media-webui
  merged into webui, media-shared-assets renamed to legacy-assets and pruned
  to the one file MediaDesktop still uses

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-04 15:02:16 +02:00

130 lines
5.1 KiB
JavaScript

/**
* 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
* 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' },
{ name: 'MediaTools', publicDir: 'MediaTools/public' },
{ name: 'MediaTools Converter', publicDir: 'MediaTools/public/converter' },
{ name: 'MediaTools Generator', publicDir: 'MediaTools/generator/public' },
{ name: 'MediaTools Downloader', publicDir: 'MediaTools/downloader/public' },
{ name: 'MediaKiosk', publicDir: 'MediaKiosk/public' },
{ name: 'MediaViewer', publicDir: 'MediaViewer/public' },
{ name: 'MediaAnalyzer', publicDir: 'MediaAnalyzer/public' },
{ 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.'
);