|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { CONSTANTS, loadMintIgnore } = require('./lib/docs-utils'); |
| 6 | + |
| 7 | +function isExternalDestination(value) { |
| 8 | + return ( |
| 9 | + typeof value === 'string' && |
| 10 | + (value.startsWith('//') || /^[A-Za-z][A-Za-z\d+.-]*:/.test(value)) |
| 11 | + ); |
| 12 | +} |
| 13 | + |
| 14 | +function normalizeInternalPath(value) { |
| 15 | + if (typeof value !== 'string' || !value.startsWith('/') || value.startsWith('//')) return null; |
| 16 | + |
| 17 | + const clean = value.split(/[?#]/, 1)[0].replace(/\/+$/, ''); |
| 18 | + return clean || '/'; |
| 19 | +} |
| 20 | + |
| 21 | +function matchesRoutePattern(value, routes) { |
| 22 | + const internal = normalizeInternalPath(value); |
| 23 | + if (!internal) return false; |
| 24 | + |
| 25 | + const match = internal.match(/^(.*)\/:([A-Za-z][A-Za-z\d_]*)\*$/); |
| 26 | + if (!match) return false; |
| 27 | + |
| 28 | + const prefix = match[1] || '/'; |
| 29 | + return routes.has(prefix) || [...routes].some((route) => route.startsWith(`${prefix}/`)); |
| 30 | +} |
| 31 | + |
| 32 | +function isMintIgnored(docsDir, fullPath, ignored) { |
| 33 | + const relative = path.relative(docsDir, fullPath).split(path.sep).join('/'); |
| 34 | + const withoutExtension = relative.replace(/\.mdx?$/, ''); |
| 35 | + const basenameWithoutExtension = path.posix.basename(withoutExtension); |
| 36 | + |
| 37 | + if (ignored.files.has(relative) || ignored.files.has(withoutExtension)) return true; |
| 38 | + if (ignored.bareFiles.has(withoutExtension) || ignored.bareFiles.has(basenameWithoutExtension)) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + for (const ignoredDir of ignored.dirs) { |
| 43 | + if (relative === ignoredDir || relative.startsWith(`${ignoredDir}/`)) return true; |
| 44 | + } |
| 45 | + |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +function collectRoutes(docsDir) { |
| 50 | + const routes = new Set(['/']); |
| 51 | + const ignored = loadMintIgnore(path.join(docsDir, '.mintignore')); |
| 52 | + |
| 53 | + function walk(dir) { |
| 54 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 55 | + if (entry.name.startsWith('.')) continue; |
| 56 | + if (CONSTANTS.skipFiles.includes(entry.name)) continue; |
| 57 | + if (entry.isDirectory() && CONSTANTS.skipDirs.includes(entry.name)) continue; |
| 58 | + |
| 59 | + const fullPath = path.join(dir, entry.name); |
| 60 | + |
| 61 | + if (isMintIgnored(docsDir, fullPath, ignored)) continue; |
| 62 | + |
| 63 | + if (entry.isDirectory()) { |
| 64 | + walk(fullPath); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + const extension = path.extname(entry.name).toLowerCase(); |
| 69 | + if (!entry.isFile() || !CONSTANTS.extensions.includes(extension)) continue; |
| 70 | + |
| 71 | + let route = path |
| 72 | + .relative(docsDir, fullPath) |
| 73 | + .split(path.sep) |
| 74 | + .join('/') |
| 75 | + .replace(/\.mdx?$/, ''); |
| 76 | + |
| 77 | + if (route === 'index') route = ''; |
| 78 | + if (route.endsWith('/index')) route = route.slice(0, -'/index'.length); |
| 79 | + |
| 80 | + routes.add(`/${route}`.replace(/\/+$/, '') || '/'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + walk(docsDir); |
| 85 | + return routes; |
| 86 | +} |
| 87 | + |
| 88 | +function auditRedirects(config, routes) { |
| 89 | + const redirects = Array.isArray(config.redirects) ? config.redirects : []; |
| 90 | + const redirectMap = new Map(); |
| 91 | + |
| 92 | + for (const redirect of redirects) { |
| 93 | + const source = normalizeInternalPath(redirect.source); |
| 94 | + if (source && typeof redirect.destination === 'string') { |
| 95 | + redirectMap.set(source, redirect.destination); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + function resolve(destination) { |
| 100 | + let current = destination; |
| 101 | + const visited = new Set(); |
| 102 | + |
| 103 | + while (true) { |
| 104 | + if (isExternalDestination(current)) { |
| 105 | + return { ok: true, terminal: current, reason: 'external' }; |
| 106 | + } |
| 107 | + |
| 108 | + const internal = normalizeInternalPath(current); |
| 109 | + if (!internal) return { ok: false, terminal: current, reason: 'invalid' }; |
| 110 | + if (routes.has(internal)) return { ok: true, terminal: internal, reason: 'page' }; |
| 111 | + if (matchesRoutePattern(internal, routes)) { |
| 112 | + return { ok: true, terminal: internal, reason: 'pattern' }; |
| 113 | + } |
| 114 | + if (visited.has(internal)) return { ok: false, terminal: internal, reason: 'cycle' }; |
| 115 | + |
| 116 | + visited.add(internal); |
| 117 | + const next = redirectMap.get(internal); |
| 118 | + if (!next) return { ok: false, terminal: internal, reason: 'missing' }; |
| 119 | + current = next; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + const brokenByDestination = new Map(); |
| 124 | + |
| 125 | + for (const redirect of redirects) { |
| 126 | + if (typeof redirect.destination !== 'string') continue; |
| 127 | + if (isExternalDestination(redirect.destination)) continue; |
| 128 | + |
| 129 | + const destination = normalizeInternalPath(redirect.destination) || redirect.destination; |
| 130 | + const result = resolve(redirect.destination); |
| 131 | + if (result.ok) continue; |
| 132 | + |
| 133 | + const existing = brokenByDestination.get(destination) || { |
| 134 | + destination, |
| 135 | + terminal: result.terminal, |
| 136 | + reason: result.reason, |
| 137 | + count: 0, |
| 138 | + sources: [], |
| 139 | + }; |
| 140 | + |
| 141 | + existing.count += 1; |
| 142 | + if (typeof redirect.source === 'string') existing.sources.push(redirect.source); |
| 143 | + brokenByDestination.set(destination, existing); |
| 144 | + } |
| 145 | + |
| 146 | + return [...brokenByDestination.values()].sort( |
| 147 | + (a, b) => b.count - a.count || a.destination.localeCompare(b.destination), |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +function printReport(broken) { |
| 152 | + if (broken.length === 0) { |
| 153 | + console.log('All internal redirect destinations resolve to an existing docs page.'); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + const totalEntries = broken.reduce((sum, item) => sum + item.count, 0); |
| 158 | + console.log( |
| 159 | + `Found ${broken.length} broken internal redirect destinations across ${totalEntries} redirect entries.`, |
| 160 | + ); |
| 161 | + console.log(''); |
| 162 | + console.log('Count\tDestination\tTerminal\tReason'); |
| 163 | + |
| 164 | + for (const item of broken) { |
| 165 | + console.log(`${item.count}\t${item.destination}\t${item.terminal}\t${item.reason}`); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +function main() { |
| 170 | + const repoRoot = path.resolve(__dirname, '..'); |
| 171 | + const docsDir = path.join(repoRoot, 'docs'); |
| 172 | + const configPath = path.join(docsDir, 'docs.json'); |
| 173 | + const strict = process.argv.includes('--strict'); |
| 174 | + |
| 175 | + const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 176 | + const routes = collectRoutes(docsDir); |
| 177 | + const broken = auditRedirects(config, routes); |
| 178 | + |
| 179 | + printReport(broken); |
| 180 | + |
| 181 | + if (strict && broken.length > 0) process.exitCode = 1; |
| 182 | +} |
| 183 | + |
| 184 | +if (require.main === module) main(); |
| 185 | + |
| 186 | +module.exports = { |
| 187 | + auditRedirects, |
| 188 | + collectRoutes, |
| 189 | + isExternalDestination, |
| 190 | + matchesRoutePattern, |
| 191 | + normalizeInternalPath, |
| 192 | +}; |
0 commit comments