|
| 1 | +#!/usr/bin/env node |
| 2 | +const Ajv = require('ajv'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const yaml = require('js-yaml'); |
| 6 | +const yargs = require('yargs/yargs') |
| 7 | +const {hideBin} = require('yargs/helpers') |
| 8 | +const ajv = new Ajv({ |
| 9 | + strictTypes: false, |
| 10 | +}); |
| 11 | + |
| 12 | +// Add support for intellij html description |
| 13 | +ajv.addKeyword('x-intellij-html-description'); |
| 14 | + |
| 15 | +// Build schema map |
| 16 | +const schemas = [ |
| 17 | + { |
| 18 | + pattern: /NodeTypes(\/?).*\.yaml$/, |
| 19 | + schemaFile: './NodeTypes.Schema.json', |
| 20 | + }, |
| 21 | + { |
| 22 | + pattern: /Caches.*\.yaml$/, |
| 23 | + schemaFile: './Caches.Schema.json', |
| 24 | + }, |
| 25 | + { |
| 26 | + pattern: /Version.*\.yaml$/, |
| 27 | + schemaFile: './NodeMigration.Schema.json', |
| 28 | + }, |
| 29 | + { |
| 30 | + pattern: /Routes.*\.yaml$/, |
| 31 | + schemaFile: './Routes.Schema.json', |
| 32 | + }, |
| 33 | + { |
| 34 | + pattern: /Settings.Presets.yaml$/, |
| 35 | + schemaFile: './NodeTypes.Presets.Schema.json', |
| 36 | + }, |
| 37 | +].map(sd => { |
| 38 | + const schema = JSON.parse(fs.readFileSync(sd.schemaFile, {encoding: 'utf8', flag: 'r'})); |
| 39 | + const validate = ajv.compile(schema); |
| 40 | + return { |
| 41 | + ...sd, |
| 42 | + schema, |
| 43 | + validate, |
| 44 | + }; |
| 45 | +}); |
| 46 | + |
| 47 | +/** |
| 48 | + * Recursively list yaml files in a directory |
| 49 | + * |
| 50 | + * @param {string} dir |
| 51 | + * @return {string[]} |
| 52 | + */ |
| 53 | +function listYamlFiles(dir) { |
| 54 | + let results = []; |
| 55 | + let files = []; |
| 56 | + |
| 57 | + // Read the contents of the directory |
| 58 | + try { |
| 59 | + files = fs.readdirSync(dir); |
| 60 | + } catch (e) { |
| 61 | + console.error('Could not read directory %s', dir); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | + |
| 65 | + files.forEach(file => { |
| 66 | + const filePath = path.join(dir, file); |
| 67 | + const stat = fs.statSync(filePath); |
| 68 | + |
| 69 | + // If it's a directory, recurse into it |
| 70 | + if (stat && stat.isDirectory()) { |
| 71 | + results = results.concat(listYamlFiles(filePath)); |
| 72 | + } else if (file.endsWith('.yaml') || file.endsWith('.yml')) { |
| 73 | + // If it's a YAML file, add to results |
| 74 | + results.push(filePath); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + return results; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * @param {string} filePath |
| 83 | + * @param {object} schema |
| 84 | + */ |
| 85 | +function validateFile(filePath, schema) { |
| 86 | + const fileContent = fs.readFileSync(filePath, {encoding: 'utf8', flag: 'r'}); |
| 87 | + const data = yaml.load(fileContent); |
| 88 | + return schema.validate(data); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @param {string} folder |
| 93 | + * @param {boolean} verbose |
| 94 | + * @return {void} |
| 95 | + */ |
| 96 | +function validateFolder(folder, verbose) { |
| 97 | + const files = listYamlFiles(folder); |
| 98 | + let hasError = false; |
| 99 | + let validFiles = 0; |
| 100 | + let skippedFiles = 0; |
| 101 | + let invalidFiles = 0; |
| 102 | + |
| 103 | + if (verbose) { |
| 104 | + console.info('Found %d files to validate', files.length); |
| 105 | + } |
| 106 | + |
| 107 | + for (const file of files) { |
| 108 | + // Only validate yaml files |
| 109 | + if (path.extname(file).match(/\.ya?ml/) === null) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + const schema = schemas.find(sd => sd.pattern.test(file)); |
| 114 | + if (!schema) { |
| 115 | + if (verbose) { |
| 116 | + console.warn('Skipped %s, no matching schema found', file); |
| 117 | + } |
| 118 | + skippedFiles++; |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + const valid = validateFile(file, schema); |
| 123 | + if (valid) { |
| 124 | + if (verbose) { |
| 125 | + console.info('✅ %s is valid', file); |
| 126 | + } |
| 127 | + validFiles++; |
| 128 | + } else { |
| 129 | + console.error('❌ %s is invalid', file, schema.validate.errors); |
| 130 | + hasError = true; |
| 131 | + invalidFiles++; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (verbose) { |
| 136 | + console.info('Valid files %d, skipped %d, invalid %d', validFiles, skippedFiles, invalidFiles); |
| 137 | + } |
| 138 | + |
| 139 | + process.exit(hasError ? 1 : 0); |
| 140 | +} |
| 141 | + |
| 142 | +yargs(hideBin(process.argv)) |
| 143 | + .command('validate <folder>', 'validate yaml files', (yargs) => { |
| 144 | + return yargs |
| 145 | + .positional('folder', { |
| 146 | + describe: 'folder with files to be validated', |
| 147 | + type: 'string', |
| 148 | + }); |
| 149 | + }, (argv) => { |
| 150 | + const {folder, verbose} = argv; |
| 151 | + validateFolder(folder, verbose); |
| 152 | + }) |
| 153 | + .example('$0 validate -v DistributionPackages/My.Site', 'Recursively validates all nodetype and other supported yaml files in your Neos site package and shows the results for each file') |
| 154 | + .option('verbose', { |
| 155 | + alias: 'v', |
| 156 | + type: 'boolean', |
| 157 | + description: 'Run with verbose logging' |
| 158 | + }) |
| 159 | + .demandCommand() |
| 160 | + .help() |
| 161 | + .parse(); |
0 commit comments