]> git.ipfire.org Git - thirdparty/blocklistproject/lists.git/commitdiff
Update update-number-of-domains.js
authorgap579137 <30596626+gap579137@users.noreply.github.com>
Mon, 19 Aug 2024 03:26:54 +0000 (22:26 -0500)
committerGitHub <noreply@github.com>
Mon, 19 Aug 2024 03:26:54 +0000 (22:26 -0500)
scripts/update-number-of-domains.js

index 5f50f79267c6b31befa4d64c92462918c514297b..439c4fa3579e3be4e94813b8a2f55abe3b2abed8 100644 (file)
@@ -2,19 +2,31 @@ const fs = require("fs").promises;
 const path = require("path");
 
 (async () => {
-       const files = (await fs.readdir(path.join(__dirname, ".."))).filter((file) => file.endsWith(".txt")); // Array of strings, each representing a single file that ends in `.txt`
+       try {
+               const directoryPath = path.join(__dirname, "..");
+               const files = (await fs.readdir(directoryPath)).filter(file => file.endsWith(".txt"));
 
-       await Promise.all(files.map(async (file) => { // For each file
-               const existingDomains = new Set();
+               await Promise.all(files.map(async file => {
+                       const filePath = path.join(directoryPath, file);
+                       const fileContents = await fs.readFile(filePath, "utf8");
 
-               const fileContents = await fs.readFile(path.join(__dirname, "..", file), "utf8"); // Get file contents as a string
+                       // Extract unique domains
+                       const existingDomains = new Set(
+                               fileContents
+                                       .split("\n")
+                                       .filter(line => line.startsWith("0.0.0.0 "))
+                                       .map(line => line.replace("0.0.0.0 ", ""))
+                       );
 
-               fileContents.split("\n").forEach((line) => {
-                       if (line.startsWith("0.0.0.0 ")) {
-                               existingDomains.add(line.replace("0.0.0.0 ", ""));
-                       }
-               });
+                       // Update the total number of network filters
+                       const updatedContents = fileContents.replace(
+                               /^# Total number of network filters: ?(\d*)$/gm,
+                               `# Total number of network filters: ${existingDomains.size}`
+                       );
 
-               await fs.writeFile(path.join(__dirname, "..", file), fileContents.replace(/^# Total number of network filters: ?(\d*)$/gmu, `# Total number of network filters: ${existingDomains.size}`), "utf8");
-       }));
+                       await fs.writeFile(filePath, updatedContents, "utf8");
+               }));
+       } catch (error) {
+               console.error("Error processing files:", error);
+       }
 })();