| 1 | import { readFileSync } from "node:fs"; |
| 2 | |
| 3 | /** |
| 4 | * The design palette as the site actually resolves it. |
| 5 | * |
| 6 | * `app/tokens.css` is generated from `crates/palette/src/tokens.rs` by |
| 7 | * `scripts/export-design-tokens.py`, and `globals.css` carries the hand-kept |
| 8 | * `--gpui-*` block that mirrors the GPUI client's theme. Site variables state |
| 9 | * which token each uses (`--paper: var(--gpui-paper)`) instead of repeating |
| 10 | * the hex. The contract tests still need the literal color to check parity |
| 11 | * and contrast, so this reads both files and flattens the alias chains |
| 12 | * (`--whale-success` -> `--whale-working-green` -> `#9bd66f`, |
| 13 | * `--paper` -> `--gpui-paper` -> `#f5f0e9`). The Blue Stage light preset's |
| 14 | * `LIGHT_*` consts export as `--light-*` beside them, and the Shoreline |
| 15 | * redesign's dark/light pair exports as `--shoreline-*` / |
| 16 | * `--shoreline-light-*`. |
| 17 | * |
| 18 | * Node-only (`node:fs`): imported by the contract tests, never by a component. |
| 19 | */ |
| 20 | const RAW: Record<string, string> = (() => { |
| 21 | const generated = readFileSync(new URL("../app/tokens.css", import.meta.url), "utf8"); |
| 22 | const globals = readFileSync(new URL("../app/globals.css", import.meta.url), "utf8"); |
| 23 | const raw: Record<string, string> = {}; |
| 24 | for (const match of generated.matchAll(/--((?:whale|light|shoreline-light|shoreline)-[\w-]+):\s*([^;]+);/g)) { |
| 25 | raw[match[1]] = match[2].trim(); |
| 26 | } |
| 27 | for (const match of globals.matchAll(/--(gpui-[\w-]+):\s*([^;]+);/g)) { |
| 28 | raw[match[1]] = match[2].trim(); |
| 29 | } |
| 30 | if (Object.keys(raw).length === 0) { |
| 31 | throw new Error("no palette properties found in tokens.css/globals.css"); |
| 32 | } |
| 33 | return raw; |
| 34 | })(); |
| 35 | |
| 36 | function flatten(name: string, seen = new Set<string>()): string { |
| 37 | const value = RAW[name]; |
| 38 | if (value === undefined) throw new Error(`Unknown design token: --${name}`); |
| 39 | const alias = value.match(/^var\(--([\w-]+)\)$/); |
| 40 | if (!alias) return value; |
| 41 | if (seen.has(name)) throw new Error(`Cyclic design token alias: --${name}`); |
| 42 | return flatten(alias[1], seen.add(name)); |
| 43 | } |
| 44 | |
| 45 | /** Resolve a `var(--whale-*)`, `var(--light-*)`, `var(--shoreline-*)`, or `var(--gpui-*)` reference to its literal value; pass anything else through. */ |
| 46 | export function resolveWhale(value: string): string { |
| 47 | const match = value.match(/^var\(--((?:whale|light|shoreline-light|shoreline|gpui)-[\w-]+)\)$/); |
| 48 | return match ? flatten(match[1]) : value; |
| 49 | } |
| 50 |