-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
129 lines (105 loc) · 3.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import minimist from 'minimist';
import ora from 'ora';
import {globby} from 'globby';
import getStdin from 'get-stdin';
import chalk from 'chalk';
import symbols from 'log-symbols';
import reachableUrls from './index.js';
const getSymbol = isReachable => isReachable ? symbols.success : symbols.error;
const formatResult = (object, compact = false) => {
let output = '\n';
for (const file of Object.keys(object)) {
output += ` ${chalk.underline(file)}\n\n`;
const result = object[file];
const keys = Object.keys(result);
const urls = compact ? keys.filter(url => !result[url]) : keys;
for (const url of urls) {
output += ` ${getSymbol(result[url])} ${url}\n`;
}
if (urls.length > 0) {
output += '\n';
}
}
return output;
};
const getExitCode = (object = {}) => {
for (const file of Object.keys(object)) {
const result = object[file];
for (const url of Object.keys(result)) {
if (!result[url]) {
return 1;
}
}
}
return 0;
};
const getResult = async args => {
const spinner = ora('Checking files').start();
const foundFiles = await globby(args, {nodir: true});
const files = foundFiles.map(file => path.resolve(process.cwd(), file));
const texts = await Promise.all(foundFiles.map(file => fs.readFile(file)));
let count = 0;
// eslint-disable-next-line arrow-body-style
const results = await Promise.all(texts.map(text => {
return reachableUrls(text).then(result => {
spinner.text = `Checking files [${++count} of ${files.length}]`;
return result;
});
}));
spinner.stop();
const result = {};
for (const [index, file] of files.entries()) {
result[file] = results[index];
}
return result;
};
process.once('uncaughtException', error => {
console.error(error);
process.exit(1);
});
const argv = minimist(process.argv.slice(2), {
alias: {
c: 'compact',
h: 'help',
s: 'silent',
v: 'version',
},
boolean: [
'compact',
'help',
'silent',
'version',
],
});
(async () => {
if (argv.v || argv.version) {
const json = JSON.parse(await fs.readFile('package.json', 'utf-8'));
console.log(json.version);
} else if (argv.h || argv.help) {
const help = await fs.readFile('usage.txt', 'utf-8');
console.log(help);
} else if (argv.stdin) {
const string = await getStdin();
const object = await reachableUrls(string);
const result = {'': object};
const output = formatResult(result, argv.c || argv.compact);
const exitCode = (argv.s || argv.silent) ? 0 : getExitCode(result);
console.log(output);
process.exit(exitCode);
} else {
try {
const result = await getResult(argv._);
const output = formatResult(result, argv.c || argv.compact);
const exitCode = (argv.s || argv.silent) ? 0 : getExitCode(result);
console.log(output);
process.exit(exitCode);
} catch (error) {
console.error(error);
process.exit(1);
}
}
})();