|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Validate YAML syntax for ObjectQL metadata files |
| 4 | + * |
| 5 | + * This script validates all metadata files in the repository: |
| 6 | + * - *.object.yml |
| 7 | + * - *.validation.yml |
| 8 | + * - *.permission.yml |
| 9 | + * - *.app.yml |
| 10 | + * - *.page.yml |
| 11 | + * - *.menu.yml |
| 12 | + */ |
| 13 | + |
| 14 | +const yaml = require('js-yaml'); |
| 15 | +const fs = require('fs'); |
| 16 | +const path = require('path'); |
| 17 | + |
| 18 | +// Define metadata file extensions |
| 19 | +const extensions = [ |
| 20 | + '.object.yml', |
| 21 | + '.validation.yml', |
| 22 | + '.permission.yml', |
| 23 | + '.app.yml', |
| 24 | + '.page.yml', |
| 25 | + '.menu.yml' |
| 26 | +]; |
| 27 | + |
| 28 | +// Directories to exclude |
| 29 | +const excludeDirs = ['node_modules', 'dist', '.git']; |
| 30 | + |
| 31 | +/** |
| 32 | + * Recursively find all files matching the specified extensions |
| 33 | + */ |
| 34 | +function findFiles(dir, fileList = []) { |
| 35 | + const files = fs.readdirSync(dir); |
| 36 | + |
| 37 | + files.forEach(file => { |
| 38 | + const filePath = path.join(dir, file); |
| 39 | + const stat = fs.statSync(filePath); |
| 40 | + |
| 41 | + if (stat.isDirectory()) { |
| 42 | + // Skip excluded directories |
| 43 | + if (!excludeDirs.includes(file)) { |
| 44 | + findFiles(filePath, fileList); |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Check if file matches any of our extensions |
| 48 | + if (extensions.some(ext => file.endsWith(ext))) { |
| 49 | + fileList.push(filePath); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + return fileList; |
| 55 | +} |
| 56 | + |
| 57 | +let hasErrors = false; |
| 58 | +let validCount = 0; |
| 59 | +let errorCount = 0; |
| 60 | + |
| 61 | +console.log('🔍 Validating ObjectQL metadata YAML files...\n'); |
| 62 | + |
| 63 | +// Find and validate all metadata files |
| 64 | +const files = findFiles(process.cwd()); |
| 65 | + |
| 66 | +if (files.length === 0) { |
| 67 | + console.log('ℹ️ No metadata files found to validate.'); |
| 68 | + process.exit(0); |
| 69 | +} |
| 70 | + |
| 71 | +files.forEach(file => { |
| 72 | + try { |
| 73 | + const content = fs.readFileSync(file, 'utf8'); |
| 74 | + yaml.load(content); |
| 75 | + console.log(`✓ ${path.relative(process.cwd(), file)}`); |
| 76 | + validCount++; |
| 77 | + } catch (error) { |
| 78 | + console.error(`✗ ${path.relative(process.cwd(), file)}`); |
| 79 | + console.error(` Error: ${error.message}`); |
| 80 | + if (error.mark) { |
| 81 | + console.error(` Line ${error.mark.line + 1}, Column ${error.mark.column + 1}`); |
| 82 | + } |
| 83 | + console.error(''); |
| 84 | + hasErrors = true; |
| 85 | + errorCount++; |
| 86 | + } |
| 87 | +}); |
| 88 | + |
| 89 | +// Print summary |
| 90 | +console.log('\n' + '='.repeat(60)); |
| 91 | +if (hasErrors) { |
| 92 | + console.error(`❌ Validation failed: ${errorCount} error(s) found`); |
| 93 | + console.log(`✓ Valid files: ${validCount}`); |
| 94 | + console.error(`✗ Invalid files: ${errorCount}`); |
| 95 | + process.exit(1); |
| 96 | +} else { |
| 97 | + console.log(`✅ All ${validCount} YAML metadata file(s) are valid!`); |
| 98 | + process.exit(0); |
| 99 | +} |
0 commit comments