代码拉取完成,页面将自动刷新
同步操作将从 zhangshitou/react-ant-admin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const path = require("path");
const fs = require("fs");
const { generateTheme, getLessVars } = require("ant-theme-generator");
const showColorSet = process.env.REACT_APP_COLOR === "1";
// 变量文件夹
const varFile = path.resolve(".", "./theme/var.less");
const varJsonPath = path.resolve(".", "./theme/colorData.json");
const outputFilePath = path.resolve(".", "./public/color.less");
// antd 默认主题 json 文件
const defaultPath = path.resolve(".", "./theme/default.json");
const darkPath = path.resolve(".", "./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],
});
}
});
const defaultVars = getLessVars(antdDefaultPath);
// 变量配置信息 写入 color.json 文件
let exportStr = JSON.stringify(varColors);
fs.writeFileSync(varJsonPath, exportStr);
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.resolve(".", "./node_modules/antd"),
stylesDir: path.resolve(".", "./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]);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。