chill_hugoplate/scripts/removeDarkmode.js
2023-07-05 16:25:58 +06:00

58 lines
1.6 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const rootDirs = ["assets/scss", "layouts"];
const configFiles = [
{
filePath: "tailwind.config.js",
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'],
},
{
filePath: "exampleSite/tailwind.config.js",
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'],
},
{
filePath: "hugo.toml",
patterns: ["\\S*\\.darkmode[^\\]]*\\]\\n*([\\s\\S]*?)(?=\\[|$)"],
},
{
filePath: "exampleSite/hugo.toml",
patterns: ["\\S*\\.darkmode[^\\]]*\\]\\n*([\\s\\S]*?)(?=\\[|$)"],
},
];
rootDirs.forEach(removeDarkModeFromPages);
configFiles.forEach(removeDarkMode);
function removeDarkModeFromFiles(filePath, regexPatterns) {
const fileContent = fs.readFileSync(filePath, "utf8");
let updatedContent = fileContent;
regexPatterns.forEach((pattern) => {
const regex = new RegExp(pattern, "g");
updatedContent = updatedContent.replace(regex, "");
});
fs.writeFileSync(filePath, updatedContent, "utf8");
}
function removeDarkModeFromPages(directoryPath) {
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
removeDarkModeFromPages(filePath);
} else if (stats.isFile()) {
removeDarkModeFromFiles(filePath, [
'(?:(?!["])\\S)*dark:(?:(?![,;"])\\S)*',
"@apply?(\\s)*;",
]);
}
});
}
function removeDarkMode(configFile) {
const { filePath, patterns } = configFile;
removeDarkModeFromFiles(filePath, patterns);
}