-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
lint.js
76 lines (64 loc) · 2.8 KB
/
lint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require("fs").promises;
const path = require("path");
(async () => {
let hasError = false;
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.filter((file) => file !== "everything.txt").map(async (file) => { // For each file
const fileContents = await fs.readFile(path.join(__dirname, "..", file), "utf8"); // Get file contents as a string
const commentedURLs = fileContents.split("\n").map((line) => {
if (line.startsWith("# 0.0.0.0")) {
return line.split(" ")[2].trim();
}
return null;
}).filter((a) => a !== null && !!a);
let isHeaderComplete = false;
fileContents.split("\n").forEach((line, index) => {
if (line.startsWith("0.0.0.0")) {
isHeaderComplete = true;
}
// Ensuring that all lines start with "#" or "0.0.0.0 "
if (line.length > 0 && !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;
}
// Checking to ensure all URLs are lowercase
if (line.startsWith("0.0.0.0 ")) {
const lineNoIP = line.replace("0.0.0.0 ", "");
const url = lineNoIP.split("#")[0].trim();
if (url.toLowerCase() !== url) {
console.error(`Line ${index + 1} in ${file} url ${url} must be all lowercase.`);
hasError = true;
}
}
// Ensuring that all lines that start with `#` are followed by a space
if (line.startsWith("#") && line.length > 1 && line[1] !== " ") {
console.error(`Line ${index + 1} in ${file} should have a space after #.`);
hasError = true;
}
// Ensure that after header is complete that all lines that start with `#` start with `# 0.0.0.0`
if (isHeaderComplete && line.startsWith("#") && !line.startsWith("# 0.0.0.0")) {
console.error(`Line ${index + 1} in ${file} should start with "# 0.0.0.0".`);
hasError = true;
}
// Ensure that the URL doesn't exist in the commentedURLs array
if (line.startsWith("0.0.0.0 ")) {
const lineNoIP = line.replace("0.0.0.0 ", "");
const url = lineNoIP.split("#")[0].trim();
if (commentedURLs.includes(url)) {
console.error(`Line ${index + 1} in ${file} url ${url} is commented out in this file. This suggests an error. Please either remove this line or remove the commented URL.`);
hasError = true;
}
}
// Ensure that the URL doesn't contain whitespace characters