2023-05-06 22:00:50 +00:00
|
|
|
// @ts-check
|
2022-09-13 16:06:15 +00:00
|
|
|
import Crawler from 'crawler'
|
2023-04-08 10:16:06 +00:00
|
|
|
import { consola } from 'consola'
|
2022-09-13 16:06:15 +00:00
|
|
|
import { parseURL, withoutTrailingSlash } from 'ufo'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import * as actions from '@actions/core'
|
|
|
|
import { isCI } from 'std-env'
|
|
|
|
|
|
|
|
const logger = consola.withTag('crawler')
|
|
|
|
|
2023-01-21 23:39:43 +00:00
|
|
|
const baseURL = withoutTrailingSlash(process.env.BASE_URL || 'https://nuxt.com')
|
2022-09-13 16:06:15 +00:00
|
|
|
const startingURL = baseURL + '/'
|
|
|
|
|
2023-03-09 11:34:41 +00:00
|
|
|
const excludedExtensions = ['svg', 'png', 'jpg', 'sketch', 'ico', 'gif', 'zip']
|
2022-09-13 16:06:15 +00:00
|
|
|
const urlsToOmit = ['http://localhost:3000']
|
|
|
|
|
|
|
|
// TODO: remove when migrating to Nuxt 3/Docus
|
|
|
|
const errorsToIgnore = [
|
|
|
|
'/guide/directory-structure/nuxt.config',
|
|
|
|
'/guide/directory-structure',
|
|
|
|
'/guide/directory-structure/app.config',
|
2022-10-07 20:39:38 +00:00
|
|
|
'/api/configuration/nuxt-config',
|
2022-09-13 16:06:15 +00:00
|
|
|
'/guide/deploy',
|
|
|
|
'/guide/features/app-config'
|
|
|
|
]
|
|
|
|
|
|
|
|
// GLOBALS
|
|
|
|
const urls = new Set([startingURL])
|
|
|
|
const erroredUrls = new Set()
|
|
|
|
|
2023-03-09 11:34:41 +00:00
|
|
|
const referrers = new Map()
|
|
|
|
|
2022-09-13 16:06:15 +00:00
|
|
|
/**
|
|
|
|
* @param {string} path Path to check
|
|
|
|
* @param {string | undefined} referrer The referring page
|
|
|
|
*/
|
|
|
|
function queue (path, referrer) {
|
2022-10-03 13:38:06 +00:00
|
|
|
if (!path) {
|
|
|
|
const message = chalk.red(`${chalk.bold('✗')} ${referrer} linked to empty href`)
|
2023-03-09 11:34:41 +00:00
|
|
|
if (isCI && path?.match(/\/docs\//)) { actions.error(message) }
|
2022-10-03 13:38:06 +00:00
|
|
|
logger.log(message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:06:15 +00:00
|
|
|
if (urlsToOmit.some(url => path.startsWith(url))) { return }
|
|
|
|
|
|
|
|
const { pathname, origin } = new URL(path, referrer)
|
|
|
|
|
|
|
|
// Don't crawl the same page more than once
|
|
|
|
const url = `${origin}${pathname}`
|
|
|
|
if (!url || urls.has(url) || !crawler) { return }
|
|
|
|
|
|
|
|
// Don't try to visit linked assets (e.g. SVGs)
|
|
|
|
const extension = url.split('.').pop()
|
|
|
|
if (extension && excludedExtensions.includes(extension)) { return }
|
|
|
|
|
|
|
|
// Don't crawl external URLs
|
|
|
|
if (origin !== baseURL) { return }
|
|
|
|
|
2023-03-09 11:34:41 +00:00
|
|
|
referrers.set(url, referrer)
|
2022-09-13 16:06:15 +00:00
|
|
|
urls.add(url)
|
|
|
|
|
|
|
|
crawler.queue(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
const crawler = new Crawler({
|
|
|
|
maxConnections: 100,
|
|
|
|
callback (error, res, done) {
|
|
|
|
const { $ } = res
|
|
|
|
const { uri } = res.options
|
2023-05-06 22:00:50 +00:00
|
|
|
const { statusCode } = res.response
|
2022-09-13 16:06:15 +00:00
|
|
|
|
|
|
|
if (error || ![200, 301, 302].includes(statusCode) || !$) {
|
2023-03-09 11:34:41 +00:00
|
|
|
// TODO: normalize relative links in module readmes - https://github.com/nuxt/nuxt.com/issues/1271
|
|
|
|
if (errorsToIgnore.includes(parseURL(uri).pathname) || referrers.get(uri)?.match(/\/modules\//) || !uri?.match(/\/docs\//)) {
|
|
|
|
const message = chalk.gray(`${chalk.bold('✗')} ${uri} (${statusCode}) [<- ${referrers.get(uri)}] (ignored)`)
|
2022-09-13 16:06:15 +00:00
|
|
|
logger.log(message)
|
|
|
|
return done()
|
|
|
|
}
|
2023-03-09 11:34:41 +00:00
|
|
|
const message = chalk.red(`${chalk.bold('✗')} ${uri} (${statusCode}) [<- ${referrers.get(uri)}]`)
|
2022-09-13 16:06:15 +00:00
|
|
|
if (isCI) { actions.error(message) }
|
|
|
|
logger.log(message)
|
|
|
|
erroredUrls.add(uri)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$) {
|
|
|
|
const message = `Could not parse HTML for ${uri}`
|
|
|
|
logger.error(message)
|
|
|
|
if (isCI) { actions.warning(message) }
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:38:06 +00:00
|
|
|
$('a:not([href*=mailto]):not([href*=tel])').each((_, el) => {
|
|
|
|
if ('attribs' in el && 'href' in el.attribs) {
|
|
|
|
queue(el.attribs.href, uri)
|
|
|
|
}
|
|
|
|
})
|
2022-09-13 16:06:15 +00:00
|
|
|
|
|
|
|
logger.success(chalk.green(uri))
|
|
|
|
logger.debug(uri, `[${crawler.queueSize} / ${urls.size}]`)
|
|
|
|
|
|
|
|
if (!isCI && crawler.queueSize === 1) {
|
|
|
|
logger.log('')
|
|
|
|
logger.info(`Checked \`${urls.size}\` pages.`)
|
|
|
|
// Tasks to run at the end.
|
|
|
|
if (erroredUrls.size) {
|
|
|
|
const message = `${chalk.bold(erroredUrls.size)} errors found on ${chalk.bold(baseURL)}.`
|
|
|
|
const error = new Error(`\n\n${message}\n`)
|
|
|
|
error.message = message
|
|
|
|
error.stack = ''
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.log('')
|
|
|
|
logger.info(`Checking \`${baseURL}\`.`)
|
|
|
|
logger.info(`Ignoring file extensions: \`${excludedExtensions.join(', ')}.\`\n`)
|
|
|
|
|
|
|
|
crawler.queue(startingURL)
|