removeDarkmode.js
1 const fs = require("fs"); 2 const path = require("path"); 3 4 const rootDirs = ["assets/scss", "layouts"]; 5 const configFiles = [ 6 { 7 filePath: "exampleSite/tailwind.config.js", 8 patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'], 9 }, 10 { 11 filePath: "exampleSite/data/theme.json", 12 patterns: ["colors.darkmode"], 13 }, 14 ]; 15 16 // asset paths 17 const deleteAssetList = [ 18 "exampleSite/assets/images/logo-darkmode.png", 19 "layouts/partials/components/theme-switcher.html", 20 ]; 21 22 const filePaths = [ 23 { 24 filePath: "layouts/partials/essentials/header.html", 25 patterns: [ 26 '{{\\s*partial\\s*"components\\/theme-switcher"\\s*\\([^)]*\\)\\s*}}', 27 ], 28 }, 29 ]; 30 31 filePaths.forEach(({ filePath, patterns }) => 32 removeDarkModeFromFiles(filePath, patterns), 33 ); 34 35 deleteAssetList.forEach((asset) => { 36 try { 37 fs.unlinkSync(asset); 38 console.log(`${path.basename(asset)} deleted successfully!`); 39 } catch (error) { 40 console.error(`${asset} not found`); 41 } 42 }); 43 44 rootDirs.forEach(removeDarkModeFromPages); 45 configFiles.forEach(removeDarkMode); 46 47 function removeDarkModeFromFiles(filePath, regexPatterns) { 48 const fileContent = fs.readFileSync(filePath, "utf8"); 49 let updatedContent = fileContent; 50 regexPatterns.forEach((pattern) => { 51 const regex = new RegExp(pattern, "g"); 52 updatedContent = updatedContent.replace(regex, ""); 53 }); 54 55 fs.writeFileSync(filePath, updatedContent, "utf8"); 56 } 57 58 // like html file 59 function removeDarkModeFromPages(directoryPath) { 60 const files = fs.readdirSync(directoryPath); 61 62 files.forEach((file) => { 63 const filePath = path.join(directoryPath, file); 64 const stats = fs.statSync(filePath); 65 if (stats.isDirectory()) { 66 removeDarkModeFromPages(filePath); 67 } else if (stats.isFile()) { 68 removeDarkModeFromFiles(filePath, [ 69 '(?:(?!["])\\S)*dark:(?:(?![,;"])\\S)*', 70 "@apply?(\\s)*;", 71 ]); 72 } 73 }); 74 } 75 76 function removeDarkMode(configFile) { 77 const { filePath, patterns } = configFile; 78 if (filePath === "exampleSite/tailwind.config.js") { 79 removeDarkModeFromFiles(filePath, patterns); 80 } else { 81 const contentFile = JSON.parse(fs.readFileSync(filePath, "utf8")); 82 patterns.forEach((pattern) => deleteNestedProperty(contentFile, pattern)); 83 fs.writeFileSync(filePath, JSON.stringify(contentFile)); 84 } 85 } 86 87 function deleteNestedProperty(obj, propertyPath) { 88 const properties = propertyPath.split("."); 89 let currentObj = obj; 90 for (let i = 0; i < properties.length - 1; i++) { 91 const property = properties[i]; 92 if (currentObj.hasOwnProperty(property)) { 93 currentObj = currentObj[property]; 94 } else { 95 return; // Property not found, no need to continue 96 } 97 } 98 delete currentObj[properties[properties.length - 1]]; 99 }