1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/script/check-internal-links.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

87 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
import linkinator from 'linkinator'
import { deprecated, latest } from '../lib/enterprise-server-releases.js'
const checker = new linkinator.LinkChecker()
const englishRoot = 'http://localhost:4002/en'
const allowedVersions = ['dotcom', 'enterprise-server', 'github-ae']
// [start-readme]
//
// This script runs in CI via GitHub Action to check all *internal* links in English content for a
// given version, not including deprecated Enterprise Server content. This is different from
// script/check-english-links.js, which checks *all* links in the site, both internal and external,
// and is much slower.
//
// If you want to run it locally, you must have a local server running. You can use `npm run link-check`.
//
// [end-readme]
const config = {
path: englishRoot,
// Use concurrency = 10 to optimize for Actions
// See https://github.com/JustinBeckwith/linkinator/issues/135#issuecomment-623240879
concurrency: 10,
recurse: true,
linksToSkip: [
// Skip any link that is not an internal link.
// NOTE: If we want this test to check for broken asset paths in the future,
// we can remove `en` from the path below. This will increase the runtime, but that
// may be an acceptable tradeoff. For the record: `check-external-links`, which runs
// nightly, currently does check for broken asset paths.
`^((?!${englishRoot}).)*$`,
// Skip dist files
'/dist/index.*',
// Skip deprecated Enterprise content
`enterprise(-server@|/)(${deprecated.join('|')})(/|$)`
]
}
// Customize config for specific versions
if (process.env.DOCS_VERSION === 'dotcom') {
// If Dotcom, skip Enterprise Server and GitHub AE links
config.linksToSkip.push('^.*/enterprise-server@.*$', '^.*/enterprise/.*$', '^.*/github-ae@latest.*$')
} else if (process.env.DOCS_VERSION === 'enterprise-server') {
// If Enterprise Server, skip links that are not Enterprise Server links
config.path = `${englishRoot}/enterprise-server@${latest}`
config.linksToSkip.push('^((?!enterprise-server@).)*$')
} else if (process.env.DOCS_VERSION === 'github-ae') {
// If GitHub AE, skip links that are not GitHub AE links
config.path = `${englishRoot}/github-ae@latest`
config.linksToSkip.push('^((?!github-ae@latest).)*$')
}
main()
async function main () {
process.env.DOCS_VERSION && allowedVersions.includes(process.env.DOCS_VERSION)
? console.log(`Checking internal links for version ${process.env.DOCS_VERSION}!\n`)
: console.log('Checking internal links for all versions!\n')
console.time('check')
const result = (await checker.check(config)).links
console.timeEnd('check')
const brokenLinks = result
.filter(link => link.state === 'BROKEN')
.map(link => { delete link.failureDetails; return link })
if (brokenLinks.length === 1 && brokenLinks[0].url === englishRoot) {
console.log(`You must be running ${englishRoot}!\n\nTry instead: npm run link-check`)
process.exit(1)
}
// Exit successfully if no broken links!
if (!brokenLinks.length) {
console.log('All links are good!')
process.exit(0)
}
console.log('\n==============================')
console.log(`Found ${brokenLinks.length} total broken links: ${JSON.stringify([...brokenLinks], null, 2)}`)
console.log('==============================\n')
// Exit unsuccessfully if broken links are found.
process.exit(1)
}