ReviewOS

also looking at this

loghqorg/loghq

Promote main to production: signed-out chrome and the subscriber tables

#17
Merged glennmichael123 wants to merge main into production
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.

config/tokens.jsonmodified+1-0
Changes to config/tokens.json
@@ -1,4 +1,5 @@
11{
2 "darkMode": true,
23 "roles": {
34 "base": {
45 "light": {
scripts/tokens.tsmodified+96-13
Changes to scripts/tokens.ts
@@ -33,7 +33,7 @@
3333 * KNOWN LIMIT: a value built by string interpolation rather than written
3434 * literally cannot be resolved here; it is emitted as-is and flagged.
3535 */
36import { readFileSync, writeFileSync } from 'node:fs'
36import { writeFileSync } from 'node:fs'
3737import { resolve } from 'node:path'
3838import theme from '../config/crosswind'
3939
@@ -65,7 +65,20 @@ const MUST_DIFFER: [string, string][] = [
6565 ['line', 'line-strong'],
6666]
6767
68const 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 */
79const source = ((theme as { preflights?: { getCSS: () => string }[] }).preflights ?? [])
80 .map(preflight => preflight.getCSS())
81 .join('\n')
6982
7083/**
7184 * 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> {
136149 if (qualifier ? (scoped && !qualifier.test(context)) : scoped)
137150 continue
138151 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)
140157 if (dark ? isLight : isDark)
141158 continue
142159 out.set(name, value)
@@ -144,15 +161,60 @@ function declarations(surface: string, dark: boolean): Map<string, string> {
144161 return out
145162}
146163
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 */
147174function resolveValue(value: string, vars: Map<string, string>, depth = 0): string {
148175 if (depth > 8)
149176 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
156218}
157219
158220/*
@@ -177,7 +239,7 @@ for (const surface of Object.keys(SURFACES)) {
177239 for (const role of ROLES) {
178240 const expression = colors[role]
179241 if (!expression) {
180 if (surface === 'base' && mode === 'light') missing.push(role)
242 if (surface === Object.keys(SURFACES)[0] && mode === 'light') missing.push(role)
181243 continue
182244 }
183245 const value = resolveValue(expression, vars)
@@ -188,20 +250,41 @@ for (const surface of Object.keys(SURFACES)) {
188250 }
189251}
190252
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 */
262const 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 */
271const primary = Object.keys(SURFACES)[0]
191272const 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])
193274 .map(([a, b]) => `${a} = ${b}`)
194275
195276const out = resolve(ROOT, 'config/tokens.json')
196writeFileSync(out, `${JSON.stringify({ roles: manifest, lost }, null, 2)}\n`)
277writeFileSync(out, `${JSON.stringify({ darkMode: hasDarkMode, roles: manifest, lost }, null, 2)}\n`)
197278
198console.log(`Resolved ${Object.keys(manifest.base.light).length} of ${ROLES.length} roles, per surface (${Object.keys(SURFACES).join(', ')}), light and dark.`)
279console.log(`Resolved ${Object.keys(manifest[primary].light).length} of ${ROLES.length} roles, per surface (${Object.keys(SURFACES).join(', ')}), light and dark.`)
199280if (missing.length)
200281 console.log(` no utility generated for: ${missing.join(', ')}`)
201282if (unresolved.length) {
202283 console.log(` not reducible to a literal (${unresolved.length}):`)
203284 for (const entry of unresolved) console.log(` ${entry}`)
204285}
286if (!hasDarkMode)
287 console.log(' this app declares no dark mode; both columns are the same palette')
205288console.log(lost.length
206289 ? ` lost distinctions (${lost.length}): ${lost.join(', ')}`
207290 : ' no lost distinctions')
tests/unit/tokens.test.tsmodified+18-0
Changes to tests/unit/tokens.test.ts
@@ -21,12 +21,30 @@ import { join } from 'node:path'
2121
2222const ROOT = join(import.meta.dir, '../..')
2323const manifest = JSON.parse(readFileSync(join(ROOT, 'config/tokens.json'), 'utf8')) as {
24 darkMode: boolean
2425 roles: Record<string, Record<string, Record<string, string>>>
2526 lost: string[]
2627}
2728const config = readFileSync(join(ROOT, 'config/crosswind.ts'), 'utf8')
2829
2930describe('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
3048 test('covers both surfaces in both modes', () => {
3149 expect(Object.keys(manifest.roles).sort()).toEqual(['base', 'dashboard', 'marketing'])
3250 for (const surface of Object.keys(manifest.roles))