|
| 1 | +import fs from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { camelCase, kebabCase } from "case-anything"; |
| 4 | +import figmaVariables from "./data/variables.json" with { type: "json" }; |
| 5 | + |
| 6 | +const OUTPUT_DIR = "src/theme/colors"; |
| 7 | + |
| 8 | +type FigmaColorValue = { |
| 9 | + value: { _light: string; _dark: string }; |
| 10 | + type: "color"; |
| 11 | +}; |
| 12 | + |
| 13 | +type FigmaColorScale = Record<string, FigmaColorValue>; |
| 14 | + |
| 15 | +type FigmaColorCore = Record<string, FigmaColorScale>; |
| 16 | + |
| 17 | +const colorCore = figmaVariables["color.core"] as FigmaColorCore; |
| 18 | + |
| 19 | +function transformColorScale( |
| 20 | + scale: FigmaColorScale |
| 21 | +): Record<string, { value: { _light: string; _dark: string } }> { |
| 22 | + return Object.fromEntries( |
| 23 | + Object.entries(scale).map(([step, { value }]) => [step, { value }]) |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function formatTokensForOutput(tokens: Record<string, unknown>): string { |
| 28 | + const formatValue = (value: unknown): string => { |
| 29 | + if (typeof value !== "object" || value === null) { |
| 30 | + return JSON.stringify(value); |
| 31 | + } |
| 32 | + |
| 33 | + if (Array.isArray(value)) { |
| 34 | + return `[${value.map((v) => formatValue(v)).join(", ")}]`; |
| 35 | + } |
| 36 | + |
| 37 | + const entries = Object.entries(value); |
| 38 | + const formatted = entries |
| 39 | + .map(([key, val]) => { |
| 40 | + const keyStr = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) |
| 41 | + ? key |
| 42 | + : JSON.stringify(key); |
| 43 | + return `${keyStr}: ${formatValue(val)}`; |
| 44 | + }) |
| 45 | + .join(", "); |
| 46 | + |
| 47 | + return `{ ${formatted} }`; |
| 48 | + }; |
| 49 | + |
| 50 | + return formatValue(tokens); |
| 51 | +} |
| 52 | + |
| 53 | +function writeColorFile(name: string, tokens: Record<string, unknown>): void { |
| 54 | + const fileName = kebabCase(name); |
| 55 | + const varName = camelCase(name); |
| 56 | + const filePath = join(process.cwd(), OUTPUT_DIR, `${fileName}.ts`); |
| 57 | + const formattedTokens = formatTokensForOutput(tokens); |
| 58 | + |
| 59 | + const content = `import { defineSemanticTokens } from "@pandacss/dev"; |
| 60 | +
|
| 61 | +export const ${varName} = defineSemanticTokens.colors(${formattedTokens}); |
| 62 | +`; |
| 63 | + |
| 64 | + fs.writeFileSync(filePath, content, "utf8"); |
| 65 | + console.log(`📄 Created ${fileName}.ts`); |
| 66 | +} |
| 67 | + |
| 68 | +function writeIndexFile(colorNames: string[]): void { |
| 69 | + const filePath = join(process.cwd(), OUTPUT_DIR, "index.ts"); |
| 70 | + |
| 71 | + const imports = colorNames |
| 72 | + .map((name) => `import { ${camelCase(name)} } from "./${kebabCase(name)}";`) |
| 73 | + .join("\n"); |
| 74 | + |
| 75 | + const exports = colorNames.map((name) => camelCase(name)).join(",\n "); |
| 76 | + |
| 77 | + const content = `${imports} |
| 78 | +
|
| 79 | +export const colors = { |
| 80 | + ${exports}, |
| 81 | +}; |
| 82 | +`; |
| 83 | + |
| 84 | + fs.writeFileSync(filePath, content, "utf8"); |
| 85 | + console.log(`📄 Created index.ts`); |
| 86 | +} |
| 87 | + |
| 88 | +function main(): void { |
| 89 | + console.log("🎨 Generating semantic color tokens from Figma export..."); |
| 90 | + |
| 91 | + const outputPath = join(process.cwd(), OUTPUT_DIR); |
| 92 | + fs.rmSync(outputPath, { recursive: true, force: true }); |
| 93 | + fs.mkdirSync(outputPath, { recursive: true }); |
| 94 | + |
| 95 | + const colorNames: string[] = []; |
| 96 | + |
| 97 | + for (const [colorName, scale] of Object.entries(colorCore)) { |
| 98 | + const tokens = transformColorScale(scale); |
| 99 | + writeColorFile(colorName, tokens); |
| 100 | + colorNames.push(colorName); |
| 101 | + } |
| 102 | + |
| 103 | + writeIndexFile(colorNames); |
| 104 | + |
| 105 | + console.log(`✅ Generated ${colorNames.length} color files`); |
| 106 | +} |
| 107 | + |
| 108 | +main(); |
0 commit comments