-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli
executable file
·94 lines (84 loc) · 3.79 KB
/
cli
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
#!/usr/bin/env node
process.on('uncaughtException', function(error) {
console.error(error.stack || error)
process.exit(1)
})
process.on('unhandledRejection', function (error) {
console.error(error.stack || error)
process.exit(1)
})
const argv = require('yargs')
const linkChecker = require('./link-checker')
const debug = require('debug')('linkchecker')
const options = argv.usage('Usage: $0 path [options]')
.example('$0 path/to/html/files', 'checks directory with HTMLfiles for broken links and anchors')
.boolean('allow-hash-href')
.describe('allow-hash-href', 'If `true`, ignores the `href` `#`')
.boolean('disable-external')
.describe('disable-external', 'disable checks HTTP links')
.boolean('external-only')
.describe('external-only', 'check HTTP links only')
.array('file-ignore')
.describe('file-ignore', 'RegExp to ignore files to scan')
.array('url-ignore')
.describe('url-ignore', 'RegExp to ignore URLs')
.array('url-swap')
.describe('url-swap', 'RegExp for URLs which can be replaced on the fly')
.boolean('limit-scope')
.describe('limit-scope', 'forbid to follow URLs which are out of provided path, like ../somewhere')
.boolean('mkdocs')
.describe('mkdocs', 'transforming URLS from foo/#bar to foo/index.html#bar')
.boolean('javadoc')
.describe('javadoc', 'Enable special URL transforming which allows to check iframe deeplinks for local javadoc and scaladoc')
.array('javadoc-external')
.describe('javadoc-external', 'Domain or base URL to do URL transformation to check iframe deeplinks')
.array('http-status-ignore')
.describe('http-status-ignore', 'pass HTTP status code which will be ignore, by default only 2xx are allowed')
.boolean('json')
.describe('json', 'print errors as JSON')
.default('http-redirects', 0)
.describe('http-redirects', 'Amount of allowed HTTP redirects')
.default('http-timeout', 5000)
.describe('http-timeout', 'HTTP timeout in milliseconds')
.boolean('http-always-get')
.describe('http-always-get', 'Use always HTTP GET requests, by default HEAD is used for pages without any anchors')
.boolean('warn-name-attr')
.describe('warn-name-attr', 'show warning if name attribute instead of id was used for an anchor')
.string('http-cache')
.describe('http-cache', "Directory to store the non failing HTTP responses. If none is specified responses won't be cached.")
.default('http-cache-max-age', '1w')
.describe('http-cache-max-age', 'Invalidate the cache after the given period. Allowed values: https://www.npmjs.com/package/ms')
.help('h')
.alias('h', 'help')
.argv
debug('CLI options', options)
if (options._.length != 1) {
console.log('You need to pass exactly one path where to check links')
argv.showHelp()
process.exit(1)
}
const mainArgument = options._[0]
linkChecker(mainArgument, options, function(err, result) {
if (err) {
console.error(err)
process.exit(1)
}
const {errors, warnings, parsedFiles, localLinks, localAnchorLinks, parentLinks, parentAnchorLinks, remoteLinks, remoteAnchorLinks} = result.stats
if (options.json) {
console.log(JSON.stringify(result, null, 2))
} else {
result.stats.errors.forEach(error => {
console.error(`${error.reason} from ${error.source} to ${error.target}`)
})
result.stats.warnings.forEach(error => {
console.log(`${error.reason} from ${error.source} to ${error.target}`)
})
console.log('')
console.log(`${parsedFiles} files were scanned in ${mainArgument}`)
console.log(`${localLinks + localAnchorLinks} local links and ${parentLinks + parentAnchorLinks} parent links and ${remoteLinks} remote links were checked`)
console.log(`${errors.length} errors and ${warnings.length} warnings`)
}
if (parsedFiles == 0 || result.stats.errors.length > 0) {
process.exit(1)
}
})