|
| 1 | +// A script the check all local links on the docs site |
| 2 | + |
| 3 | +const globby = require('globby') |
| 4 | +const posthtml = require('posthtml') |
| 5 | +const fs = require('fs') |
| 6 | +const server = require('browser-sync').create() |
| 7 | +const checkLinks = require('check-links') |
| 8 | +const ora = require('ora') |
| 9 | + |
| 10 | + |
| 11 | +const checkForDeadLocalUrls = async () => { |
| 12 | + try { |
| 13 | + // Grab all the files from the specified directory, add their paths to a new set |
| 14 | + const files = await globby('_site/**/*.html') |
| 15 | + const throbber = ora('Link Check Starting').start() |
| 16 | + // Use a set here for efficiency, no duplicate values! |
| 17 | + const urls = new Set() |
| 18 | + |
| 19 | + // Logic for collecting the list of URLs to check |
| 20 | + // If the link starts with `/docs/`, replace that with a localhost:3000 domain, and add it to the list. |
| 21 | + const ph = posthtml([ |
| 22 | + require('posthtml-urls')({ |
| 23 | + eachURL: (url) => { |
| 24 | + if (url.startsWith('/docs/')) { |
| 25 | + urls.add(url.replace('/docs/', 'http://localhost:3000/')) |
| 26 | + } |
| 27 | + }, |
| 28 | + }), |
| 29 | + ]) |
| 30 | + throbber.succeed() |
| 31 | + |
| 32 | + // Using the logic above, iterate through the entire list of files |
| 33 | + throbber.start('Processing files') |
| 34 | + files.forEach((file) => { |
| 35 | + ph.process(fs.readFileSync(file)) |
| 36 | + }) |
| 37 | + throbber.succeed() |
| 38 | + |
| 39 | + // Spin up a lightweight browsersync server to check each URL |
| 40 | + throbber.start('Starting server') |
| 41 | + await new Promise((resolve) => { |
| 42 | + server.init({ |
| 43 | + port: 3000, |
| 44 | + server: { |
| 45 | + baseDir: '_site', |
| 46 | + }, |
| 47 | + open: false, |
| 48 | + logLevel: 'silent', |
| 49 | + }, |
| 50 | + resolve, |
| 51 | + ) |
| 52 | + throbber.succeed() |
| 53 | + }) |
| 54 | + |
| 55 | + // Check the links against the local browsersync site |
| 56 | + const results = await checkLinks( |
| 57 | + Array.from(urls).map((url) => |
| 58 | + url |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + // If a link returns 'dead' (404), add it to an array |
| 63 | + const deadUrls = Array.from(urls).filter( |
| 64 | + (url) => results[url].status === 'dead', |
| 65 | + ) |
| 66 | + |
| 67 | + // For ease of checking, replace the localhost domain with the live domain, add those to a new array. |
| 68 | + let broke = [] |
| 69 | + deadUrls.forEach(url => { |
| 70 | + link = url.replace('http://localhost:3000', 'https://segment.com/docs') |
| 71 | + if (!link.endsWith('/')){ |
| 72 | + link = link+'/' |
| 73 | + } |
| 74 | + broke.push(link) |
| 75 | + }); |
| 76 | + |
| 77 | + |
| 78 | + // Sometimes, we redirect urls based on jekyll settings, or a setting an app-nginx. |
| 79 | + // For those, we want to remove them from the list of dead links, because they aren't dead. |
| 80 | + |
| 81 | + // app-nginx redirects |
| 82 | + const redirects = ['https://segment.com/docs/guides/usage-and-billing/','https://segment.com/docs/connections/sources/catalog/libraries/website/plugins/', 'https://segment.com/docs/assets/docs.bundle.js/'] |
| 83 | + |
| 84 | + // Redirects generated by Jekyll |
| 85 | + // Pull the redirects json file |
| 86 | + const data = require('../_site/redirects.json') |
| 87 | + // Grab the 'from' redirect |
| 88 | + Object.keys(data).forEach(key => { |
| 89 | + if (!key.endsWith('/')){ |
| 90 | + key = key+'/' |
| 91 | + } |
| 92 | + redirects.push('https://segment.com/docs'+key.replace('/docs','')) |
| 93 | + }) |
| 94 | + // Remove the redirect urls from the list of broken URLs |
| 95 | + broke = broke.filter(val => !redirects.includes(val)); |
| 96 | + |
| 97 | + // If there are dead URLs, list them here, along with the count. Exit status 1 to indicate an error. |
| 98 | + |
| 99 | + if (broke.length > 0) { |
| 100 | + throbber.fail(`Dead URLS: ${broke.length}\n\n`) |
| 101 | + console.log(`Dead URLS: ${broke.length}\n\n${broke.join('\n')}`) |
| 102 | + process.exit(1) |
| 103 | + }else { |
| 104 | + console.log('All links work!') |
| 105 | + process.exit |
| 106 | + } |
| 107 | + throbber.stop() |
| 108 | + server.exit() |
| 109 | + } catch (e) { |
| 110 | + console.error(e) |
| 111 | + server.exit() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +checkForDeadLocalUrls() |
| 116 | + |
0 commit comments