Skip to content

Commit

Permalink
Update update-number-of-domains.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gap579137 authored Aug 19, 2024
1 parent 9a41838 commit 6912057
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions scripts/update-number-of-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();

0 comments on commit 6912057

Please sign in to comment.