chill_hugoplate/scripts/removeDarkmode.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-05-29 09:56:54 +00:00
const fs = require("fs");
const path = require("path");
const rootDirs = ["assets/scss", "layouts"];
const configFiles = [
{
filePath: "exampleSite/tailwind.config.js",
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'],
},
2023-07-05 10:25:58 +00:00
{
filePath: "exampleSite/hugo.toml",
2023-05-29 09:56:54 +00:00
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)*',
2023-05-29 10:36:58 +00:00
"@apply?(\\s)*;",
2023-05-29 09:56:54 +00:00
]);
}
});
}
function removeDarkMode(configFile) {
const { filePath, patterns } = configFile;
removeDarkModeFromFiles(filePath, patterns);
}