2019-01-11 00:58:25 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
const spinner = require('char-spinner')
|
|
|
|
const {gray, green, yellow, red, bold} = require('colorette')
|
|
|
|
const {SiteChecker} = require('broken-link-checker')
|
2019-01-11 01:01:04 +03:00
|
|
|
|
2019-01-11 00:58:25 +03:00
|
|
|
const yargs = require('yargs')
|
|
|
|
.option('filter-level', {type: Number, alias: 'L', default: 3})
|
2019-01-11 02:53:47 +03:00
|
|
|
.option('verbose', {type: Boolean, alias: 'v', default: false})
|
|
|
|
|
|
|
|
const options = Object.assign(
|
|
|
|
{
|
|
|
|
excludedKeywords: [
|
|
|
|
// exclude dash-feed:// URLs (in meta)
|
|
|
|
'dash-feed',
|
|
|
|
// exclude github org repos, which are (mostly) private
|
|
|
|
'https://github.com/github/*'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
yargs.argv
|
|
|
|
)
|
2019-01-11 00:58:25 +03:00
|
|
|
|
|
|
|
const args = options._
|
|
|
|
|
|
|
|
const pages = []
|
|
|
|
const seen = new Set()
|
|
|
|
|
|
|
|
const VERBOSE = options.v === true
|
|
|
|
const OK = ' ✓ '
|
|
|
|
const NOT_OK = ' ✘ '
|
|
|
|
const TAG_LENGTH = 3
|
|
|
|
|
|
|
|
const URL = args[0]
|
|
|
|
if (URL) {
|
|
|
|
log(green('go!'), bold(URL))
|
|
|
|
} else {
|
|
|
|
log('err', 'you must provide a URL')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:53:47 +03:00
|
|
|
let page = {url: URL, links: []}
|
|
|
|
|
2019-01-11 00:58:25 +03:00
|
|
|
const checker = new SiteChecker(options, {
|
|
|
|
page(error, url) {
|
|
|
|
if (error) {
|
|
|
|
log(red('ERR'), `${url} (${error.code})`)
|
|
|
|
} else if (page) {
|
2019-01-11 02:53:47 +03:00
|
|
|
const {url, response, links = []} = page
|
2019-01-11 00:58:25 +03:00
|
|
|
const num = String(links.length).padEnd(TAG_LENGTH)
|
|
|
|
let message = `${bold(num)} unique links`
|
|
|
|
if (!VERBOSE) message = `${message} on ${yellow(url)}`
|
|
|
|
log(OK, message)
|
|
|
|
if (links.length) {
|
|
|
|
pages.push(page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
html(tree, robots, response, url) {
|
|
|
|
if (VERBOSE) log(yellow('get'), url)
|
2019-01-11 02:53:47 +03:00
|
|
|
page = {url, links: []}
|
2019-01-11 00:58:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
junk(result) {
|
2019-01-11 02:53:47 +03:00
|
|
|
const url = result.url.resolved || result.url.original
|
|
|
|
if (!url || seen.has(url)) {
|
2019-01-11 01:10:00 +03:00
|
|
|
return
|
|
|
|
} else if (VERBOSE) {
|
|
|
|
log(' '.repeat(TAG_LENGTH), gray(`skip ${shorten(url)}`))
|
2019-01-11 02:53:47 +03:00
|
|
|
} else if (result.excluded && url.indexOf(URL) !== 0) {
|
|
|
|
log(yellow('???'), gray(`skip ${url}`))
|
2019-01-11 01:10:00 +03:00
|
|
|
}
|
2019-01-11 00:58:25 +03:00
|
|
|
seen.add(url)
|
|
|
|
},
|
|
|
|
|
|
|
|
link(result) {
|
2019-01-11 02:53:47 +03:00
|
|
|
const url = result.url.resolved || result.url.original
|
2019-01-11 00:58:25 +03:00
|
|
|
if (VERBOSE && !seen.has(url)) log(' + ', gray('link'), shorten(url))
|
|
|
|
if (!seen.has(url)) page.links.push(result)
|
|
|
|
seen.add(url)
|
|
|
|
},
|
|
|
|
|
|
|
|
end() {
|
|
|
|
let brokenTotal = 0
|
|
|
|
for (const page of pages) {
|
|
|
|
const broken = page.links.filter(link => link.broken)
|
|
|
|
brokenTotal += broken.length
|
|
|
|
|
|
|
|
if (!broken.length && !VERBOSE) {
|
|
|
|
continue
|
|
|
|
} else {
|
2019-01-11 02:53:47 +03:00
|
|
|
const num = broken.length ? red(` ${broken.length}`.padEnd(TAG_LENGTH)) : green(' 0').padEnd(TAG_LENGTH)
|
|
|
|
log(bold(num), `broken links on ${bold(page.url)}`)
|
2019-01-11 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const link of page.links) {
|
|
|
|
if (!link.broken && !VERBOSE) continue
|
|
|
|
const tag = link.broken ? red(NOT_OK) : green(OK)
|
|
|
|
const reason = link.broken ? yellow(link.brokenReason) : ''
|
2019-01-11 02:53:47 +03:00
|
|
|
const url = link.url.resolved || link.url.original
|
|
|
|
log(tag, shorten(url), reason)
|
2019-01-11 00:58:25 +03:00
|
|
|
}
|
|
|
|
log('')
|
|
|
|
}
|
2019-01-11 02:53:47 +03:00
|
|
|
|
2019-01-11 00:58:25 +03:00
|
|
|
if (brokenTotal) {
|
|
|
|
log(red(String(brokenTotal).padEnd(TAG_LENGTH)), 'broken links!')
|
|
|
|
process.exitCode = 1
|
|
|
|
} else {
|
2019-01-11 02:53:47 +03:00
|
|
|
log('')
|
2019-01-11 01:09:37 +03:00
|
|
|
log(green(OK), bold('0'.padEnd(TAG_LENGTH)), 'broken links; nice work!')
|
2019-01-11 00:58:25 +03:00
|
|
|
}
|
2019-01-11 02:53:47 +03:00
|
|
|
|
|
|
|
log('')
|
2019-01-11 00:58:25 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
spinner()
|
|
|
|
checker.enqueue(URL)
|
|
|
|
checker.resume()
|
|
|
|
|
|
|
|
function log(tag, ...args) {
|
|
|
|
spinner.clear()
|
|
|
|
console.log(tag ? gray(`[${tag}]`) : '', ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
function shorten(url) {
|
2019-01-11 02:53:47 +03:00
|
|
|
return String(url).indexOf(URL) === 0 ? url.substr(URL.length) || '/' : url
|
2019-01-11 00:58:25 +03:00
|
|
|
}
|