forked from Laboratoria/SAP010-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·77 lines (67 loc) · 2.35 KB
/
cli.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
#! /usr/bin/env node
const chalk = require('chalk');
const { mdLinks, getHTTPStatus } = require('./index');
const path = process.argv[2];
const options = {
validate: process.argv.includes('--validate'),
stats: process.argv.includes('--stats'),
};
function printLinks(linksObject) {
linksObject.forEach((link) => {
if (link.ok === 'Ok') {
console.log(chalk.cyan(`\nhref: ${link.href} `));
console.log(chalk.yellow(`text: ${link.text}`));
console.log(chalk.magenta(`file: ${link.file}`));
console.log(chalk.blue(`status: ${link.status}`));
console.log(chalk.green(`ok: ${link.ok} \n`));
console.log(
'------------------------------------------------------------------------'
);
} else if (link.ok === 'FAIL') {
console.log(chalk.cyan(`\nhref: ${link.href} `));
console.log(chalk.yellow(`text: ${link.text}`));
console.log(chalk.magenta(`file: ${link.file}`));
console.log(chalk.red(`status: ${link.status}`));
console.log(chalk.bgRed(`ok: ${link.ok} \n`));
console.log(
'------------------------------------------------------------------------'
);
} else {
console.log(chalk.cyan(`\nhref: ${link.href} `));
console.log(chalk.yellow(`text: ${link.text}`));
console.log(chalk.magenta(`file: ${link.file} \n`));
console.log(
'------------------------------------------------------------------------'
);
}
});
}
mdLinks(path, options).then((linksObject) => {
if (options.validate && !options.stats) {
getHTTPStatus(linksObject).then((result) => {
printLinks(result.linksObject);
});
} else if (options.stats && !options.validate) {
const uniqueLinks = [...new Set(linksObject.map((link) => link.href))]
.length;
const stats = {
'Unique Links': uniqueLinks,
'Total Links': linksObject.length,
};
console.table(stats);
} else if (options.stats && options.validate) {
getHTTPStatus(linksObject).then((result) => {
const failStatCount = result.brokenCount;
const uniqueLinks = [...new Set(linksObject.map((link) => link.href))]
.length;
const stats = {
'Unique Links': uniqueLinks,
'Total Links': linksObject.length,
'Broken Links': failStatCount,
};
console.table(stats);
});
} else {
printLinks(linksObject);
}
});