/
cli.ts
220 lines (190 loc) · 5.75 KB
/
cli.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env node
import * as meow from 'meow';
import * as updateNotifier from 'update-notifier';
import chalk = require('chalk');
import {LinkChecker, LinkState, LinkResult, CheckOptions} from './index';
import {promisify} from 'util';
import {Flags, getConfig} from './config';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const toCSV = promisify(require('jsonexport'));
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');
updateNotifier({pkg}).notify();
const cli = meow(
`
Usage
$ linkinator LOCATION [ --arguments ]
Positional arguments
LOCATION
Required. Either the URLs or the paths on disk to check for broken links.
Flags
--concurrency
The number of connections to make simultaneously. Defaults to 100.
--config
Path to the config file to use. Looks for \`linkinator.config.json\` by default.
--format, -f
Return the data in CSV or JSON format.
--help
Show this command.
--markdown
Automatically parse and scan markdown if scanning from a location on disk.
--recurse, -r
Recursively follow links on the same root domain.
--server-root
When scanning a locally directory, customize the location on disk
where the server is started. Defaults to the path passed in [LOCATION].
--silent
Only output broken links
--skip, -s
List of urls in regexy form to not include in the check.
--timeout
Request timeout in ms. Defaults to 0 (no timeout).
Examples
$ linkinator docs/
$ linkinator https://www.google.com
$ linkinator . --recurse
$ linkinator . --skip www.googleapis.com
$ linkinator . --format CSV
`,
{
flags: {
config: {type: 'string'},
concurrency: {type: 'number'},
recurse: {type: 'boolean', alias: 'r'},
skip: {type: 'string', alias: 's'},
format: {type: 'string', alias: 'f'},
silent: {type: 'boolean'},
timeout: {type: 'number'},
markdown: {type: 'boolean'},
serverRoot: {type: 'string'},
},
booleanDefault: undefined,
}
);
let flags: Flags;
async function main() {
if (cli.input.length !== 1) {
cli.showHelp();
return;
}
flags = await getConfig(cli.flags);
const start = Date.now();
if (!flags.silent) {
log(`🏊♂️ crawling ${cli.input}`);
}
const checker = new LinkChecker();
// checker.on('pagestart', url => {
// if (!flags.silent) {
// log(`\n Scanning ${chalk.grey(url)}`);
// }
// });
checker.on('link', (link: LinkResult) => {
if (flags.silent && link.state !== LinkState.BROKEN) {
return;
}
let state = '';
switch (link.state) {
case LinkState.BROKEN:
state = `[${chalk.red(link.status!.toString())}]`;
break;
case LinkState.OK:
state = `[${chalk.green(link.status!.toString())}]`;
break;
case LinkState.SKIPPED:
state = `[${chalk.grey('SKP')}]`;
break;
default:
throw new Error('Invalid state.');
}
log(`${state} ${chalk.gray(link.url)}`);
});
const opts: CheckOptions = {
path: cli.input[0],
recurse: flags.recurse,
timeout: Number(flags.timeout),
markdown: flags.markdown,
concurrency: Number(flags.concurrency),
serverRoot: flags.serverRoot,
};
if (flags.skip) {
if (typeof flags.skip === 'string') {
opts.linksToSkip = flags.skip.split(' ').filter(x => !!x);
} else if (Array.isArray(flags.skip)) {
opts.linksToSkip = flags.skip;
}
}
const result = await checker.check(opts);
log();
const format = flags.format ? flags.format.toLowerCase() : null;
if (format === 'json') {
console.log(JSON.stringify(result, null, 2));
return;
} else if (format === 'csv') {
const csv = await toCSV(result.links);
console.log(csv);
return;
} else {
const parents = result.links.reduce((acc, curr) => {
if (!flags.silent || curr.state === LinkState.BROKEN) {
const parent = curr.parent || '';
if (!acc[parent]) {
acc[parent] = [];
}
acc[parent].push(curr);
}
return acc;
}, {} as {[index: string]: LinkResult[]});
Object.keys(parents).forEach(parent => {
const links = parents[parent];
log(chalk.blue(parent));
links.forEach(link => {
if (flags.silent && link.state !== LinkState.BROKEN) {
return;
}
let state = '';
switch (link.state) {
case LinkState.BROKEN:
state = `[${chalk.red(link.status!.toString())}]`;
break;
case LinkState.OK:
state = `[${chalk.green(link.status!.toString())}]`;
break;
case LinkState.SKIPPED:
state = `[${chalk.grey('SKP')}]`;
break;
default:
throw new Error('Invalid state.');
}
log(` ${state} ${chalk.gray(link.url)}`);
});
});
}
const total = (Date.now() - start) / 1000;
if (!result.passed) {
const borked = result.links.filter(x => x.state === LinkState.BROKEN);
console.error(
chalk.bold(
`${chalk.red('ERROR')}: Detected ${
borked.length
} broken links. Scanned ${chalk.yellow(
result.links.length.toString()
)} links in ${chalk.cyan(total.toString())} seconds.`
)
);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
log(
chalk.bold(
`🤖 Successfully scanned ${chalk.green(
result.links.length.toString()
)} links in ${chalk.cyan(total.toString())} seconds.`
)
);
}
function log(message = '\n') {
if (!flags.format) {
console.log(message);
}
}
main();