forked from Laboratoria/DEV001-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
82 lines (73 loc) · 2.96 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
78
79
80
81
82
#!/usr/bin/env node
// Made our js file executable by the locally installed node program
const mdLinks = require('./index');
const chalk = require('chalk');
const { optStatsValidate, optStats, optValidate } = require('./src/optionsFunc');
const path = process.argv[2]
const argv = process.argv
const red = chalk.red;
const green = chalk.green;
const blue = chalk.blue;
const welcome = chalk.bgBlue;
const yellow = chalk.yellow;
const cli = (path, argv) => {
// Grab provided arguments
const [,, ...args] = process.argv;
const validate = argv.includes("--validate");
const stats = argv.includes("--stats");
if(path == undefined){
console.log(welcome('\n---------------------Welcome to MD-links---------------------' ))
console.log('\nInstructions:')
console.log(blue('1. Submit a valid path right after "md-links".'))
console.log(blue('2. To see links route, url, status message, status number and text, write "--validate" right after your path.'))
console.log(blue('3. To see total links number and unique links number, write "--stats" right after your path.'))
console.log(blue('4. To additionally see broken links number, add "--stats --validate" right after your path.'))
}
if(stats && validate){
mdLinks.mdLinks(path, { validate: true })
.then((results) => {
const statsValidate = optStatsValidate(results)
console.log(blue(`Total: ${statsValidate.total} \nUnique: ${statsValidate.unique} \nBroken: ${statsValidate.broken}`))
})
.catch((error) => {
console.log(error)
})
return
}
if (stats) {
mdLinks.mdLinks(path, { validate: false })
.then((results) => {
const onlyStats = optStats(results)
console.log(blue(`Total: ${onlyStats.total}\nUnique: ${onlyStats.unique}`))
})
.catch((error) => {
console.log(error)
})
return
}
if (validate) {
mdLinks.mdLinks(path, { validate: true })
.then((results) => {
results.forEach(elem => {
console.log(blue(`\nFile: ${elem.file} `) + `\nHref: ${ elem.href} ` + `\nMessage: ${elem.message} ` + `\nStatus: ${elem.status} ` + `\nText: ${elem.text}`)
})
})
.catch((error) => {
console.log(error)
})
return
} else {
mdLinks.mdLinks(path, { validate: false })
.then((results) => {
results.forEach(elem => {
console.log(blue(`\nFile: ${elem.file} `) + `\nHref: ${elem.href} ` + `\nText: ${elem.text.slice(0, 50)}`)
});
})
.catch((error) => {
console.log(error)
})
return
}
}
cli(path, argv)
// mdLinks('C:\\Users\\balry\\OneDrive\\Documentos\\Laboratoria\\Proyecto4-MDLinks\\DEV001-md-links\\exampleFiles')