|
| 1 | +const globby = require('globby') |
| 2 | +const posthtml = require('posthtml') |
| 3 | +const fs = require('fs') |
| 4 | +const server = require('browser-sync').create() |
| 5 | +const checkLinks = require('check-links') |
| 6 | +const ora = require('ora') |
| 7 | + |
| 8 | + |
| 9 | +const checkForDeadLocalUrls = async () => { |
| 10 | + try { |
| 11 | + const files = await globby('_site/**/*.html') |
| 12 | + const throbber = ora('Link Check Starting').start() |
| 13 | + const urls = new Set() |
| 14 | + |
| 15 | + const ph = posthtml([ |
| 16 | + require('posthtml-urls')({ |
| 17 | + eachURL: (url) => { |
| 18 | + if (url.startsWith('/docs/')) { |
| 19 | + urls.add(url.replace('/docs/', 'http://localhost:3000/')) |
| 20 | + } |
| 21 | + }, |
| 22 | + }), |
| 23 | + ]) |
| 24 | + throbber.succeed() |
| 25 | + throbber.start('Processing files') |
| 26 | + |
| 27 | + files.forEach((file) => { |
| 28 | + ph.process(fs.readFileSync(file)) |
| 29 | + }) |
| 30 | + throbber.succeed() |
| 31 | + throbber.start('Starting server') |
| 32 | + |
| 33 | + await new Promise((resolve) => { |
| 34 | + server.init({ |
| 35 | + port: 3000, |
| 36 | + server: { |
| 37 | + baseDir: '_site', |
| 38 | + }, |
| 39 | + open: false, |
| 40 | + logLevel: 'silent', |
| 41 | + }, |
| 42 | + resolve, |
| 43 | + ) |
| 44 | + throbber.succeed() |
| 45 | + }) |
| 46 | + |
| 47 | + const results = await checkLinks( |
| 48 | + Array.from(urls).map((url) => |
| 49 | + url |
| 50 | + ), |
| 51 | + ) |
| 52 | + const deadUrls = Array.from(urls).filter( |
| 53 | + (url) => results[url].status === 'dead', |
| 54 | + ) |
| 55 | + |
| 56 | + let broke = [] |
| 57 | + |
| 58 | + deadUrls.forEach(url => { |
| 59 | + link = url.replace('http://localhost:3000', 'https://segment.com/docs') |
| 60 | + if (!link.endsWith('/')){ |
| 61 | + link = link+'/' |
| 62 | + } |
| 63 | + broke.push(link) |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + 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/'] |
| 69 | + const data = require('../_site/redirects.json') |
| 70 | + Object.keys(data).forEach(key => { |
| 71 | + if (!key.endsWith('/')){ |
| 72 | + key = key+'/' |
| 73 | + } |
| 74 | + redirects.push('https://segment.com/docs'+key.replace('/docs','')) |
| 75 | + }) |
| 76 | + broke = broke.filter(val => !redirects.includes(val)); |
| 77 | + |
| 78 | + if (broke.length > 0) { |
| 79 | + throbber.fail(`Dead URLS: ${broke.length}\n\n`) |
| 80 | + console.log(`Dead URLS: ${broke.length}\n\n${broke.join('\n')}`) |
| 81 | + process.exit(1) |
| 82 | + }else { |
| 83 | + console.log('All links work!') |
| 84 | + process.exit |
| 85 | + } |
| 86 | + throbber.stop() |
| 87 | + server.exit() |
| 88 | + } catch (e) { |
| 89 | + console.error(e) |
| 90 | + server.exit() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +checkForDeadLocalUrls() |
| 95 | + |
0 commit comments