forked from tcort/markdown-link-check
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-link-check.ts
executable file
·354 lines (330 loc) · 12.3 KB
/
markdown-link-check.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import chalk from 'chalk'
import fs from 'fs'
import program from 'commander'
import { Options, processInputs, ProcessInputsResults, ProcessInputResults, InputsArgs, Status } from '../'
const statusLabels: { [status: string]: string } = {
alive: chalk.green('✓'),
dead: chalk.red('✖'),
ignored: chalk.gray('/'),
error: chalk.yellow('⚠'),
}
export interface CmdOptions {
config?: string
timeout?: string
fileEncoding?: string
quiet: boolean
verbose: boolean
alive: (number | RegExp)[]
debug: boolean
printSummary: boolean
printCacheStats: boolean
printLongChecks: boolean
longChecksMaxDuration: number
retryOnError: boolean
retryOn429: boolean
}
// tslint:disable:no-console
function run(filenameOrUrls: string[], cmdObj: CmdOptions): void {
const options = getOptions(cmdObj)
overrideOptionswithCmdObj(options, cmdObj)
if (filenameOrUrls) {
const inputsArgs: InputsArgs = {
inputs: filenameOrUrls.map((input) => {
return { filenameOrUrl: input }
}),
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
processInputs(inputsArgs, options, (err: any, processInputsResults?: ProcessInputsResults) => {
printInputsResult(cmdObj, err, processInputsResults)
})
} else {
readFromStdin((stdin) => {
const inputsArgs: InputsArgs = {
inputs: [
{
filenameOrUrl: 'stdin',
markdown: stdin,
},
],
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
processInputs(inputsArgs, options, (err: any, processInputsResults?: ProcessInputsResults) => {
printInputsResult(cmdObj, err, processInputsResults)
})
})
}
}
function readFromStdin(callback: (stdin: string) => void) {
const stream = process.stdin
let stdin = ''
stream
.on('data', function (chunk) {
stdin += chunk.toString()
})
.on('error', function (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((error as any).code === 'ENOENT') {
console.error(chalk.red('\nERROR: File not found! Please provide a valid filename as an argument.'))
} else {
console.error(chalk.red(error))
}
return process.exit(1)
})
.on('end', function () {
callback(stdin)
})
}
function getOptions(cmdObj: CmdOptions): Options {
let options: Options
if (cmdObj.config) {
let configContent
try {
configContent = fs.readFileSync(cmdObj.config)
} catch (err) {
console.error(chalk.red('ERROR: Error reading config file'), '. Error:', err)
process.exit(1)
}
options = JSON.parse(configContent.toString())
if (typeof options !== 'object') {
console.error(chalk.red(`ERROR: Config is not a valid JSON Object: ${options}`))
process.exit(1)
}
} else {
options = {}
}
// set default
if (cmdObj.quiet === undefined) {
cmdObj.quiet = false
}
if (cmdObj.verbose === undefined) {
cmdObj.verbose = false
}
if (cmdObj.debug === undefined) {
cmdObj.debug = false
}
if (cmdObj.printSummary === undefined) {
cmdObj.printSummary = false
}
if (cmdObj.printCacheStats === undefined) {
cmdObj.printCacheStats = false
}
if (cmdObj.printLongChecks === undefined) {
cmdObj.printLongChecks = false
}
if (cmdObj.longChecksMaxDuration === undefined) {
cmdObj.longChecksMaxDuration = 5000 // 5s
}
if (cmdObj.retryOnError === undefined) {
cmdObj.retryOnError = false
}
if (cmdObj.retryOn429 === undefined) {
cmdObj.retryOn429 = false
}
return options
}
function overrideOptionswithCmdObj(options: Options, cmdObj: CmdOptions) {
if (cmdObj.debug) {
options.debug = cmdObj.debug
}
if (cmdObj.retryOn429) {
options.aliveStatusCodes = cmdObj.alive
}
if (cmdObj.retryOn429) {
options.retryOn429 = cmdObj.retryOn429
}
if (cmdObj.retryOnError) {
options.retryOnError = cmdObj.retryOnError
}
if (cmdObj.timeout) {
options.timeout = cmdObj.timeout
}
if (cmdObj.fileEncoding) {
options.fileEncoding = cmdObj.fileEncoding
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function printInputsResult(cmdObj: CmdOptions, err: any, processInputsResults?: ProcessInputsResults): void {
if (err) {
console.error(chalk.red('ERROR: something went wrong!'))
console.error(err)
process.exit(1)
}
if (!processInputsResults) {
console.error(chalk.red('ERROR: No processInputsResults! (should not happen)'))
process.exit(1)
}
const results = processInputsResults.results
if (!results || results.length === 0) {
console.error(chalk.red('ERROR: No input processed! (should not happen)'))
process.exit(1)
}
const inputCount = results.length
for (const result of results) {
if (!result) {
if (!cmdObj.quiet) {
console.log(chalk.yellow('Warning: no detail! (should not happen)'))
}
} else {
printInputResult(cmdObj, result)
}
}
// print summary
if (cmdObj.printSummary) {
console.log()
console.log(chalk.cyan('SUMMARY:'))
console.log(chalk.cyan('--------'))
console.log('Total inputs:', inputCount)
console.log('Total links:', processInputsResults.stats.linksCount)
console.log('- alive :', processInputsResults.stats.aliveLinksCount)
console.log('- ignored :', processInputsResults.stats.ignoredLinksCount)
console.log('- error :', processInputsResults.stats.errorLinksCount)
console.log('- dead :', processInputsResults.stats.deadLinksCount)
}
if (cmdObj.printCacheStats) {
console.log()
console.log(chalk.cyan('CACHE STATISTICS:'))
console.log(chalk.cyan('--------'))
console.log('Cache:', processInputsResults.stats.linksCount)
console.log('- hits :', processInputsResults.cacheStats.cacheHits)
console.log('- miss :', processInputsResults.cacheStats.cacheMiss)
}
if (cmdObj.printLongChecks) {
printLongChecks(cmdObj, results)
}
const isFailed = processInputsResults.stats.errorLinksCount + processInputsResults.stats.deadLinksCount === 0
process.exit(isFailed ? 0 : 1)
}
function printInputResult(cmdObj: CmdOptions, result: ProcessInputResults): void {
console.log()
console.log(chalk.cyan('Input: ' + result.filenameOrUrl))
const linkResults = result.results
let deadLinksCount = 0
let errorLinksCount = 0
if (!linkResults) {
if (!cmdObj.verbose) {
console.log(chalk.yellow('Warning: No hyperlinks found!'))
}
return
}
for (const linkResult of linkResults) {
if (!linkResult) {
if (!cmdObj.quiet) {
console.log(chalk.yellow('Warning: no link detail! (should not happen)'))
}
break
}
if (linkResult.status === Status.ALIVE) {
// ignore
} else if (linkResult.status === Status.IGNORED) {
// ignore
} else if (linkResult.status === Status.ERROR) {
errorLinksCount++
} else if (linkResult.status === Status.DEAD) {
deadLinksCount++
} else {
console.log(chalk.yellow(`Warning: unknowns link status "${linkResult.status}"`))
}
const statusLabel = statusLabels[linkResult.status] || '?'
// prettier-ignore
const isOk = (linkResult.status === Status.ALIVE || linkResult.status === Status.IGNORED)
if (cmdObj.quiet && isOk) {
// Skip alive messages in quiet mode.
break
}
console.log(
`[${statusLabel}] ${linkResult.link}` +
(!isOk || cmdObj.verbose ? ` → Status: ${linkResult.statusCode}` : '') +
(cmdObj.verbose ? ` in ${linkResult.stats.durationInMs} ms` : '') +
(cmdObj.verbose && linkResult.stats.retryCount ? ` after ${linkResult.stats.retryCount} retry)` : '') +
(linkResult.err ? chalk.red(` (Error: ${linkResult.err})`) : '') +
(linkResult.additionalMessages ? chalk.yellow(` (Warning: ${linkResult.additionalMessages})`) : ''),
)
}
const linksCount = linkResults.length
console.log('%s links checked.', linksCount)
if (deadLinksCount) {
console.log(chalk.red('ERROR: %s dead links found!'), deadLinksCount)
}
if (errorLinksCount) {
console.log(chalk.red('ERROR: %s error links found!'), errorLinksCount)
}
}
function printLongChecks(cmdObj: CmdOptions, results: (ProcessInputResults | undefined)[]): void {
console.log()
console.log(chalk.cyan('LONG CHECK:'))
console.log(chalk.cyan('--------'))
for (const result of results) {
if (!result) {
if (!cmdObj.quiet) {
console.log(chalk.yellow('Warning: no detail! (should not happen)'))
}
} else {
printLongChecksInput(cmdObj, result)
}
}
}
function printLongChecksInput(cmdObj: CmdOptions, result: ProcessInputResults): void {
const linkResults = result.results
if (!linkResults) {
if (!cmdObj.verbose) {
console.log(chalk.yellow('Warning: No hyperlinks found!'))
}
return
}
for (const linkResult of linkResults) {
if (!linkResult) {
if (!cmdObj.quiet) {
console.log(chalk.yellow('Warning: no link detail! (should not happen)'))
}
break
}
if (linkResult.stats.durationInMs && linkResult.stats.durationInMs > cmdObj.longChecksMaxDuration) {
console.log(
`- ${linkResult.link}: ` +
// duration
(linkResult.stats.durationInMs > cmdObj.longChecksMaxDuration * 10
? chalk.red(`${linkResult.stats.durationInMs} ms (> 10x)`)
: chalk.yellow(`${linkResult.stats.durationInMs} ms`)) +
// retry count if any
(linkResult.stats.retryCount && linkResult.stats.retryCount > 0
? ` (retry: ${linkResult.stats.retryCount}`
: ''),
)
}
}
}
function commaSeparatedCodesList(value: string) {
return value.split(',').map(function (item) {
return parseInt(item, 10)
})
}
program
// Options specific to command line:
.option('-c, --config [config]', 'apply a config file (JSON), holding e.g. url specific header configuration')
.option('-q, --quiet', 'displays errors only')
.option('-v, --verbose', 'displays detailed error information')
.option('-d, --debug', 'displays debug information')
.option(
'--print-summary',
'print total number of inputs and links process with details about link status (alive, ignored, dead, error)',
)
.option('--print-cache-stats', 'print cache usage (hits and misses).')
.option(
'--print-long-checks',
'print links that took more than given delay to verify. Default delay is 5000, configure it with --long-checks-max-duration option',
)
.option('--long-checks-max-duration <number>', 'configure delay for long check. Default is 5000.')
// Options that override config file:
.option(
'-a, --alive <code>',
'comma separated list of HTTP codes to be considered as alive',
commaSeparatedCodesList,
)
.option('--retry-on-error', 'retry after an error')
.option('--retry-on-429', "retry after the duration indicated in 'retry-after' header when HTTP code is 429")
.option('--timeout <string>', 'timeout in zeit/ms format. (e.g. "2000ms", 20s, 1m). Default is 10s.')
.option('-e, --fileEncoding <string>', '')
.arguments('[filenameOrUrls...]')
.description('[filenameOrUrls...] One or several markdown files or URLs to check. If absent, check stdin.')
.action(run)
.parse(process.argv)