-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcolor.js
135 lines (126 loc) · 3.63 KB
/
color.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
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
const path = require("path");
const fs = require("fs");
const { generateTheme, getLessVars } = require("ant-theme-generator");
const showColorSet = process.env.COLOR === "true";
// 变量文件夹
const varFile = path.join(__dirname, "./src/assets/theme/var.less");
const varJsonPath = path.join(__dirname, "./color.json.js");
const outputFilePath = path.join(__dirname, "./public/color.less");
// antd 默认主题 json 文件
const defaultPath = path.join(__dirname, "./src/assets/theme/default.json");
const darkPath = path.join(__dirname, "./src/assets/theme/dark.json");
// antd 主题 less文件
const antdDefaultPath = "./node_modules/antd/lib/style/themes/default.less";
const antdDarkPath = "./node_modules/antd/lib/style/themes/dark.less";
const antdLightPath = "./node_modules/antd/lib/style/themes/compact.less";
function colorStart() {
try {
var varStr = fs.readFileSync(varFile, "utf-8");
} catch (error) {
throw error;
}
// 读取less变量文件 提取变量 信息
let varStrArr = varStr.split(/\n/);
let scipteReg = /\/\/\s+script/g;
let slice = [];
varStrArr.forEach((i, index) => {
if (scipteReg.test(i)) {
slice.push(index);
}
});
varStrArr = varStrArr.slice(...slice).filter((i) => i[0] === "@");
let colorsReg = /(.*?)\s*:\s*(.*?);\s*\/\/\s*([\u4e00-\u9fa5]*)/g;
let varColors = [];
varStrArr.forEach((item) => {
colorsReg.lastIndex = 0;
let execRes = colorsReg.exec(item);
if (execRes) {
varColors.push({
title: execRes[3],
key: execRes[1],
value: execRes[2],
});
}
});
// 变量配置信息 写入 color.json.js 文件
var tips = "\n// 本文件由脚本自动生成";
let exportStr = "module.exports=" + JSON.stringify(varColors) + tips;
fs.writeFileSync(varJsonPath, exportStr);
const defaultVars = getLessVars(antdDefaultPath);
const darkVars = {
...getLessVars(antdDarkPath),
"@primary-color": defaultVars["@primary-color"],
"@picker-basic-cell-active-with-range-color": "darken(@primary-color, 20%)",
};
const lightVars = {
...getLessVars(antdLightPath),
"@primary-color": defaultVars["@primary-color"],
};
const configVars = reduceMap(varColors, "key", "value");
try {
// 写入json文件
fs.writeFileSync(
defaultPath,
JSON.stringify({
...defaultVars,
...lightVars,
...configVars,
})
);
fs.writeFileSync(
darkPath,
JSON.stringify({
...defaultVars,
...darkVars,
...configVars,
})
);
} catch (error) {
throw error;
}
// 使用 antd-theme-generator
const options = {
antDir: path.join(__dirname, "./node_modules/antd"),
stylesDir: path.join(__dirname, "./src"),
varFile,
themeVariables: Array.from(
new Set([
...Object.keys(darkVars),
...Object.keys(lightVars),
...Object.keys(defaultVars),
...varColors.map((i) => i.key),
])
), //需要动态切换的主题变量
indexFileName: "index.html",
outputFilePath,
};
generateTheme(options)
.then((less) => {
console.log("Theme generated successfully");
})
.catch((error) => {
console.log("Error", error);
});
}
function reduceMap(listObj, key, value) {
return listObj.reduce((a, c) => {
a[c[key]] = c[value];
return a;
}, {});
}
function delFile(path) {
if (typeof path === "string") {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
return;
}
if (Array.isArray(path)) {
path.forEach((item) => delFile(item));
}
}
if (showColorSet) {
colorStart();
} else {
delFile([varJsonPath, outputFilePath, defaultPath, darkPath]);
}