gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit876b2064876b2064docs: workaround note for hybriel#130 (slot marker mid-line)mre876b2064/theme/check-theme.mjs

5.0 KB

  1. #!/usr/bin/env node
  2. // theme/check-theme.mjs — THE THEME GATE HELPER an app runs: it flags what bypasses the WorldAPI theme in the app's own code.
  3. // node theme/check-theme.mjs [appDir] [--ignore <path prefix>]…
  4. // 1. HARD-CODED COLOURS: #hex, rgb()/rgba(), hsl()/hsla(), oklch(), and named colours (white, black, red …) in .hl and .css
  5. // files. Colours come from the theme's named tokens (shared/tokens.hl, `colorText`, 'var(--color-text)'). A fallback inside
  6. // `var(--token, …)` is allowed (the components of components.hybriel carry those so a copy works without the theme).
  7. // 2. DECORATIVE BORDERS: any border / borderTop … / borderColor that is not 0 / none. Allowed only where a border MEANS
  8. // something (docs/design-rules.md): on a line about details/summary (foldables), drop zones, focus or an invalid field,
  9. // or on a line that says so with the comment `// functional: <why>`.
  10. // Never scanned: plugins/ bin/ node_modules/ storage/ .scratch/ .git/ .sessions/ and shared/tokens.hl (the one place for raw
  11. // values). More paths to skip (e.g. a vendored component folder whose frames the theme removes): --ignore, or one prefix per
  12. // line in `.check-theme-ignore` in the app folder.
  13. // Output: one `file:line: problem — text` per finding, then `check-theme: N files, M problems`; exit 1 when M > 0.
  14. import { readFileSync, readdirSync, statSync, existsSync } from 'node:fs';
  15. import { join, relative, resolve } from 'node:path';
  16. const args = process.argv.slice(2);
  17. let dir = '.';
  18. const ignore = ['plugins/', 'bin/', 'node_modules/', 'storage/', '.scratch/', '.git/', '.sessions/', 'shared/tokens.hl'];
  19. for (let i = 0; i < args.length; i++) {
  20. if (args[i] === '--ignore') ignore.push(args[++i]);
  21. else dir = args[i];
  22. }
  23. const APP = resolve(dir);
  24. const ignoreFile = join(APP, '.check-theme-ignore');
  25. if (existsSync(ignoreFile)) {
  26. for (const line of readFileSync(ignoreFile, 'utf8').split('\n')) {
  27. const p = line.replace(/#.*/, '').trim();
  28. if (p) ignore.push(p);
  29. }
  30. }
  31. function files(d) {
  32. const out = [];
  33. for (const name of readdirSync(d)) {
  34. const full = join(d, name);
  35. const rel = relative(APP, full);
  36. const st = statSync(full);
  37. if (ignore.some(p => rel === p.replace(/\/$/, '') || rel.startsWith(p.endsWith('/') ? p : p + '/') || rel === p)) continue;
  38. if (st.isDirectory()) out.push(...files(full));
  39. else if (/\.(hl|css)$/.test(name)) out.push(full);
  40. }
  41. return out;
  42. }
  43. const NAMED = 'white|black|red|green|blue|gray|grey|yellow|orange|purple|pink|brown|silver|navy|teal|maroon|olive|lime|aqua|fuchsia|cyan|magenta|gold|indigo|violet';
  44. const COLOUR = new RegExp(`(#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3}(?:[0-9a-fA-F]{2})?)?|#[0-9a-fA-F]{4})(?![0-9a-zA-Z_-])|\\b(?:rgba?|hsla?|oklch|oklab|lab|lch|hwb)\\(`);
  45. const NAMED_RE = new RegExp(`[=:]\\s*['"]?[^'";]*\\b(${NAMED})\\b`, 'i');
  46. // a css declaration that sets a colour: hl (`color = '…'`, `background = …`) or css (`color: …`)
  47. const COLOUR_PROP = /\b(color|background(?:Color|-color)?|fill|stroke|borderColor|border-color|outlineColor|outline-color|boxShadow|box-shadow|accentColor|accent-color|caretColor|caret-color|textDecorationColor)\s*[=:]/i;
  48. const BORDER = /\b(border(?:Top|Right|Bottom|Left|Block|Inline)?(?:Start|End)?(?:Color|Width|Style)?|border(?:-(?:top|right|bottom|left|block|inline))?(?:-(?:start|end))?(?:-(?:color|width|style))?)\s*([=:])\s*(?:'([^']*)'|"([^"]*)"|([^;}\s]+))/g;
  49. const FUNCTIONAL = /details|summary|drop-zone|dropZone|\.drop|:focus|focus-visible|aria-invalid|\/\/\s*functional/i;
  50. const NO_BORDER = /^\s*(0|0px|none|0 none|transparent|inherit|initial|unset)\s*$/i;
  51. // strip comments (whole-line `//`, trailing ` // …`, /* … */ on one line) and allowed var(--x, fallback) fallbacks
  52. function code(line) {
  53. let s = line.replace(/\/\*.*?\*\//g, '');
  54. if (/^\s*\/\//.test(s)) return '';
  55. s = s.replace(/\s\/\/\s.*$/, '');
  56. s = s.replace(/var\(\s*--[\w-]+\s*,\s*(?:[^()]|\([^()]*\))*\)/g, 'var()');
  57. return s;
  58. }
  59. let problems = 0;
  60. const list = files(APP);
  61. for (const f of list) {
  62. const lines = readFileSync(f, 'utf8').split('\n');
  63. lines.forEach((raw, i) => {
  64. const s = code(raw);
  65. if (!s.trim()) return;
  66. const where = `${relative(APP, f)}:${i + 1}`;
  67. const m = s.match(COLOUR);
  68. if (m && !/^\s*import\b/.test(s)) { problems++; console.log(`${where}: hard-coded colour '${m[0]}' — use a theme token — ${raw.trim().slice(0, 140)}`); }
  69. else if (COLOUR_PROP.test(s)) {
  70. const n = s.match(NAMED_RE);
  71. if (n && !/currentColor|transparent/i.test(n[0].slice(-12))) { problems++; console.log(`${where}: named colour '${n[1]}' — use a theme token — ${raw.trim().slice(0, 140)}`); }
  72. }
  73. for (const b of s.matchAll(BORDER)) {
  74. const value = (b[3] ?? b[4] ?? b[5] ?? '').trim();
  75. if (b[1].toLowerCase().includes('radius')) continue;
  76. if (NO_BORDER.test(value)) continue;
  77. if (FUNCTIONAL.test(raw)) continue;
  78. problems++;
  79. console.log(`${where}: decorative border '${b[1]} ${b[2]} ${value || '…'}' — borderless unless it means something (docs/design-rules.md), else mark it '// functional: why' — ${raw.trim().slice(0, 140)}`);
  80. }
  81. });
  82. }
  83. console.log(`check-theme: ${list.length} files, ${problems} problems`);
  84. process.exit(problems ? 1 : 0);

Branches

Latest commits

  • 876b2064docs: workaround note for hybriel#130 (slot marker mid-line)mre
  • 85b56541layouts mission 002 3/3: same-output tests, letcount, shots ports, docs, reportmre
  • 0f325d3blayouts mission 002 2/3: code rules — styles.hl -> components/styles.hl, the one never-reassigned let dropped; gate 73/0, same outputmre
  • a3e74c8alayouts mission 002 1/3: components re-copied byte-verbatim from components.hybriel 3f93d71 by tools/sync-components.sh (+ --check, components/SOURCE); gate 73/0, check-theme 0mre
  • 80ce6ba9report 001 (mission 075)mre
  • 5c6e338flayouts.worldapi.org in Hybriel: WorldAPI theme (tokens, borderless elements, themed components.hybriel components), shells (header, main/user menu, sidebar overlay, content-first, list-first, three-pane), showcase, check-theme helper; gate 73/0 (mission 075)mre