]> git.ipfire.org Git - thirdparty/blocklistproject/lists.git/commitdiff
Update generate-noip.js
authorgap579137 <30596626+gap579137@users.noreply.github.com>
Tue, 5 Nov 2024 03:31:38 +0000 (21:31 -0600)
committerGitHub <noreply@github.com>
Tue, 5 Nov 2024 03:31:38 +0000 (21:31 -0600)
scripts/generate-noip.js

index 7f4f8c1e77b66442b30f3b2296e7e4e0fd98d873..fe64f91fc8a99e649624dbbe7ade08bd889af62e 100644 (file)
@@ -3,32 +3,45 @@ const path = require("node:path");
 
 (async () => {
        try {
-               const files = (await fs.readdir(path.join(__dirname, ".."))).filter(
-                       (file) => file.endsWith(".txt"),
-               );
+               // Define directories for better readability and maintainability
+               const baseDir = path.join(__dirname, "..");
+               const outputDir = path.join(baseDir, "alt-version");
+
+               // Ensure the output directory exists, creating it if necessary
+               await fs.mkdir(outputDir, { recursive: true });
+
+               // Filter for .txt files in the base directory
+               const files = (await fs.readdir(baseDir)).filter((file) => file.endsWith(".txt"));
+
+               // Process each file concurrently
                await Promise.all(
                        files.map(async (file) => {
-                               const fileContents = await fs.readFile(
-                                       path.join(__dirname, "..", file),
-                                       "utf8",
-                               );
-                               const noIPFileContents = fileContents
-                                       .replaceAll(/^0\.0\.0\.0 /gmu, "")
-                                       .replaceAll(/^# 0\.0\.0\.0 /gmu, "# ")
-                                       .replace(/^# Title: (.*?)$/gmu, "# Title: $1 (NL)");
-                               await fs.writeFile(
-                                       path.join(
-                                               __dirname,
-                                               "..",
-                                               "alt-version",
-                                               file.replace(".txt", "-nl.txt"),
-                                       ),
-                                       noIPFileContents,
-                                       "utf8",
-                               );
-                       }),
+                               try {
+                                       // Read file contents
+                                       const filePath = path.join(baseDir, file);
+                                       const fileContents = await fs.readFile(filePath, "utf8");
+
+                                       // Perform replacements
+                                       const noIPFileContents = fileContents
+                                               .replace(/^0\.0\.0\.0 /gm, "")
+                                               .replace(/^# 0\.0\.0\.0 /gm, "# ")
+                                               .replace(/^# Title: (.*?)$/gm, "# Title: $1 (NL)");
+
+                                       // Define output file path
+                                       const outputFilePath = path.join(outputDir, file.replace(".txt", "-nl.txt"));
+
+                                       // Write modified content to output file
+                                       await fs.writeFile(outputFilePath, noIPFileContents, "utf8");
+
+                                       console.log(`Processed: ${file}`);
+                               } catch (fileError) {
+                                       console.error(`Error processing file "${file}":`, fileError);
+                               }
+                       })
                );
+
+               console.log("All files processed successfully.");
        } catch (error) {
-               console.error("Error processing files:", error);
+               console.error("Error during file processing:", error);
        }
 })();