From: gap579137 <30596626+gap579137@users.noreply.github.com> Date: Mon, 19 Aug 2024 03:26:54 +0000 (-0500) Subject: Update update-number-of-domains.js X-Git-Tag: aggregated-20250518~40 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=69120579540fbd7133540ae627e8290ae3dee29b;p=thirdparty%2Fblocklistproject%2Flists.git Update update-number-of-domains.js --- diff --git a/scripts/update-number-of-domains.js b/scripts/update-number-of-domains.js index 5f50f792..439c4fa3 100644 --- a/scripts/update-number-of-domains.js +++ b/scripts/update-number-of-domains.js @@ -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); + } })();