]> git.ipfire.org Git - thirdparty/blocklistproject/lists.git/commitdiff
Update generate-adguard.js
authorgap579137 <30596626+gap579137@users.noreply.github.com>
Fri, 20 Sep 2024 15:04:44 +0000 (10:04 -0500)
committerGitHub <noreply@github.com>
Fri, 20 Sep 2024 15:04:44 +0000 (10:04 -0500)
scripts/generate-adguard.js

index 6bf7bcec3178246919a7ad5a6d121f9f4562c1a1..3c16ea745934217ae07ed416c07b0ff4a05456d7 100644 (file)
@@ -1,15 +1,35 @@
-const fs = require("fs").promises;
-const path = require("path");
+const fs = require("node:fs").promises;
+const path = require("node: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`
-       await Promise.all(files.map(async (file) => { // For each file
-               const fileContents = await fs.readFile(path.join(__dirname, "..", file), "utf8"); // Get file contents as a string
-               const adGuardFileContents = fileContents
-               .replace(/^# Title: (.*?)$/gmu, "# Title: $1 (adguard)") // Add (adguard) to end of title
-               .replaceAll(/^# 0\.0\.0\.0 (.*?) (.*)/gmu, "@@||$1^! $2")
-               .replaceAll(/0\.0\.0\.0 (.*?)$/gmu, "||$1^")
-               .replaceAll(/^#/gmu, "!");
-               await fs.writeFile(path.join(__dirname, "..", "adguard", file.replace(".txt", "-ags.txt")), adGuardFileContents, "utf8"); // Write new file to `adguard` directory
-       }));
+       try {
+               const files = (await fs.readdir(path.join(__dirname, ".."))).filter(
+                       (file) => file.endsWith(".txt"),
+               );
+               await Promise.all(
+                       files.map(async (file) => {
+                               const fileContents = await fs.readFile(
+                                       path.join(__dirname, "..", file),
+                                       "utf8",
+                               );
+                               const adGuardFileContents = fileContents
+                                       .replace(/^# Title: (.*?)$/gmu, "# Title: $1 (adguard)")
+                                       .replaceAll(/^# 0\.0\.0\.0 (.*?) (.*)/gmu, "@@||$1^! $2")
+                                       .replaceAll(/0\.0\.0\.0 (.*?)$/gmu, "||$1^")
+                                       .replaceAll(/^#/gmu, "!");
+                               await fs.writeFile(
+                                       path.join(
+                                               __dirname,
+                                               "..",
+                                               "adguard",
+                                               file.replace(".txt", "-ags.txt"),
+                                       ),
+                                       adGuardFileContents,
+                                       "utf8",
+                               );
+                       }),
+               );
+       } catch (error) {
+               console.error("Error processing files:", error);
+       }
 })();