-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakeThemes.mjs
More file actions
98 lines (67 loc) · 2.67 KB
/
makeThemes.mjs
File metadata and controls
98 lines (67 loc) · 2.67 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
import fs from 'fs';
const inputFilePath = './src/data/tokens.json';
const outputDir = './src/styles/theme/';
const data = JSON.parse(fs.readFileSync(inputFilePath, 'utf8'));
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const lightCss = generateCss(data.props.theme.light, 'light');
const darkCss = generateCss(data.props.theme.dark, 'dark');
const themeCss = generateThemeCss(data.props.theme);
const variablesCss = generateVariablesCss(data.props.global);
const lightOutputPath = `${outputDir}props.light.css`;
const darkOutputPath = `${outputDir}props.dark.css`;
const themeOutputPath = `${outputDir}theme.css`;
const variablesOutputPath = `${outputDir}global.css`;
fs.writeFileSync(lightOutputPath, lightCss);
fs.writeFileSync(darkOutputPath, darkCss);
fs.writeFileSync(themeOutputPath, themeCss);
fs.writeFileSync(variablesOutputPath, variablesCss);
console.log(`Generated CSS files for light and dark themes, theme.css, and variables.css.`);
function generateCss(themeData, suffix) {
let css = `/* Autogenerated from ${inputFilePath} */\n/* ${suffix.charAt(0).toUpperCase() + suffix.slice(1)} Theme */\n\n`;
css += 'html {\n';
Object.keys(themeData).forEach(key => {
const variableName = ` --${key}-${suffix}`;
const value = `: ${themeData[key]};`;
css += `${variableName}${value}\n`;
});
css += '}\n';
return css;
}
function generateThemeCss(themeData) {
let css = `/* Autogenerated from ${inputFilePath} */\n`;
css += `@import "./props.light.css";\n@import "./props.dark.css";\n\n`;
css += `:root,\n:root[data-theme="light"] {\n color-scheme: light;\n`;
Object.keys(themeData.light).forEach(key => {
const variableName = ` --${key}`;
const value = `: var(--${key}-light);`;
css += `${variableName}${value}\n`;
});
css += '}\n\n@media (prefers-color-scheme: dark) {\n :root {\n color-scheme: dark;\n';
Object.keys(themeData.dark).forEach(key => {
const variableName = ` --${key}`;
const value = `: var(--${key}-dark);`;
css += `${variableName}${value}\n`;
});
css += '}\n}\n\n:root[data-theme="dark"] {\n color-scheme: dark;\n';
Object.keys(themeData.dark).forEach(key => {
const variableName = ` --${key}`;
const value = `: var(--${key}-dark);`;
css += `${variableName}${value}\n`;
});
css += '}\n';
return css;
}
function generateVariablesCss(variablesData) {
let css = `/* Autogenerated from ${inputFilePath} */\n`;
// global
css += `* {\n`;
Object.keys(variablesData).forEach(key => {
const variableName = ` --${key}`;
const value = `: ${variablesData[key]};`;
css += `${variableName}${value}\n`;
});
css += '}\n';
return css;
}