-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
101 lines (91 loc) · 3.33 KB
/
request.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
89
90
91
92
93
94
95
96
97
98
99
100
101
var request = require('request')
var fs = require('fs')
var EventEmitter = require('events');
class MyEmitter extends EventEmitter { }
var myEmitter = new MyEmitter();
var inputs = 0
var outputs = 0
var data = []
myEmitter.on('validateLink', validateLink);
myEmitter.on('validateLinkFull', validateLinkFull);
//myEmitter.emit('validateLink', 'http://go.microsoft.com/fwlink/p/?LinkID=525040');
function validateLink(link, file) {
inputs++
request.head(link, function (error, response) {
outputs++
if (error) {
inputs--
outputs--
console.log('error:', error); // Print the error if one occurred
//data.push({ code: -1, link: link, file: file })
myEmitter.emit("validateLinkFull", link, file)
return
}
if (response && response.statusCode != 200) {
inputs--
outputs--
//data.push({ code: -1, link: link, file: file })
debugger
myEmitter.emit("validateLinkFull", link, file)
return
}
if (response && response.statusCode) {
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
data.push({ code: response.statusCode, link: link, file: file })
}
else {
data.push({ code: -2, link: link, file: file })
}
});
}
function validateLinkFull(link, file) {
inputs++
request(link, function (error, response) {
outputs++
if (error) {
console.log('error:', error); // Print the error if one occurred
data.push({ code: -1, link: link, file: file })
return
}
if (response && response.statusCode) {
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
if (link == "https://marketplace.visualstudio.com/items?itemName=docsmsft.docs-authoring-pack") {
console.log(response.statusCode)
debugger
}
data.push({ code: response.statusCode, link: link, file: file })
}
else {
data.push({ code: -2, link: link, file: file })
}
});
}
function printer() {
if (inputs == outputs) {
console.log("==================================== output is ====================================")
console.log(inputs)
console.log(outputs)
var report = {}
var reportData = 'file,link,code'
for (var i = 0; i < data.length; i++) {
if (report[data[i].code]) {
report[data[i].code] = report[data[i].code] + 1
}
else {
report[data[i].code] = 1
}
if (data[i].code == 404) {
console.log(data[i].file + ' ' + data[i].link + ' ' + data[i].code )
}
reportData = reportData + '\n' + data[i].file + ',' + data[i].link + ',' + data[i].code
}
console.log(report)
fs.writeFile('report.csv', reportData, 'utf8', function (err) {
if (err) throw err;
console.log('The file has been saved!');
});
clearInterval(refreshIntervalId);
}
}
var refreshIntervalId = setInterval(printer, 5000)
module.exports = myEmitter