|
| 1 | +/** |
| 2 | + * Minimal markdown smoke check for docs. |
| 3 | + * |
| 4 | + * Goals: |
| 5 | + * - Ensure all markdown files under docs/ are readable. |
| 6 | + * - Fail fast on obvious bad states (e.g. unresolved merge markers). |
| 7 | + * |
| 8 | + * This is intentionally lightweight and dependency-free so it can run quickly in CI. |
| 9 | + */ |
| 10 | + |
| 11 | +import fs from 'node:fs'; |
| 12 | +import path from 'node:path'; |
| 13 | + |
| 14 | +const DOCS_ROOT = path.join(process.cwd(), 'docs'); |
| 15 | + |
| 16 | +function isMarkdownFile(filePath: string): boolean { |
| 17 | + return filePath.toLowerCase().endsWith('.md'); |
| 18 | +} |
| 19 | + |
| 20 | +function findMarkdownFiles(root: string): string[] { |
| 21 | + const results: string[] = []; |
| 22 | + |
| 23 | + function walk(current: string) { |
| 24 | + const entries = fs.readdirSync(current, {withFileTypes: true}); |
| 25 | + for (const entry of entries) { |
| 26 | + const fullPath = path.join(current, entry.name); |
| 27 | + if (entry.isDirectory()) { |
| 28 | + walk(fullPath); |
| 29 | + } else if (entry.isFile() && isMarkdownFile(entry.name)) { |
| 30 | + results.push(fullPath); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + walk(root); |
| 36 | + return results; |
| 37 | +} |
| 38 | + |
| 39 | +function checkFile(filePath: string): string[] { |
| 40 | + const errors: string[] = []; |
| 41 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 42 | + |
| 43 | + if (!content.trim()) { |
| 44 | + errors.push('file is empty'); |
| 45 | + } |
| 46 | + |
| 47 | + const lines = content.split(/\r?\n/); |
| 48 | + lines.forEach((line, index) => { |
| 49 | + const lineNumber = index + 1; |
| 50 | + if (line.includes('<<<<<<<') || line.includes('=======') || line.includes('>>>>>>>')) { |
| 51 | + errors.push(`unresolved merge marker on line ${lineNumber}`); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + return errors; |
| 56 | +} |
| 57 | + |
| 58 | +function main() { |
| 59 | + if (!fs.existsSync(DOCS_ROOT)) { |
| 60 | + console.error(`Docs directory not found at ${DOCS_ROOT}`); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + const markdownFiles = findMarkdownFiles(DOCS_ROOT); |
| 65 | + const allErrors: string[] = []; |
| 66 | + |
| 67 | + for (const file of markdownFiles) { |
| 68 | + try { |
| 69 | + const errors = checkFile(file); |
| 70 | + for (const err of errors) { |
| 71 | + allErrors.push(`${path.relative(process.cwd(), file)}: ${err}`); |
| 72 | + } |
| 73 | + } catch (error) { |
| 74 | + allErrors.push( |
| 75 | + `${path.relative(process.cwd(), file)}: failed to read or parse file: ${ |
| 76 | + (error as Error).message |
| 77 | + }`, |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (allErrors.length > 0) { |
| 83 | + console.error('Docs smoke check failed with the following issues:\n'); |
| 84 | + for (const msg of allErrors) { |
| 85 | + console.error(`- ${msg}`); |
| 86 | + } |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + console.log(`Docs smoke check passed for ${markdownFiles.length} markdown file(s).`); |
| 91 | +} |
| 92 | + |
| 93 | +main(); |
| 94 | + |
0 commit comments