Something went wrong. Try again.
Tools for building Offline-first Web Apps
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203#!/usr/bin/env -S deno run --allow-read/** * @module Cascade-layer contract check * * The 0.12 layer order is * `normalize, base, components, app, base-theme, theme` — **rules below `app`, * tokens above it**. That split is the only reason a drop-in theme can work: * an app can't redefine `:root` tokens from its structural layer and quietly * out-rank the designer, and a designer can restate a colour without fighting * the app's grid. * * A boundary nobody checks stops being a boundary. The previous one was * documented in the spec as "an app overrides a handful of variables in its * own theme.css and rarely writes component CSS" — by the time anyone measured, * the six app themes held 10,049 lines of which 8.5% were tokens. * * Three rules, each cheap to state and cheap to check: * * 1. `base-theme` contains **only** custom-property declarations. Hoisting it * above `app` is safe precisely because it holds no rules that could * out-rank an app's — the day it holds one, that stops being true. * 2. An app `theme.css` sets **no layout properties**. Colour, type, radius, * shadow, border are skin; `display`, `grid-*`, `width`, `margin` are * structure and belong in `app.css`. * 3. Unlayered CSS in an app stays **small**. It out-ranks every layer, so it * is a real escape hatch — but "unlayered because I meant it" and * "unlayered because nobody migrated this file" look identical, and the * second is how the old situation arose. * * Run via `deno task test:layer-contract`. */import { fromFileUrl } from '@std/path'
const ROOT = fromFileUrl(new URL('../', import.meta.url))const APPS_ROOT = fromFileUrl(new URL('../../', import.meta.url))
/** * Framework themes — all must stay declaration-only. * * `default.css` because it *is* `base-theme`, which sits above `app`: hoisting * it there is only safe while it holds nothing that could out-rank an app * rule. The alternates because a theme that needs a rule is a theme reporting * a missing token — that is what `eink.css` is for. */async function frameworkThemes(): Promise<string[]> { const dir = 'packages/ui/themes' const out: string[] = [] for await (const entry of Deno.readDir(dir)) { if (entry.isFile && entry.name.endsWith('.css')) { out.push(`${dir}/${entry.name}`) } } return out.sort()}
/** App `theme.css` files, relative to `apps/`. */const APP_THEMES = [ 'maps/www/static/styles/theme.css', 'music/www/static/styles/theme.css', 'notes/www/static/theme.css', 'photos/www/static/theme.css',]
/** Structure, not skin. A theme that sets these is competing with `app.css`. */const LAYOUT_PROPERTIES = [ 'display', 'position', 'float', 'grid', 'grid-template', 'grid-template-columns', 'grid-template-rows', 'grid-area', 'grid-column', 'grid-row', 'flex', 'flex-direction', 'flex-wrap', 'flex-basis', 'align-items', 'justify-content', 'gap', 'width', 'height', 'min-width', 'min-height', 'max-width', 'max-height', 'margin', 'padding', 'inset', 'top', 'right', 'bottom', 'left',]
/** * Ceiling on unlayered rules per app. Deliberately low: past this it is no * longer an escape hatch, it is a second cascade. */const UNLAYERED_CEILING = 15
const problems: string[] = []
function stripComments(css: string): string { return css.replace(/\/\*[\s\S]*?\*\//g, '')}
/** Rule 1 — framework themes hold only custom properties. */async function checkFrameworkTheme(rel: string): Promise<void> { const css = stripComments(await Deno.readTextFile(`${ROOT}${rel}`)) const offenders = new Set<string>() for (const [, body] of css.matchAll(/\{([^{}]*)\}/g)) { for (const [, , prop] of body.matchAll(/(^|;)\s*([a-zA-Z-]+)\s*:/g)) { if (prop && !prop.startsWith('--')) offenders.add(prop) } } if (offenders.size > 0) { problems.push( `${rel} declares non-custom properties: ` + `${[...offenders].sort().join(', ')}\n` + ` \`base-theme\` sits above \`app\`; anything but a token there ` + `out-ranks every app rule.`, ) }}
/** Rules 2 and 3 — app themes are skin, app unlayered CSS is small. */async function checkApp(rel: string): Promise<void> { const app = rel.split('/')[0] let css: string try { css = stripComments(await Deno.readTextFile(`${APPS_ROOT}${rel}`)) } catch { return // app not present in this checkout }
const layout = new Set<string>() for (const [, body] of css.matchAll(/\{([^{}]*)\}/g)) { for (const [, , prop] of body.matchAll(/(^|;)\s*([a-zA-Z-]+)\s*:/g)) { if (LAYOUT_PROPERTIES.includes(prop)) layout.add(prop) } } if (layout.size > 0) { problems.push( `${rel} sets layout properties: ${[...layout].sort().join(', ')}\n` + ` Move those rules to app.css (\`@layer app\`); theme.css is skin.`, ) }
// Unlayered = anything outside a top-level `@layer` block. const appCss = `${APPS_ROOT}${rel.replace(/theme\.css$/, 'app.css')}` let structural: string try { structural = stripComments(await Deno.readTextFile(appCss)) } catch { return } let depth = 0 let unlayered = 0 let inLayer = false for (let i = 0; i < structural.length; i++) { const ch = structural[i] if (ch === '{') { if (depth === 0) { const prelude = structural.slice(0, i) const start = Math.max(prelude.lastIndexOf('}'), 0) inLayer = /@layer/.test(prelude.slice(start)) } if (depth === 1 && !inLayer) unlayered++ if (depth === 0 && !inLayer) unlayered++ depth++ } else if (ch === '}') depth-- } if (unlayered > UNLAYERED_CEILING) { problems.push( `${app}: ${unlayered} unlayered rule(s) in app.css ` + `(ceiling ${UNLAYERED_CEILING})\n` + ` Unlayered CSS out-ranks \`theme\`. If these are deliberate ` + `overrides, say so and raise the ceiling; if they are unmigrated, ` + `wrap them in \`@layer app\`.`, ) }}
// Discovered rather than listed: a theme added to `themes/` and left out of a// hardcoded array would be exempt from the one rule that makes it a theme.const FRAMEWORK_THEMES = await frameworkThemes()for (const rel of FRAMEWORK_THEMES) await checkFrameworkTheme(rel)for (const rel of APP_THEMES) await checkApp(rel)
if (problems.length > 0) { console.error(`Layer contract violated:\n`) for (const p of problems) console.error(` ${p}\n`) Deno.exit(1)}
console.log( `Layer contract holds: ${FRAMEWORK_THEMES.length} framework theme(s) ` + `tokens-only, ${APP_THEMES.length} app theme(s) skin-only.`,)