forked from Laboratoria/DEV004-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·104 lines (95 loc) · 3.89 KB
/
cli.js
File metadata and controls
executable file
·104 lines (95 loc) · 3.89 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
/* eslint-disable max-len */
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
import chalk from 'chalk';
import argv from 'node:process';
import { mdLinks } from './index.js';
// chalk
const ok = chalk.greenBright;
const unique = chalk.yellowBright;
const error = chalk.redBright;
const data = chalk.blueBright;
// argv
const route = process.argv[2];
const option = process.argv[3];
const option2 = process.argv[4];
if (route && option === undefined) {
mdLinks(route)
.then((res) => {
res.forEach((element, i) => {
console.log(' ');
console.log(unique(i += 1));
console.log(data(`href: ${element.href}`));
console.log(data(`text: ${element.text}`));
console.log(data(`file: ${element.file}`));
console.log('----------------------------------------------------------------------------');
});
})
.catch((err) => console.log(error(err)));
} else if (route && option === '--validate' && option2 === undefined) {
mdLinks(route, option)
.then((res) => {
res.forEach((element, i) => {
console.log(' ');
console.log(unique(i += 1));
console.log(data(`href: ${element.href}`));
console.log(data(`text: ${element.text}`));
console.log(data(`file: ${element.file}`));
if (element.ok === 'Ok') {
console.log(ok(`status: ${element.status}`));
console.log(ok(`ok: ${element.ok}`));
} else {
console.log(error(`status: ${element.status}`));
console.log(error(`ok: ${element.ok}`));
}
console.log('----------------------------------------------------------------------------');
});
})
.catch((err) => console.log(error(err)));
} else if (route && option === '--stats' && option2 === undefined) {
mdLinks(route)
.then((res) => {
const totalLinks = res.length;
const hrefArray = res.map((obj) => obj.href);
const uniqueLinks = hrefArray.filter((elem, index) => hrefArray.indexOf(elem) === index).length;
console.log(data(`Total: ${totalLinks}`));
console.log(unique(`Unique: ${uniqueLinks}`));
})
.catch((err) => console.log(error(err)));
} else if (route && option === '--stats' && option2 === '--validate') {
mdLinks(route, option2)
.then((res) => {
const totalLinks = res.length;
const hrefArray = res.map((obj) => obj.href);
const uniqueLinks = hrefArray.filter((elem, index) => hrefArray.indexOf(elem) === index).length;
const brokenLinks = res.filter((obj) => obj.ok === 'Fail').length;
console.log(data(`Total: ${totalLinks}`));
console.log(unique(`Unique: ${uniqueLinks}`));
console.log(error(`Broken: ${brokenLinks}`));
})
.catch((err) => console.log(error(err)));
} else if (route && option === '--validate' && option2 === '--stats') {
mdLinks(route, option)
.then((res) => {
const totalLinks = res.length;
const hrefArray = res.map((obj) => obj.href);
const uniqueLinks = hrefArray.filter((elem, index) => hrefArray.indexOf(elem) === index).length;
const brokenLinks = res.filter((obj) => obj.ok === 'Fail').length;
console.log(data(`Total: ${totalLinks}`));
console.log(unique(`Unique: ${uniqueLinks}`));
console.log(error(`Broken: ${brokenLinks}`));
})
.catch((err) => console.log(error(err)));
} else if (argv.includes('--help')) {
console.log(unique(`
md-links <ruta> [opcion]
- <ruta>: la ruta del archivo que se desea evaluar
- [opcion]: ingresar la opción selecionada
* --validate -> el módulo hace una petición HTTP para averiguar si el
o los links funciona o no.
* --stats -> entrega un texto con estadísticas básicas sobre los links.
* --stats --validate o --validate --stats -> entrega un texto con estadisticas
de los links y cuanto de ellos estan rotos.
`));
}