import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const root = dirname(fileURLToPath(import.meta.url)); const tokens = JSON.parse(readFileSync(join(root, "src", "tokens.json"), "utf8")); function flatten(node, prefix = []) { return Object.entries(node).flatMap(([key, value]) => typeof value === "object" && "value" in value ? [[prefix.concat(key).join("-"), value.value]] : flatten(value, prefix.concat(key)), ); } const lines = flatten(tokens) .sort(([a], [b]) => a.localeCompare(b)) .map(([name, value]) => ` --a-${name}: ${value};`); const css = `/** * Design tokens — generated by build.mjs from src/tokens.json. Do not edit manually. */ :root { ${lines.join("\n")} } `; mkdirSync(join(root, "dist"), { recursive: true }); writeFileSync(join(root, "dist", "tokens.css"), css); console.log(`Emitted ${lines.length} tokens to dist/tokens.css`);