-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (68 loc) · 2.19 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require("fs");
const ampValidator = require("amphtml-validator");
const axios = require("axios");
if (process.argv.length < 3) {
console.error("You have to supply a text input file. Eg. input.txt");
process.exit();
}
const filePath = process.argv[2];
console.log("Input file is: ", filePath)
let outputFileStream
if (typeof process.argv[3] !== "undefined"){
outputFileStream = fs.createWriteStream(process.argv[3]);
console.log("Output file is: ", process.argv[3])
}
function getMessage(validationResult, url, exception) {
let message = "\n----------------------------------------------------\n";
if (exception) {
message += `${url} - ${exception.response.status}\n`
message += "----------------------------------------------------\n";
return message;
}
if (validationResult.status === "FAIL") {
message += `${validationResult.status} - ${url}\n`;
validationResult.errors.forEach((err) => {
message += `${err.severity} at line ${err.line}, col ${err.col}\n`;
message += `${err.message}\n`;
});
message += "----------------------------------------------------\n";
} else {
message += `${validationResult.status} - ${url}`;
}
return message;
}
function writeToConsole(message){
console.log(message);
}
function writeToOutput(outputFileStream, message) {
if (outputFileStream !== undefined) {
outputFileStream.write(message, "utf-8");
}
}
async function sleep(millis) {
return new Promise((resolve) => setTimeout(resolve, millis));
}
async function run() {
const allUrls = fs.readFileSync(filePath).toString().split("\n");
const validatorInstance = await ampValidator.getInstance();
for (const url of allUrls) {
if (url === "") {
continue;
}
await sleep(200);
let res;
try {
res = await axios.default.get(url);
} catch (e) {
const message = getMessage(null, url, e);
writeToConsole(message);
writeToOutput(outputFileStream, message);
continue;
}
const validationResult = validatorInstance.validateString(res.data);
const message = getMessage(validationResult, url);
writeToConsole(message);
writeToOutput(outputFileStream, message);
}
}
run();