Files
CollabTable/shared/scripts/media/enforce-design-system-css.mjs
T
Gabriel20xx fd32419371
Docker / Docker Build & Push (push) Successful in 3m34s
Sync shared snapshot from 43d77dd
2026-09-01 08:44:09 +02:00

118 lines
3.7 KiB
JavaScript

#!/usr/bin/env node
/**
* Enforce design-system CSS constraints for shared theme files.
*
* Rules enforced:
* - Theme CSS must not contain project/service-coupled selector tokens.
* - Theme CSS must not contain route-coupled panel IDs in selectors.
*
* TODO(MediaEngine extraction): the original monorepo version of this script also asserted that
* MediaShared/public/css/index.css and MediaDesktop/public/index.css stayed byte-for-byte synced.
* That check is no longer possible from inside MediaEngine: MediaDesktop is now a fully separate
* repo (Original/MediaDesktop) and isn't checked out alongside this one, so there is nothing here
* to diff it against. The desktop-vs-shared theme sync check has been dropped rather than pointed
* at a nonexistent path; if that guarantee still matters, it belongs in MediaDesktop's own CI
* (comparing its vendored shared/ copy's theme file against its own), not here.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, '..', '..');
// MediaShared/public/css/index.css became packages/media-shared-assets/public/css/index.css
// once media-backend/media-webui were folded directly into the shared repo (no more vendored copy).
const sharedThemePath = path.join(
repoRoot,
'packages',
'media-shared-assets',
'public',
'css',
'index.css'
);
const bannedSelectorRules = [
{
name: 'project-coupled service token',
regex: /\b(?:kiosk|viewer|moderator|generator|library|analyzer|downloader|servant)-page\b/i,
},
{
name: 'project-coupled state class token',
regex:
/\b(?:viewer|kiosk|moderator|generator|library|analyzer|downloader|servant)-(?:home|page|stage|mode|active|panel)\b/i,
},
{
name: 'project-prefixed keyframe name',
regex:
/@keyframes\s+nc-(?:viewer|kiosk|moderator|generator|library|analyzer|downloader|servant)/i,
},
{
name: 'project-coupled data attribute',
regex:
/\[data-(?:kiosk|viewer|moderator|generator|library|analyzer|downloader|servant)[^\]]*\]/i,
},
{
name: 'route-coupled panel selector',
regex: /#panel-[a-z0-9_-]+/i,
},
];
function readUtf8(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
function stripCssComments(text) {
return text.replace(/\/\*[\s\S]*?\*\//g, '');
}
function findViolations(filePath, content) {
const cleaned = stripCssComments(content);
const lines = cleaned.split(/\r?\n/);
const violations = [];
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
for (const rule of bannedSelectorRules) {
if (rule.regex.test(line)) {
violations.push({
filePath,
line: i + 1,
rule: rule.name,
snippet: line.trim(),
});
}
}
}
return violations;
}
function main() {
if (!fs.existsSync(sharedThemePath)) {
console.error('[enforce-design-system-css] Missing expected theme file:');
console.error(` - ${path.relative(repoRoot, sharedThemePath)}`);
process.exit(1);
}
const sharedTheme = readUtf8(sharedThemePath);
const allViolations = findViolations(sharedThemePath, sharedTheme);
if (allViolations.length > 0) {
console.error('[enforce-design-system-css] Design-system CSS violations found:');
for (const v of allViolations) {
const rel = path.isAbsolute(v.filePath) ? path.relative(repoRoot, v.filePath) : v.filePath;
console.error(` - ${rel}:${v.line} [${v.rule}] ${v.snippet}`);
}
console.error('[enforce-design-system-css] Fix violations before running lint/test.');
process.exit(1);
}
console.log(
'[enforce-design-system-css] OK: shared theme is free of project-coupled selectors.'
);
}
main();