forked from Laboratoria/bog001-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·65 lines (52 loc) · 2.06 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
#!/usr/bin/env node
// that line (↑)is an instance of a shebang line and describe node like a
// interpreter to pass the file for execution
const chalk = require('chalk');
const argv = require('minimist')(process.argv.slice(2));
const statsLinks = require('./lib/stats.js');
const mdLinks = require('./index.js');
const path = argv._[0];
if (!argv.validate) {
mdLinks(path)
.then((allLinks) => {
if (argv.stats) {
const linkStats = statsLinks(allLinks);
const total = chalk.bold.yellow(`Total:${linkStats[0]}`);
const unique = chalk.bold.cyan(`Unique:${linkStats[1]}`);
console.log(`\n${total}\n${unique}\n`);
} else {
const strLinks = allLinks.map((link) => {
const pathLink = chalk.blueBright(link.path);
const text = chalk.cyan(link.text.substring(0, 49));
return `${pathLink} ${link.href} ${text}
`;
});
console.log(`\n${strLinks.join('\n')}\n`);
}
}).catch((e) => console.log(chalk.bgRedBright(e)));
}
if (argv.validate) {
mdLinks(path, { validate: true })
.then((allLinks) => {
if (argv.stats) {
const linkStats = statsLinks(allLinks);
const total = chalk.bold.yellow(`Total:${linkStats[0]}`);
const unique = chalk.bold.cyan(`Unique:${linkStats[1]}`);
const broken = chalk.bold.red(`Broken:${linkStats[2]}`);
console.log(`\n${total}\n${unique}\n${broken}\n`);
} else {
const strLinks = allLinks.map((link) => {
const pathLink = chalk.blueBright(link.path);
const text = chalk.cyan(link.text.substring(0, 49));
const status = (link.response === 'OK')
? chalk.bgGreenBright(link.statusCode)
: chalk.bgRedBright(link.statusCode);
const response = (link.response === 'OK')
? chalk.green(link.response) : chalk.red(link.response);
return `${pathLink} ${link.href} ${response} ${status} ${text}
`;
});
console.log(`\n${strLinks.join('\n')}\n`);
}
}).catch((e) => console.log(chalk.bgRedBright(e)));
}