|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import fs from 'fs/promises'; |
| 4 | +import path from 'path'; |
| 5 | +import { Command } from 'commander'; |
| 6 | +import matter from 'gray-matter'; |
| 7 | + |
| 8 | +// Define command line options |
| 9 | +const program = new Command(); |
| 10 | +program |
| 11 | + .requiredOption('--indir <directory>', 'Input directory containing MDX files') |
| 12 | + .option('--count <number>', 'Number of files to process', parseInt) |
| 13 | + .parse(process.argv); |
| 14 | + |
| 15 | +const options = program.opts(); |
| 16 | + |
| 17 | +// Validate required options |
| 18 | +if (!options.indir) { |
| 19 | + console.error('❌ Error: --indir is required'); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +const inputDir = path.resolve(options.indir); |
| 24 | +const count = options.count; |
| 25 | + |
| 26 | +const main = async () => { |
| 27 | + try { |
| 28 | + // Ensure directory exists |
| 29 | + await fs.access(inputDir); |
| 30 | + |
| 31 | + console.log(`Validating frontmatter in MDX files in ${inputDir}`); |
| 32 | + |
| 33 | + // Get all MDX files from input directory |
| 34 | + const files = await fs.readdir(inputDir); |
| 35 | + const mdxFiles = files.filter(file => file.endsWith('.mdx') && file !== 'index.mdx'); |
| 36 | + |
| 37 | + console.log(`Found ${mdxFiles.length} MDX files`); |
| 38 | + |
| 39 | + // Limit the number of files to process if count is specified |
| 40 | + const filesToProcess = count ? mdxFiles.slice(0, count) : mdxFiles; |
| 41 | + console.log(`Processing ${filesToProcess.length} files from ${inputDir}`); |
| 42 | + |
| 43 | + let hasErrors = false; |
| 44 | + let errorCount = 0; |
| 45 | + |
| 46 | + for (const mdxFile of filesToProcess) { |
| 47 | + const baseName = path.basename(mdxFile, '.mdx'); |
| 48 | + const mdxFilePath = path.join(inputDir, mdxFile); |
| 49 | + |
| 50 | + try { |
| 51 | + // Read MDX file content |
| 52 | + const mdxContent = await fs.readFile(mdxFilePath, 'utf-8'); |
| 53 | + |
| 54 | + // Parse frontmatter |
| 55 | + const { data } = matter(mdxContent); |
| 56 | + |
| 57 | + // Validate frontmatter has required fields |
| 58 | + if (!data.id) { |
| 59 | + console.error(`❌ Error: No 'id' field in frontmatter of ${mdxFile}`); |
| 60 | + hasErrors = true; |
| 61 | + errorCount++; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // Check if frontmatter id matches filename |
| 66 | + if (data.id !== baseName) { |
| 67 | + console.error(`❌ Error: Frontmatter id "${data.id}" does not match filename "${baseName}" in ${mdxFile}`); |
| 68 | + hasErrors = true; |
| 69 | + errorCount++; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // Verify the file exists |
| 74 | + const mdxExists = await fs.access(mdxFilePath) |
| 75 | + .then(() => true) |
| 76 | + .catch(() => false); |
| 77 | + |
| 78 | + if (!mdxExists) { |
| 79 | + console.error(`❌ Error: MDX file ${mdxFile} does not exist but is referenced in frontmatter`); |
| 80 | + hasErrors = true; |
| 81 | + errorCount++; |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + console.log(`✅ Validated frontmatter for ${mdxFile}`); |
| 86 | + } catch (error: any) { |
| 87 | + console.error(`❌ Error processing ${mdxFile}: ${error?.message || String(error)}`); |
| 88 | + hasErrors = true; |
| 89 | + errorCount++; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (hasErrors) { |
| 94 | + console.error(`❌ Fatal error: Found ${errorCount} validation errors`); |
| 95 | + process.exit(1); |
| 96 | + } else { |
| 97 | + console.log(`✨ Validation complete! All ${filesToProcess.length} files have valid frontmatter.`); |
| 98 | + } |
| 99 | + } catch (error: any) { |
| 100 | + console.error(`❌ Fatal error: ${error?.message || String(error)}`); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +main(); |
0 commit comments