-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.js
More file actions
executable file
·365 lines (293 loc) · 11.2 KB
/
convert.js
File metadata and controls
executable file
·365 lines (293 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { readFileSync, writeFileSync } from 'fs';
// Input and output file paths
const inputFile = process.argv[2] || './export.json';
const outputFile = process.argv[3] || './design-tokens.css';
// Read and parse the JSON file
const jsonData = JSON.parse(readFileSync(inputFile, 'utf8'));
// Store all variables to resolve references later
const variableMap = new Map();
const cssLines = [];
// Add comment header
cssLines.push('/**');
cssLines.push(' * Design Tokens CSS Variables');
cssLines.push(' * Generated on: ' + new Date().toISOString());
cssLines.push(' */');
cssLines.push('');
// Process the design tokens
function processTokens() {
// Extract foundation and typography data
const foundations = jsonData[0]['DIY Foundations'];
const typography = jsonData[1]?.Typography;
// Start with base tokens section (shared tokens that don't change between themes)
cssLines.push(':root {');
cssLines.push(' /* Base design tokens - shared across all themes */');
cssLines.push(' /* These foundational values never change between themes */');
// Add typography tokens - these are typically theme-agnostic
if (typography) {
cssLines.push('');
cssLines.push(' /* Typography tokens - theme-agnostic */');
const mode = typography.modes['Mode 1'];
processTypography(mode);
}
// Add base color tokens
if (foundations && foundations.modes.Light && foundations.modes.Light.global) {
const globalTokens = foundations.modes.Light.global;
processBaseTokens(globalTokens);
}
cssLines.push('}');
// Process Light Theme
if (foundations && foundations.modes.Light) {
cssLines.push('');
cssLines.push('/* Light theme tokens */');
cssLines.push('.light-theme {');
processThemeTokens(foundations.modes.Light, 'light');
cssLines.push('}');
}
// Process Dark Theme
if (foundations && foundations.modes.Dark) {
cssLines.push('');
cssLines.push('/* Dark theme tokens */');
cssLines.push('.dark-theme {');
processThemeTokens(foundations.modes.Dark, 'dark');
cssLines.push('}');
}
// Add utility classes and theme toggle helper
cssLines.push('');
cssLines.push('/* Apply one of these classes to your html or body element */');
cssLines.push('html, body {');
cssLines.push(' /* Default to light theme */');
cssLines.push(' color-scheme: light;');
cssLines.push('}');
cssLines.push('');
cssLines.push('.dark-theme {');
cssLines.push(' color-scheme: dark;');
cssLines.push('}');
cssLines.push('');
cssLines.push('/* Theme-specific display control */');
cssLines.push('.light-theme .dark-only { display: none !important; }');
cssLines.push('.dark-theme .light-only { display: none !important; }');
cssLines.push('');
cssLines.push('/* Media query for system preference - automatically applies theme */');
cssLines.push('@media (prefers-color-scheme: dark) {');
cssLines.push(' html:not(.light-theme), body:not(.light-theme) {');
cssLines.push(' color-scheme: dark;');
cssLines.push(' }');
cssLines.push(' ');
cssLines.push(' html:not(.light-theme), body:not(.light-theme) {');
cssLines.push(' /* Auto-apply dark theme if no theme class is specified */');
cssLines.push(' --auto-theme: "dark";');
cssLines.push(' }');
cssLines.push('}');
}
// Process base tokens that don't change between themes
function processBaseTokens(globalTokens) {
cssLines.push('');
cssLines.push(' /* Base spacing tokens */');
processBaseCategory(globalTokens.spacing, 'spacing');
cssLines.push('');
cssLines.push(' /* Base radius tokens */');
processBaseCategory(globalTokens.radius, 'radius');
cssLines.push('');
cssLines.push(' /* Base container tokens */');
processBaseCategory(globalTokens.container, 'container');
// You can add more shared tokens here if needed
}
// Process a category of base tokens
function processBaseCategory(category, prefix) {
if (!category) return;
for (const key in category) {
// Skip metadata properties
if (key.startsWith('$')) continue;
const token = category[key];
if (token && typeof token === 'object' && !Array.isArray(token)) {
if (token.$value !== undefined) {
// This is a token with a value
const variableName = kebabCase(`${prefix}-${key}`);
const cssValue = getRawValue(token);
cssLines.push(` --${variableName}: ${cssValue};`);
variableMap.set(variableName, cssValue);
}
}
}
}
// Process theme-specific tokens
function processThemeTokens(mode, themeName) {
currentProcessingMode = themeName;
// Add color primitives first
if (mode.global) {
cssLines.push(' /* Color primitives */');
processThemeColors(mode.global);
}
// Process semantic tokens
if (mode.semantic) {
cssLines.push('');
cssLines.push(' /* Semantic tokens */');
for (const category in mode.semantic) {
if (category.startsWith('$')) continue;
const categoryTokens = mode.semantic[category];
cssLines.push('');
cssLines.push(` /* ${category} tokens */`);
for (const key in categoryTokens) {
if (key.startsWith('$')) continue;
const token = categoryTokens[key];
if (token && token.$value !== undefined) {
const tokenName = `--${kebabCase(`${category}-${key}`)}`;
const cssValue = getCssValue(token);
cssLines.push(` ${tokenName}: ${cssValue};`);
}
}
}
}
// Process other theme-specific properties
for (const key in mode) {
if (key !== 'semantic' && key !== 'global' && !key.startsWith('$')) {
const categoryTokens = mode[key];
if (key === 'spacing' || key === 'radius' || key === 'container') {
// Skip the base tokens that are already in :root
continue;
}
cssLines.push('');
cssLines.push(` /* ${key} tokens */`);
for (const tokenKey in categoryTokens) {
if (tokenKey.startsWith('$')) continue;
const token = categoryTokens[tokenKey];
if (token && token.$value !== undefined) {
const tokenName = `--${kebabCase(`${key}-${tokenKey}`)}`;
const cssValue = getCssValue(token);
cssLines.push(` ${tokenName}: ${cssValue};`);
}
}
}
}
}
// Process theme-specific color tokens
function processThemeColors(global) {
if (!global) return;
for (const category in global) {
if (category === 'spacing' || category === 'radius' || category === 'container') {
// Skip the base tokens that are already in :root
continue;
}
if (category.startsWith('$')) continue;
const categoryTokens = global[category];
cssLines.push('');
cssLines.push(` /* ${category} colors */`);
for (const key in categoryTokens) {
if (key.startsWith('$')) continue;
const token = categoryTokens[key];
if (token && token.$value !== undefined) {
const variableName = kebabCase(`${category}-${key}`);
const cssValue = getRawValue(token);
cssLines.push(` --${variableName}: ${cssValue};`);
} else if (token && typeof token === 'object' && !Array.isArray(token)) {
// This is a nested category (like brand.primary)
for (const subKey in token) {
if (subKey.startsWith('$')) continue;
const subToken = token[subKey];
if (subToken && subToken.$value !== undefined) {
const variableName = kebabCase(`${category}-${key}-${subKey}`);
const cssValue = getRawValue(subToken);
cssLines.push(` --${variableName}: ${cssValue};`);
}
}
}
}
}
}
// Process typography tokens
function processTypography(typography) {
for (const categoryKey in typography) {
if (categoryKey.startsWith('$')) continue;
const category = typography[categoryKey];
cssLines.push('');
cssLines.push(` /* Typography - ${categoryKey} */`);
for (const propertyKey in category) {
if (propertyKey.startsWith('$')) continue;
const property = category[propertyKey];
if (property && property.$value !== undefined) {
const variableName = kebabCase(`typography-${categoryKey}-${propertyKey}`);
const cssValue = getRawValue(property);
// For font family and font style values, remove quotes if needed
let finalValue = cssValue;
if (categoryKey === 'font family' || categoryKey === 'font weight') {
finalValue = cssValue.replace(/['"]/g, '');
}
cssLines.push(` --${variableName}: ${finalValue};`);
variableMap.set(variableName, finalValue);
}
}
}
}
// Get the raw value of a token (without variable references)
function getRawValue(token) {
if (!token || token.$value === undefined) return 'inherit';
const value = token.$value;
const type = token.$type;
// Handle different value types
switch (type) {
case 'color':
return value;
case 'float':
return `${value}px`;
case 'string':
return `${value}`;
default:
return value;
}
}
// Keep track of the current mode being processed
let currentProcessingMode = 'light';
function getCurrentMode() {
return currentProcessingMode;
}
// Convert token value to CSS value with variable references
function getCssValue(token) {
if (!token || token.$value === undefined) return 'inherit';
const value = token.$value;
const type = token.$type;
// Handle references to other tokens
if (typeof value === 'string' && value.startsWith('{') && value.endsWith('}')) {
const reference = value.slice(1, -1); // Remove brackets
return resolveReference(reference);
}
// For non-reference values, return the raw value
return getRawValue(token);
}
// Resolve a token reference to a CSS variable
function resolveReference(reference) {
const parts = reference.split('.');
// Handle different types of references
if (parts[0] === 'semantic') {
// References to semantic tokens within the current theme
return `var(--${parts.join('-')})`;
} else if (parts[0] === 'global') {
// References to global tokens within the current theme
if (parts[1] === 'spacing' || parts[1] === 'radius' || parts[1] === 'container') {
// These are in the :root
return `var(--${parts.slice(1).join('-').toLowerCase()})`;
} else {
// Theme-specific colors
return `var(--${parts.slice(1).join('-').toLowerCase()})`;
}
} else if (parts[0] === 'color') {
// Handle color.brand structure
return `var(--${parts.join('-')})`;
} else {
// Other references
return `var(--${parts.join('-').toLowerCase()})`;
}
}
// Convert a string to kebab-case
function kebabCase(str) {
return str
.replace(/\./g, '-') // Replace dots with hyphens
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/([a-z])([A-Z])/g, '$1-$2') // Convert camelCase to kebab-case
.toLowerCase();
}
// Execute the script
processTokens();
// Write the CSS to file
const cssContent = cssLines.join('\n');
writeFileSync(outputFile, cssContent);
console.log(`Design tokens CSS generated successfully in ${outputFile}`);