/** Bake brand values from the exact build-time dependency; consumers need no Git dependency. */ import { readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; import { tokens } from "pm-brand"; import { isMainInvocation } from "./main-invocation.ts"; /** Serialize values, not package source or fonts, into deterministic erasable TypeScript. */ export function brandValuesSource(): string { const shared = Object.fromEntries(Object.entries(tokens.light).filter(([key, value]) => value === tokens.dark[key as keyof typeof tokens.dark])); const palettes = Object.fromEntries(Object.entries(tokens).map(([theme, values]) => [theme, Object.fromEntries(Object.entries(values).filter(([key]) => !Object.hasOwn(shared, key)))])); return "/** Generated by scripts/brand-values.ts from the pinned pm-brand tokens. Do not edit. */\n" + "const shared = " + JSON.stringify(shared, null, 2) + " as const;\n" + "/** Complete light and dark values with common typography and status aliases shared. */\n" + "export const brandValues = {\n" + Object.entries(palettes).map(([theme, values]) => " " + theme + ": { ...shared, ..." + JSON.stringify(values, null, 2) + " }" ).join(",\n") + "\n} as const;\n"; } /** Regenerate the source or fail if a committed value differs from the pinned brand kit. */ export function generateBrandValues(root: string, check: boolean): void { const target = resolve(root, "src/brand-values.ts"); const expected = brandValuesSource(); if (check) { if (readFileSync(target, "utf8") !== expected) throw new Error("Stale brand values; run npm run brand:build"); } else writeFileSync(target, expected); } /** CLI entry point; an explicit root supports real temporary-project parity tests. */ export function main(args: readonly string[]): void { generateBrandValues(args[0] && args[0] !== "--check" ? resolve(args[0]) : resolve(import.meta.dirname, ".."), args.includes("--check")); } [(_args: readonly string[]): void => {}, main][Number(isMainInvocation(process.argv, import.meta.url))](process.argv.slice(2));