build-oc2-v2-overrides.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env bun
  2. import { V2_PRIMITIVES_DEFAULT } from "../src/theme/v2/default-primitives"
  3. import type { DesktopTheme } from "../src/theme/types"
  4. const themePath = import.meta.dir + "/../src/theme/themes/oc-2.json"
  5. const theme = (await Bun.file(themePath).json()) as DesktopTheme
  6. const css = await Bun.file(import.meta.dir + "/../src/v2/styles/theme.css").text()
  7. const light = { ...V2_PRIMITIVES_DEFAULT, ...readTokens("light") }
  8. const dark = { ...V2_PRIMITIVES_DEFAULT, ...readTokens("dark") }
  9. const next: DesktopTheme = {
  10. ...theme,
  11. light: { ...theme.light, v2Overrides: light },
  12. dark: { ...theme.dark, v2Overrides: dark },
  13. }
  14. await Bun.write(themePath, JSON.stringify(next, null, 2) + "\n")
  15. console.log("Updated oc-2.json v2Overrides", Object.keys(light).length, "tokens per mode")
  16. function readTokens(mode: "light" | "dark") {
  17. const selector = mode === "light" ? ":root" : `\\[data-color-scheme="${mode}"\\]`
  18. const block = css.match(new RegExp(`${selector} \\{([\\s\\S]*?)\\n \\}`))?.[1]
  19. if (!block) throw new Error(`Missing ${mode} OC-2 tokens`)
  20. return Object.fromEntries(
  21. [...block.matchAll(/--(v2-[\w-]+):\s*([^;]+);/g)]
  22. // Fonts and the fixed avatar foreground remain global CSS rather than theme overrides.
  23. .filter(([, key]) => key !== "v2-avatar-fg" && key !== "v2-font-family-sans")
  24. .map(([, key, value]) => [key, value!.replace(/\s+/g, " ").trim()]),
  25. )
  26. }