Files
astroplate/scripts/removeDarkmode.js
T

89 lines
2.6 KiB
JavaScript
Raw Normal View History

2025-11-20 10:09:11 +06:00
import fs from "node:fs";
import path from "node:path";
2024-05-16 10:39:54 +06:00
(function () {
const rootDirs = ["src/pages", "src/hooks", "src/layouts", "src/styles"];
2024-05-16 10:39:54 +06:00
const deleteAssetList = [
"public/images/logo-darkmode.png",
"src/layouts/components/ThemeSwitcher.astro",
];
2024-05-16 10:39:54 +06:00
const configFiles = [
{ filePath: "src/config/theme.json", patterns: ["colors.darkmode"] },
];
2024-05-16 10:39:54 +06:00
const filePaths = [
{
filePath: "src/layouts/partials/Header.astro",
patterns: [
"<ThemeSwitchers*(?:\\s+[^>]+)?\\s*(?:\\/\\>|>([\\s\\S]*?)<\\/ThemeSwitchers*>)",
],
},
];
2024-05-16 10:39:54 +06:00
filePaths.forEach(({ filePath, patterns }) => {
removeDarkModeFromFiles(filePath, patterns);
});
2024-05-16 10:39:54 +06:00
deleteAssetList.forEach(deleteAsset);
function deleteAsset(asset) {
try {
fs.unlinkSync(asset);
console.log(`${path.basename(asset)} deleted successfully!`);
} catch (error) {
console.error(`${asset} not found`);
}
2024-05-16 10:39:54 +06:00
}
2024-05-16 10:39:54 +06:00
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");
}
2024-05-16 10:39:54 +06:00
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)*',
]);
}
});
}
function removeDarkMode(configFile) {
const { filePath, patterns } = configFile;
2025-02-17 10:52:46 +06:00
const contentFile = JSON.parse(fs.readFileSync(filePath, "utf8"));
patterns.forEach((pattern) => deleteNestedProperty(contentFile, pattern));
fs.writeFileSync(filePath, JSON.stringify(contentFile));
2024-05-16 10:39:54 +06:00
}
function deleteNestedProperty(obj, propertyPath) {
const properties = propertyPath.split(".");
let currentObj = obj;
for (let i = 0; i < properties.length - 1; i++) {
const property = properties[i];
if (currentObj.hasOwnProperty(property)) {
currentObj = currentObj[property];
} else {
return; // Property not found, no need to continue
}
}
2024-05-16 10:39:54 +06:00
delete currentObj[properties[properties.length - 1]];
}
2024-05-16 10:39:54 +06:00
})();