|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath, pathToFileURL } from 'url'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +const navPath = '../docs/nav.mjs'; |
| 11 | + |
| 12 | +const mdListPath = process.argv[2]; |
| 13 | +if (!mdListPath) { |
| 14 | + console.error('Usage: node checkNav.mjs <md-list-file>'); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +const navFile = path.resolve(__dirname, navPath); |
| 19 | +const nav = await import(pathToFileURL(navFile).href).then(mod => mod.default); |
| 20 | + |
| 21 | +const extractPathsFromNav = (items) => |
| 22 | + items.filter(item => item.type === 'page').map(page => page.path); |
| 23 | + |
| 24 | +const navPaths = extractPathsFromNav(nav.items); |
| 25 | +const navPathSet = new Set(navPaths); |
| 26 | + |
| 27 | +const expectedMdPaths = fs.readFileSync(path.resolve(__dirname, mdListPath), 'utf8') |
| 28 | + .split('\n') |
| 29 | + .map(line => line.trim()) |
| 30 | + .filter(Boolean); |
| 31 | +const expectedPathSet = new Set(expectedMdPaths); |
| 32 | + |
| 33 | +const missingInNav = expectedMdPaths.filter(p => !navPathSet.has(p)); |
| 34 | +const extraInNav = navPaths.filter(p => !expectedPathSet.has(p)); |
| 35 | + |
| 36 | +let failed = false; |
| 37 | + |
| 38 | +if (missingInNav.length > 0) { |
| 39 | + console.error('❌ These docs are missing from nav:'); |
| 40 | + missingInNav.forEach(p => console.error(`- ${p}`)); |
| 41 | + failed = true; |
| 42 | +} |
| 43 | + |
| 44 | +if (extraInNav.length > 0) { |
| 45 | + console.error('❌ These docs are listed in nav but not found under docs/:'); |
| 46 | + extraInNav.forEach(p => console.error(`- ${p}`)); |
| 47 | + failed = true; |
| 48 | +} |
| 49 | + |
| 50 | +if (!failed) { |
| 51 | + console.log('✅ nav list matches filesystem.'); |
| 52 | +} else { |
| 53 | + process.exit(1); |
| 54 | +} |
0 commit comments