Skip to content

Commit

Permalink
Update lint.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gap579137 authored Nov 5, 2024
1 parent a6d7635 commit 6a13174
Showing 1 changed file with 14 additions and 74 deletions.
88 changes: 14 additions & 74 deletions scripts/lint.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,29 @@
const fs = require("node:fs").promises;
const path = require("node:path");
const fs = require("fs").promises;
const path = require("path");

(async () => {
const hasError = false;
let hasError = false;

try {
let directoryPath = path.join(__dirname, "..");
let files = (await fs.readdir(directoryPath)).filter(
(file) => file.endsWith(".txt") && file !== "everything.txt",
);
const directoryPath = path.join(__dirname, "..");
const files = (await fs.readdir(directoryPath)).filter(file => file.endsWith(".txt") && file !== "everything.txt");

await Promise.all(
files.map(async (file) => {
let filePath = path.join(directoryPath, file);
let fileContents = await fs.readFile(filePath, "utf8");
let lines = fileContents.split("\n");
let commentedURLs = lines
.filter((line) => line.startsWith("# 0.0.0.0"))
.map((line) => line.split(" ")[2].trim());
await Promise.all(files.map(async file => {
const filePath = path.join(directoryPath, file);
const fileContents = await fs.readFile(filePath, "utf8");

let isHeaderComplete = false;
const lines = fileContents.split("\n");
const commentedURLs = lines
.filter(line => line.startsWith("# 0.0.0.0"))
.map(line => line.split(" ")[2].trim());
let isHeaderComplete = false;

lines.forEach((line, index) => {
// Mark the end of the header section
if (line.startsWith("0.0.0.0")) {
isHeaderComplete = true;
}

// Check if the line contains a Version or Date, which should be avoided
if (line.includes("Version") || line.includes("Date")) {
console.error(`Line ${index + 1} in ${file} must not contain a Version/Date.`);
hasError = true;
}

// Validate that each line starts with "#" or "0.0.0.0 "
if (line.trim() && !line.startsWith("#") && !line.startsWith("0.0.0.0 ")) {
console.error(`Line ${index + 1} in ${file} must start with "#" or "0.0.0.0 ".`);
hasError = true;
}

// Ensure URLs in the file are lowercase
if (line.startsWith("0.0.0.0 ")) {
let url = line.split(" ")[1].split("#")[0].trim();
if (url.toLowerCase() !== url) {
console.error(`Line ${index + 1} in ${file} URL ${url} must be all lowercase.`);
hasError = true;
}
}

// Validate that lines starting with "#" have a space following the "#"
if (line.startsWith("#") && line.length > 1 && line[1] !== " ") {
console.error(`Line ${index + 1} in ${file} should have a space after "#".`);
hasError = true;
}

// Check that lines after the header with "#" start with "# 0.0.0.0" or "# NOTE:"
if (isHeaderComplete && line.startsWith("#") && !line.startsWith("# 0.0.0.0") && !line.startsWith("# NOTE:")) {
console.error(`Line ${index + 1} in ${file} should start with "# 0.0.0.0" or "# NOTE:".`);
hasError = true;
}

// Ensure that no active URL matches a commented-out URL
if (line.startsWith("0.0.0.0 ")) {
let url = line.split(" ")[1].split("#")[0].trim();
if (commentedURLs.includes(url)) {
console.error(`Line ${index + 1} in ${file} URL ${url} is commented out in this file. Please remove the duplicate or uncomment the URL.`);
hasError = true;
}
}

// Ensure URLs do not contain whitespace
if (line.startsWith("0.0.0.0 ")) {
let url = line.split(" ")[1].split("#")[0].trim();
if (/\s/g.test(url)) {
console.error(`Line ${index + 1} in ${file} URL ${url} contains whitespace.`);
hasError = true;
}
}
@@ -78,6 +80,7 @@ const path = require("path");
});
}));


process.exit(hasError ? 1 : 0);
} catch (error) {
console.error("An error occurred during file processing:", error);
process.exit(1);
}
})();

0 comments on commit 6a13174

Please sign in to comment.