-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathremoveDarkmode.js
96 lines (84 loc) · 2.82 KB
/
removeDarkmode.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
const fs = require("fs");
const path = require("path");
(function () {
const rootDirs = ["src/pages", "src/hooks", "src/layouts", "src/styles"];
const deleteAssetList = [
"public/images/logo-darkmode.png",
"src/layouts/components/ThemeSwitcher.astro",
];
const configFiles = [
{
filePath: "tailwind.config.js",
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'],
},
{ filePath: "src/config/theme.json", patterns: ["colors.darkmode"] },
];
const filePaths = [
{
filePath: "src/layouts/partials/Header.astro",
patterns: [
"<ThemeSwitchers*(?:\\s+[^>]+)?\\s*(?:\\/\\>|>([\\s\\S]*?)<\\/ThemeSwitchers*>)",
],
},
];
filePaths.forEach(({ filePath, patterns }) => {
removeDarkModeFromFiles(filePath, patterns);
});
deleteAssetList.forEach(deleteAsset);
function deleteAsset(asset) {
try {
fs.unlinkSync(asset);
console.log(`${path.basename(asset)} deleted successfully!`);
} catch (error) {
console.error(`${asset} not found`);
}
}
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)*',
]);
}
});
}
function removeDarkMode(configFile) {
const { filePath, patterns } = configFile;
if (filePath === "tailwind.config.js") {
removeDarkModeFromFiles(filePath, patterns);
} else {
const contentFile = JSON.parse(fs.readFileSync(filePath, "utf8"));
patterns.forEach((pattern) => deleteNestedProperty(contentFile, pattern));
fs.writeFileSync(filePath, JSON.stringify(contentFile));
}
}
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
}
}
delete currentObj[properties[properties.length - 1]];
}
})();