forked from Laboratoria/DEV007-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (81 loc) · 3.02 KB
/
app.js
File metadata and controls
93 lines (81 loc) · 3.02 KB
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
const { mdLinks, statsWithBroken, getHttpCode } = require("./index.js");
const path = require('path');
const colors = require("colors");
const Table = require('cli-table');
const command = process.argv[2];
const validateOptionIndex = process.argv.indexOf('--validate');
const statsOptionIndex = process.argv.indexOf('--stats');
const hasValidateOption = validateOptionIndex !== -1;
const hasStatsOption = statsOptionIndex !== -1;
colors.setTheme({
ok:'green',
fail: 'red',
info: 'blue',
warn: 'yellow',
});
if (command)
if (!hasValidateOption && !hasStatsOption) {
console.log(colors.fail(`The command is not valid or it was not provided. It must be ${colors.info('--validate')}, ${colors.info('--stats')}, ${colors.info('--validate --stats')} or ${colors.info('--stats --validate')}.`));
} else {
mdLinks(command, { validate: hasValidateOption })
.then(result => {
if (hasValidateOption) {
if(!result.links.length){
console.log(colors.fail('No info found'));
return
} else if (result.type === 'file') {
// Print the table of validated links
console.log('Info found:');
const table = new Table({
head: ['URL'.info, 'Text'.info, 'HTTPcode'.info, 'Status'.info],
colWidths: [30, 30, 10, 10]
});
const linkPromises = result.links.map(async link => {
const linkText = link.text || 'No text';
const { httpCode, statusMessage } = await getHttpCode(link.url);
return [link.url, linkText, httpCode, statusMessage];
});
Promise.all(linkPromises)
.then(linkData => {
for (const data of linkData) {
table.push(data);
}
console.log(table.toString());
})
.catch(error => console.error(error));
} else if (result.type === 'directory') {
// Print the table of validated links
console.log('Info found:');
const table = new Table({
head: ['URL'.info, 'Text'.info, 'HTTPcode'.info, 'Status'.info],
colWidths: [30, 30, 10, 10]
});
const linkPromises = result.links.map(async link => {
const linkText = link.text || 'No text';
const linkUrl = link.url || 'No links';
const { httpCode, statusMessage } = await getHttpCode(link.url);
return [
linkUrl,
linkText,
httpCode,
statusMessage
];
});
Promise.all(linkPromises)
.then(linkData => {
for (const data of linkData) {
table.push(data);
}
console.log(table.toString());
})
.catch(error => console.error(error));
}
}
if (hasStatsOption) {
statsWithBroken(result.links)
.then(stats => console.log(stats))
.catch(error => console.error(error));
}
})
.catch(error => console.error(error));
}