also looking at this
Promote main to production: signed-out chrome and the subscriber tables
#17
54 files
+4252
-1353
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | * KNOWN LIMIT: a value built by string interpolation rather than written |
| 34 | 34 | * literally cannot be resolved here; it is emitted as-is and flagged. |
| 35 | 35 | */ |
| 36 | import { readFileSync, writeFileSync } from 'node:fs' | |
| 36 | import { writeFileSync } from 'node:fs' | |
| 37 | 37 | import { resolve } from 'node:path' |
| 38 | 38 | import theme from '../config/crosswind' |
| 39 | 39 | |
| @@ -65,7 +65,20 @@ const MUST_DIFFER: [string, string][] = [ | ||
| 65 | 65 | ['line', 'line-strong'], |
| 66 | 66 | ] |
| 67 | 67 | |
| 68 | const source = readFileSync(resolve(ROOT, 'config/crosswind.ts'), 'utf8') | |
| 68 | /* | |
| 69 | * The CSS the engine actually receives, not the TypeScript that produces it. | |
| 70 | * | |
| 71 | * Parsing the config file was the wrong source and failed three different ways: | |
| 72 | * comments were read as selectors, `@media` nesting was lost to a regex, and | |
| 73 | * bughq -- where this was found -- declares its tokens as ARRAY ELEMENTS | |
| 74 | * (`` `--text: ${p.text}`, ``) with no semicolon at all, so a scan keyed on `;` | |
| 75 | * found none of them. `preflights[].getCSS()` is the generated stylesheet, so | |
| 76 | * every app's declarations arrive in one shape no matter how the config spells | |
| 77 | * them, already interpolated. | |
| 78 | */ | |
| 79 | const source = ((theme as { preflights?: { getCSS: () => string }[] }).preflights ?? []) | |
| 80 | .map(preflight => preflight.getCSS()) | |
| 81 | .join('\n') | |
| 69 | 82 | |
| 70 | 83 | /** |
| 71 | 84 | * The custom properties in force for one surface and one colour mode. |
| @@ -136,7 +149,11 @@ function declarations(surface: string, dark: boolean): Map<string, string> { | ||
| 136 | 149 | if (qualifier ? (scoped && !qualifier.test(context)) : scoped) |
| 137 | 150 | continue |
| 138 | 151 | const isDark = /data-theme\s*=\s*["']dark["']|prefers-color-scheme:\s*dark/.test(context) |
| 139 | const isLight = /data-theme\s*=\s*["']light["']/.test(context) | |
| 152 | // `@media (prefers-color-scheme: light)` counts too. Without it, a light | |
| 153 | // media block reads as unqualified and overwrites the dark column -- which | |
| 154 | // is what happened to analyticshq's dark-first marketing and auth surfaces, | |
| 155 | // where the unscoped block holds the DARK values and light is the override. | |
| 156 | const isLight = /data-theme\s*=\s*["']light["']|prefers-color-scheme:\s*light/.test(context) | |
| 140 | 157 | if (dark ? isLight : isDark) |
| 141 | 158 | continue |
| 142 | 159 | out.set(name, value) |
| @@ -144,15 +161,60 @@ function declarations(surface: string, dark: boolean): Map<string, string> { | ||
| 144 | 161 | return out |
| 145 | 162 | } |
| 146 | 163 | |
| 164 | /** | |
| 165 | * Resolve `var()` by matching parentheses, not by regex. | |
| 166 | * | |
| 167 | * A regex has to bound how deep a fallback may nest, and these nest deeper than | |
| 168 | * is comfortable to guess: analyticshq declares | |
| 169 | * `warning: var(--warn, var(--text-2, var(--muted)))`, three levels, because | |
| 170 | * its three layout groups name that colour three different ways. A | |
| 171 | * one-level-nesting pattern resolved the inner chain and left the outer | |
| 172 | * `var(--warn, ...)` sitting in the output. | |
| 173 | */ | |
| 147 | 174 | function resolveValue(value: string, vars: Map<string, string>, depth = 0): string { |
| 148 | 175 | if (depth > 8) |
| 149 | 176 | return value |
| 150 | return value.replace(/var\((--[a-z0-9-]+)(?:,\s*([^()]*(?:\([^()]*\)[^()]*)*))?\)/g, (whole, name, fallback) => { | |
| 151 | const found = vars.get(name) | |
| 152 | if (found) return resolveValue(found, vars, depth + 1) | |
| 153 | if (fallback) return resolveValue(fallback.trim(), vars, depth + 1) | |
| 154 | return whole | |
| 155 | }) | |
| 177 | ||
| 178 | let out = '' | |
| 179 | let i = 0 | |
| 180 | while (i < value.length) { | |
| 181 | const at = value.indexOf('var(', i) | |
| 182 | if (at < 0) { | |
| 183 | out += value.slice(i) | |
| 184 | break | |
| 185 | } | |
| 186 | out += value.slice(i, at) | |
| 187 | ||
| 188 | let depthHere = 0 | |
| 189 | let close = at + 3 | |
| 190 | for (; close < value.length; close++) { | |
| 191 | if (value[close] === '(') depthHere++ | |
| 192 | else if (value[close] === ')') { depthHere--; if (depthHere === 0) break } | |
| 193 | } | |
| 194 | const inner = value.slice(at + 4, close) | |
| 195 | ||
| 196 | // the first comma that is not inside a nested call separates name from fallback | |
| 197 | let comma = -1 | |
| 198 | let nested = 0 | |
| 199 | for (let x = 0; x < inner.length; x++) { | |
| 200 | if (inner[x] === '(') nested++ | |
| 201 | else if (inner[x] === ')') nested-- | |
| 202 | else if (inner[x] === ',' && nested === 0) { comma = x; break } | |
| 203 | } | |
| 204 | const name = (comma < 0 ? inner : inner.slice(0, comma)).trim() | |
| 205 | const fallback = comma < 0 ? '' : inner.slice(comma + 1).trim() | |
| 206 | ||
| 207 | const declared = vars.get(name) | |
| 208 | if (declared !== undefined) | |
| 209 | out += resolveValue(declared, vars, depth + 1) | |
| 210 | else if (fallback) | |
| 211 | out += resolveValue(fallback, vars, depth + 1) | |
| 212 | else | |
| 213 | out += value.slice(at, close + 1) | |
| 214 | ||
| 215 | i = close + 1 | |
| 216 | } | |
| 217 | return out | |
| 156 | 218 | } |
| 157 | 219 | |
| 158 | 220 | /* |
| @@ -177,7 +239,7 @@ for (const surface of Object.keys(SURFACES)) { | ||
| 177 | 239 | for (const role of ROLES) { |
| 178 | 240 | const expression = colors[role] |
| 179 | 241 | if (!expression) { |
| 180 | if (surface === 'base' && mode === 'light') missing.push(role) | |
| 242 | if (surface === Object.keys(SURFACES)[0] && mode === 'light') missing.push(role) | |
| 181 | 243 | continue |
| 182 | 244 | } |
| 183 | 245 | const value = resolveValue(expression, vars) |
| @@ -188,20 +250,41 @@ for (const surface of Object.keys(SURFACES)) { | ||
| 188 | 250 | } |
| 189 | 251 | } |
| 190 | 252 | |
| 253 | /* | |
| 254 | * Whether this app has a dark mode at all. | |
| 255 | * | |
| 256 | * commshq has none -- not one `prefers-color-scheme: dark` or | |
| 257 | * `[data-theme="dark"]` in its stylesheet -- so its two columns are the same | |
| 258 | * palette twice. Recording that is the difference between "this app is | |
| 259 | * light-only" and "the dark context was never found", which look identical in | |
| 260 | * the output and are very different facts. | |
| 261 | */ | |
| 262 | const hasDarkMode = /prefers-color-scheme:\s*dark|data-theme\s*=\s*["']dark["']/.test(source) | |
| 263 | ||
| 264 | /* | |
| 265 | * Read off the first surface. Not `base` by name: analyticshq declares almost | |
| 266 | * nothing unscoped and its surfaces are the three layout groups, so a hardcoded | |
| 267 | * `base` resolves to nothing there. A collapse is a limit of the palette rather | |
| 268 | * than of one surface anyway -- the surfaces differ in accent, not in whether | |
| 269 | * two greys are the same grey. | |
| 270 | */ | |
| 271 | const primary = Object.keys(SURFACES)[0] | |
| 191 | 272 | const lost = MUST_DIFFER |
| 192 | .filter(([a, b]) => manifest.dashboard.light[a] && manifest.dashboard.light[a] === manifest.dashboard.light[b]) | |
| 273 | .filter(([a, b]) => manifest[primary].light[a] && manifest[primary].light[a] === manifest[primary].light[b]) | |
| 193 | 274 | .map(([a, b]) => `${a} = ${b}`) |
| 194 | 275 | |
| 195 | 276 | const out = resolve(ROOT, 'config/tokens.json') |
| 196 | writeFileSync(out, `${JSON.stringify({ roles: manifest, lost }, null, 2)}\n`) | |
| 277 | writeFileSync(out, `${JSON.stringify({ darkMode: hasDarkMode, roles: manifest, lost }, null, 2)}\n`) | |
| 197 | 278 | |
| 198 | console.log(`Resolved ${Object.keys(manifest.base.light).length} of ${ROLES.length} roles, per surface (${Object.keys(SURFACES).join(', ')}), light and dark.`) | |
| 279 | console.log(`Resolved ${Object.keys(manifest[primary].light).length} of ${ROLES.length} roles, per surface (${Object.keys(SURFACES).join(', ')}), light and dark.`) | |
| 199 | 280 | if (missing.length) |
| 200 | 281 | console.log(` no utility generated for: ${missing.join(', ')}`) |
| 201 | 282 | if (unresolved.length) { |
| 202 | 283 | console.log(` not reducible to a literal (${unresolved.length}):`) |
| 203 | 284 | for (const entry of unresolved) console.log(` ${entry}`) |
| 204 | 285 | } |
| 286 | if (!hasDarkMode) | |
| 287 | console.log(' this app declares no dark mode; both columns are the same palette') | |
| 205 | 288 | console.log(lost.length |
| 206 | 289 | ? ` lost distinctions (${lost.length}): ${lost.join(', ')}` |
| 207 | 290 | : ' no lost distinctions') |
| @@ -21,12 +21,30 @@ import { join } from 'node:path' | ||
| 21 | 21 | |
| 22 | 22 | const ROOT = join(import.meta.dir, '../..') |
| 23 | 23 | const manifest = JSON.parse(readFileSync(join(ROOT, 'config/tokens.json'), 'utf8')) as { |
| 24 | darkMode: boolean | |
| 24 | 25 | roles: Record<string, Record<string, Record<string, string>>> |
| 25 | 26 | lost: string[] |
| 26 | 27 | } |
| 27 | 28 | const config = readFileSync(join(ROOT, 'config/crosswind.ts'), 'utf8') |
| 28 | 29 | |
| 29 | 30 | describe('token manifest', () => { |
| 31 | /* | |
| 32 | * Recorded because the alternative reading is indistinguishable in the output: | |
| 33 | * commshq declares no dark mode at all, so its two columns are the same | |
| 34 | * palette twice, which looks exactly like a dark context the resolver failed | |
| 35 | * to find. This app has one. | |
| 36 | */ | |
| 37 | test('this app has a dark mode, and light and dark differ', () => { | |
| 38 | expect(manifest.darkMode).toBe(true) | |
| 39 | for (const surface of Object.keys(manifest.roles)) { | |
| 40 | const light = manifest.roles[surface].light | |
| 41 | const dark = manifest.roles[surface].dark | |
| 42 | const identical = Object.keys(light).filter(role => light[role] === dark[role]).length | |
| 43 | expect(`${surface}: all identical = ${identical === Object.keys(light).length}`) | |
| 44 | .toBe(`${surface}: all identical = false`) | |
| 45 | } | |
| 46 | }) | |
| 47 | ||
| 30 | 48 | test('covers both surfaces in both modes', () => { |
| 31 | 49 | expect(Object.keys(manifest.roles).sort()).toEqual(['base', 'dashboard', 'marketing']) |
| 32 | 50 | for (const surface of Object.keys(manifest.roles)) |