-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetCSSVariablesFromTheme.js
More file actions
95 lines (84 loc) · 2.37 KB
/
Copy pathgetCSSVariablesFromTheme.js
File metadata and controls
95 lines (84 loc) · 2.37 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
import { lightTheme, darkTheme } from "./src/view/UI/theme/index.js";
import fs from "fs";
/**
* Массив CSS переменных.
*/
const lightThemeVariables = [];
const darkThemeVariables = [];
const prefix = "--yy";
/**
* Поля, по которым мы не создаем переменные. Они содержат невалидные значения, н-р функции.
* Переписать с использованием библиотеки https://www.npmjs.com/package/is-css-color
*/
const blackList = [
"-mode",
"-text-primary-channel",
"-text-secondary-channel",
"-action-active-channel",
"-action-selected-channel",
"-get-contrast-text",
"-augment-color",
];
/**
* Обогащает variables из темы MUI.
*/
const getLightThemeVars = (obj, path = "") => {
const keys = Object.keys(obj);
keys.forEach((key) => {
if (typeof obj[key] === "object") {
getLightThemeVars(obj[key], "-" + key);
} else {
const value = path + "-" + key;
const formattedValue = value
.replace(/([a-z])([A-Z])/g, "$1-$2")
.toLocaleLowerCase();
if (!blackList.includes(formattedValue)) {
const variable = prefix + formattedValue + ": " + obj[key] + ";";
lightThemeVariables.push(variable);
}
}
});
};
getLightThemeVars(lightTheme.palette);
const getDarkThemeVars = (obj, path = "") => {
const keys = Object.keys(obj);
keys.forEach((key) => {
if (typeof obj[key] === "object") {
getDarkThemeVars(obj[key], "-" + key);
} else {
const value = path + "-" + key;
const formattedValue = value
.replace(/([a-z])([A-Z])/g, "$1-$2")
.toLocaleLowerCase();
if (!blackList.includes(formattedValue)) {
const variable = prefix + formattedValue + ": " + obj[key] + ";";
darkThemeVariables.push(variable);
}
}
});
};
getDarkThemeVars(darkTheme.palette);
/**
* Обработчик ошибок.
* @param err
*/
function errorCallback(err) {
if (err) {
return console.log(err);
}
}
/**
* Шаблон файла
* @type {string}
*/
const template = `:root {
${lightThemeVariables.join("")}
}
[data-theme='dark'] {
${darkThemeVariables.join("")}
}
`;
/**
* Создает соответствующий файл с CSS переменными.
*/
fs.writeFile("./public/styles/mui-variables.css", template, errorCallback);