-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-css-vars.js
100 lines (86 loc) · 2.8 KB
/
create-css-vars.js
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
const fs = require('fs');
// Read and parse JSON files
const readJsonFile = (filename) => {
try {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
} catch (error) {
console.error(`Error reading ${filename}:`, error);
return {};
}
};
// Flatten nested objects into dot notation
const flattenObject = (obj, prefix = '') => {
return Object.keys(obj).reduce((acc, key) => {
const newPrefix = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
// If the object has a $value property, treat it as a leaf node
if ('$value' in obj[key]) {
acc[newPrefix] = obj[key].$value;
} else {
Object.assign(acc, flattenObject(obj[key], newPrefix));
}
} else {
acc[newPrefix] = obj[key];
}
return acc;
}, {});
};
// Resolve color references like {purple.200}
const resolveColorReferences = (value, tokens, globals) => {
if (typeof value !== 'string') return value;
const referenceRegex = /\{([^}]+)\}/g;
return value.replace(referenceRegex, (match, path) => {
// First check in tokens
let resolvedValue = path.split('.').reduce((obj, key) => obj?.[key], tokens);
// If not found in tokens, check in globals under the core path
if (!resolvedValue) {
const globalPath = `core.${path}`;
resolvedValue = globalPath.split('.').reduce((obj, key) => obj?.[key], globals);
// If we found an object with a $value property, use that
if (resolvedValue && typeof resolvedValue === 'object' && '$value' in resolvedValue) {
resolvedValue = resolvedValue.$value;
}
}
return resolvedValue || match;
});
}
// Generate CSS variables
const generateCssVariables = (theme, tokens, globals) => {
const flatTheme = flattenObject(theme);
const cssVars = Object.entries(flatTheme)
.filter(([key]) => !key.includes('$type'))
.map(([key, value]) => {
const resolvedValue = resolveColorReferences(value, tokens, globals);
return `--${key.replace(/\./g, '-').replace(/-\$value$/, '')}: ${resolvedValue};`;
});
return cssVars.join('\n');
};
// Main process
const main = () => {
// Read all theme files
const dark = readJsonFile('dark.json');
const tokens = readJsonFile('tokens.json');
const globals = readJsonFile('global.json');
// Write CSS content to file
const writeCssFile = (filename, content) => {
try {
fs.writeFileSync(filename, content, 'utf8');
console.log(`Successfully wrote CSS to ${filename}`);
} catch (error) {
console.error(`Error writing to ${filename}:`, error);
process.exit(1);
}
};
// Generate CSS variables for dark theme
const darkCss = [
':root {',
' /* Dark theme variables */',
generateCssVariables(dark, tokens, globals)
.split('\n')
.map(line => ' ' + line)
.join('\n'),
'}'
].join('\n');
writeCssFile('dark.css', darkCss);
};
main();