|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Emit Module - Generate tokens.json and tokens.css |
| 5 | + * |
| 6 | + * Transforms consolidated token data into: |
| 7 | + * 1. tokens.json - Machine-readable source of truth |
| 8 | + * 2. tokens.css - Browser-consumable CSS variables |
| 9 | + * |
| 10 | + * Ensures complete parity between JSON and CSS output. |
| 11 | + */ |
| 12 | + |
| 13 | +import fs from 'fs/promises'; |
| 14 | +import path from 'path'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Emit tokens.json file |
| 18 | + * @param {Object} options |
| 19 | + * @param {Object} options.tokens - Consolidated token object |
| 20 | + * @param {string} options.out - Output file path |
| 21 | + */ |
| 22 | +export async function emitJSON({ tokens, out }) { |
| 23 | + await fs.mkdir(path.dirname(out), { recursive: true }); |
| 24 | + await fs.writeFile(out, JSON.stringify(tokens, null, 2), 'utf8'); |
| 25 | + return out; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Emit tokens.css file with complete coverage |
| 30 | + * @param {Object} options |
| 31 | + * @param {Object} options.tokens - Consolidated token object |
| 32 | + * @param {string} options.out - Output file path |
| 33 | + */ |
| 34 | +export async function emitCSS({ tokens, out }) { |
| 35 | + const lines = []; |
| 36 | + |
| 37 | + lines.push(':root {'); |
| 38 | + |
| 39 | + // Brand colors |
| 40 | + if (tokens.colors?.brand) { |
| 41 | + lines.push(' /* Brand colors (OKLCH with sRGB fallback) */'); |
| 42 | + for (const [key, value] of Object.entries(tokens.colors.brand)) { |
| 43 | + lines.push(` --color-${key}: ${value.oklch};`); |
| 44 | + lines.push(` --color-${key}-fallback: ${value.fallback};`); |
| 45 | + } |
| 46 | + lines.push(''); |
| 47 | + } |
| 48 | + |
| 49 | + // Semantic colors (all 4: success, error, warning, info) |
| 50 | + if (tokens.colors?.semantic) { |
| 51 | + lines.push(' /* Semantic colors (bg/fg/border/icon structure) */'); |
| 52 | + for (const [semantic, states] of Object.entries(tokens.colors.semantic)) { |
| 53 | + for (const [state, value] of Object.entries(states)) { |
| 54 | + lines.push(` --color-${semantic}-${state}: ${value.oklch};`); |
| 55 | + } |
| 56 | + lines.push(''); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Neutral palette |
| 61 | + if (tokens.colors?.neutral) { |
| 62 | + lines.push(' /* Neutral palette */'); |
| 63 | + for (const [shade, value] of Object.entries(tokens.colors.neutral)) { |
| 64 | + lines.push(` --color-neutral-${shade}: ${value.oklch};`); |
| 65 | + } |
| 66 | + lines.push(''); |
| 67 | + } |
| 68 | + |
| 69 | + // Typography - Families |
| 70 | + if (tokens.typography?.families) { |
| 71 | + lines.push(' /* Typography - Families */'); |
| 72 | + for (const [key, value] of Object.entries(tokens.typography.families)) { |
| 73 | + lines.push(` --font-${key}: ${value};`); |
| 74 | + } |
| 75 | + lines.push(''); |
| 76 | + } |
| 77 | + |
| 78 | + // Typography - Sizes |
| 79 | + if (tokens.typography?.sizes) { |
| 80 | + lines.push(' /* Typography - Sizes */'); |
| 81 | + for (const [key, value] of Object.entries(tokens.typography.sizes)) { |
| 82 | + lines.push(` --font-size-${key}: ${value};`); |
| 83 | + } |
| 84 | + lines.push(''); |
| 85 | + } |
| 86 | + |
| 87 | + // Typography - Weights |
| 88 | + if (tokens.typography?.weights) { |
| 89 | + lines.push(' /* Typography - Weights */'); |
| 90 | + for (const [key, value] of Object.entries(tokens.typography.weights)) { |
| 91 | + lines.push(` --font-weight-${key}: ${value};`); |
| 92 | + } |
| 93 | + lines.push(''); |
| 94 | + } |
| 95 | + |
| 96 | + // Typography - Line Heights |
| 97 | + if (tokens.typography?.lineHeights) { |
| 98 | + lines.push(' /* Typography - Line Heights */'); |
| 99 | + for (const [key, value] of Object.entries(tokens.typography.lineHeights)) { |
| 100 | + lines.push(` --line-height-${key}: ${value};`); |
| 101 | + } |
| 102 | + lines.push(''); |
| 103 | + } |
| 104 | + |
| 105 | + // Typography - Letter Spacing |
| 106 | + if (tokens.typography?.letterSpacing) { |
| 107 | + lines.push(' /* Typography - Letter Spacing */'); |
| 108 | + for (const [key, value] of Object.entries(tokens.typography.letterSpacing)) { |
| 109 | + lines.push(` --letter-spacing-${key}: ${value};`); |
| 110 | + } |
| 111 | + lines.push(''); |
| 112 | + } |
| 113 | + |
| 114 | + // Spacing scale |
| 115 | + if (tokens.spacing) { |
| 116 | + lines.push(' /* Spacing scale (4px grid) */'); |
| 117 | + for (const [key, value] of Object.entries(tokens.spacing)) { |
| 118 | + lines.push(` --spacing-${key}: ${value};`); |
| 119 | + } |
| 120 | + lines.push(''); |
| 121 | + } |
| 122 | + |
| 123 | + // Shadows - Light mode |
| 124 | + if (tokens.shadows?.light) { |
| 125 | + lines.push(' /* Shadows - Light mode */'); |
| 126 | + for (const [key, value] of Object.entries(tokens.shadows.light)) { |
| 127 | + const shadowValue = value.value || value; |
| 128 | + lines.push(` --shadow-${key}: ${shadowValue};`); |
| 129 | + } |
| 130 | + lines.push(''); |
| 131 | + } |
| 132 | + |
| 133 | + // Motion - Duration |
| 134 | + if (tokens.motion?.duration) { |
| 135 | + lines.push(' /* Motion - Duration */'); |
| 136 | + for (const [key, value] of Object.entries(tokens.motion.duration)) { |
| 137 | + lines.push(` --motion-duration-${key}: ${value};`); |
| 138 | + } |
| 139 | + lines.push(''); |
| 140 | + } |
| 141 | + |
| 142 | + // Motion - Easing |
| 143 | + if (tokens.motion?.easing) { |
| 144 | + lines.push(' /* Motion - Easing */'); |
| 145 | + for (const [key, value] of Object.entries(tokens.motion.easing)) { |
| 146 | + lines.push(` --motion-easing-${key}: ${value};`); |
| 147 | + } |
| 148 | + lines.push(''); |
| 149 | + } |
| 150 | + |
| 151 | + // Data visualization - Okabe-Ito |
| 152 | + if (tokens.dataViz?.categorical?.['okabe-ito']) { |
| 153 | + lines.push(' /* Data visualization - Okabe-Ito (color-blind-safe) */'); |
| 154 | + for (const [key, value] of Object.entries(tokens.dataViz.categorical['okabe-ito'])) { |
| 155 | + const varName = key.replace(/([A-Z])/g, '-$1').toLowerCase(); |
| 156 | + lines.push(` --dataviz-okabe-ito-${varName}: ${value.oklch};`); |
| 157 | + } |
| 158 | + lines.push(''); |
| 159 | + } |
| 160 | + |
| 161 | + // Data visualization - Sequential scales |
| 162 | + if (tokens.dataViz?.sequential) { |
| 163 | + lines.push(' /* Data visualization - Sequential scales */'); |
| 164 | + for (const [palette, colors] of Object.entries(tokens.dataViz.sequential)) { |
| 165 | + colors.forEach((color, index) => { |
| 166 | + lines.push(` --dataviz-sequential-${palette}-${index + 1}: ${color};`); |
| 167 | + }); |
| 168 | + lines.push(''); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Data visualization - Diverging scales |
| 173 | + if (tokens.dataViz?.diverging) { |
| 174 | + lines.push(' /* Data visualization - Diverging scales */'); |
| 175 | + for (const [palette, colors] of Object.entries(tokens.dataViz.diverging)) { |
| 176 | + colors.forEach((color, index) => { |
| 177 | + lines.push(` --dataviz-diverging-${palette}-${index + 1}: ${color};`); |
| 178 | + }); |
| 179 | + lines.push(''); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + lines.push('}'); |
| 184 | + lines.push(''); |
| 185 | + |
| 186 | + // Dark mode shadows |
| 187 | + if (tokens.shadows?.dark) { |
| 188 | + lines.push('/* Dark mode shadows (4-6x opacity increase) */'); |
| 189 | + lines.push('@media (prefers-color-scheme: dark) {'); |
| 190 | + lines.push(' :root {'); |
| 191 | + for (const [key, value] of Object.entries(tokens.shadows.dark)) { |
| 192 | + const shadowValue = value.value || value; |
| 193 | + lines.push(` --shadow-${key}: ${shadowValue};`); |
| 194 | + } |
| 195 | + lines.push(' }'); |
| 196 | + lines.push('}'); |
| 197 | + lines.push(''); |
| 198 | + } |
| 199 | + |
| 200 | + // Reduced motion (accessibility) |
| 201 | + if (tokens.motion) { |
| 202 | + lines.push('/* Reduced motion (accessibility) */'); |
| 203 | + lines.push('@media (prefers-reduced-motion: reduce) {'); |
| 204 | + lines.push(' :root {'); |
| 205 | + |
| 206 | + if (tokens.motion.duration) { |
| 207 | + for (const key of Object.keys(tokens.motion.duration)) { |
| 208 | + lines.push(` --motion-duration-${key}: 0ms;`); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + if (tokens.motion.easing) { |
| 213 | + for (const key of Object.keys(tokens.motion.easing)) { |
| 214 | + lines.push(` --motion-easing-${key}: linear;`); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + lines.push(' }'); |
| 219 | + lines.push('}'); |
| 220 | + lines.push(''); |
| 221 | + } |
| 222 | + |
| 223 | + // OKLCH fallback for legacy browsers |
| 224 | + if (tokens.colors?.brand) { |
| 225 | + lines.push('/* OKLCH fallback for legacy browsers (~8%) */'); |
| 226 | + lines.push('@supports not (color: oklch(0% 0 0)) {'); |
| 227 | + lines.push(' :root {'); |
| 228 | + for (const key of Object.keys(tokens.colors.brand)) { |
| 229 | + lines.push(` --color-${key}: var(--color-${key}-fallback);`); |
| 230 | + } |
| 231 | + lines.push(' }'); |
| 232 | + lines.push('}'); |
| 233 | + } |
| 234 | + |
| 235 | + const css = lines.join('\n'); |
| 236 | + await fs.mkdir(path.dirname(out), { recursive: true }); |
| 237 | + await fs.writeFile(out, css, 'utf8'); |
| 238 | + return out; |
| 239 | +} |
| 240 | + |
| 241 | +/** |
| 242 | + * Emit both JSON and CSS files |
| 243 | + * @param {Object} options |
| 244 | + * @param {Object} options.tokens - Consolidated token object |
| 245 | + * @param {string} options.jsonOut - Path for tokens.json |
| 246 | + * @param {string} options.cssOut - Path for tokens.css |
| 247 | + */ |
| 248 | +export async function emitAll({ tokens, jsonOut, cssOut }) { |
| 249 | + const [jsonPath, cssPath] = await Promise.all([ |
| 250 | + emitJSON({ tokens, out: jsonOut }), |
| 251 | + emitCSS({ tokens, out: cssOut }) |
| 252 | + ]); |
| 253 | + |
| 254 | + return { jsonPath, cssPath }; |
| 255 | +} |
0 commit comments