|
| 1 | +const Command = require('../../Command'); |
| 2 | +const _ = require('lodash'); |
| 3 | +const Style = require('../../../../output/style'); |
| 4 | +const path = require('path'); |
| 5 | +const { validatePipelineYaml } = require('../../helpers/validation'); |
| 6 | +const { pathExists, watchFile } = require('../../helpers/general'); |
| 7 | + |
| 8 | +const VALID_MESSAGE = Style.green('Yaml is valid!'); |
| 9 | + |
| 10 | +function _printResult(result) { |
| 11 | + console.log(result.valid ? VALID_MESSAGE : Style.red(result.message), '\n'); |
| 12 | +} |
| 13 | + |
| 14 | +const validateCmd = new Command({ |
| 15 | + root: true, |
| 16 | + command: 'validate [filenames..]', |
| 17 | + description: 'Validate codefresh pipeline yaml config files.', |
| 18 | + usage: 'Validate one or many pipeline yaml files or attach validator to one and validate on changes', |
| 19 | + webDocs: { |
| 20 | + description: 'Validate codefresh pipeline yaml config files', |
| 21 | + category: 'Validation', |
| 22 | + title: 'Validate pipeline yaml', |
| 23 | + weight: 100, |
| 24 | + }, |
| 25 | + builder: (yargs) => { |
| 26 | + yargs |
| 27 | + .positional('filenames', { |
| 28 | + describe: 'Paths to yaml files', |
| 29 | + required: true, |
| 30 | + }) |
| 31 | + .option('attach', { |
| 32 | + alias: 'a', |
| 33 | + describe: 'Attach validator to the file and validate on change', |
| 34 | + }); |
| 35 | + |
| 36 | + |
| 37 | + return yargs; |
| 38 | + }, |
| 39 | + handler: async (argv) => { |
| 40 | + let { filenames, attach } = argv; |
| 41 | + filenames = filenames.map(f => path.resolve(process.cwd(), f)); |
| 42 | + |
| 43 | + if (_.isEmpty(filenames)) { |
| 44 | + console.log('No filename provided!'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const checkPromises = filenames.map((filename) => { |
| 49 | + return pathExists(filename) |
| 50 | + .then((exists) => { |
| 51 | + if (!exists) { |
| 52 | + console.log(`File does not exist: ${filename}`); |
| 53 | + } |
| 54 | + return exists; |
| 55 | + }); |
| 56 | + }); |
| 57 | + const allExist = (await Promise.all(checkPromises)).reduce((a, b) => a && b); |
| 58 | + |
| 59 | + if (!allExist) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (filenames.length > 1) { |
| 64 | + if (attach) { |
| 65 | + console.log('Cannot watch many files!'); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + filenames.forEach(f => validatePipelineYaml(f).then((result) => { |
| 70 | + console.log(`Validation result for ${f}:`); |
| 71 | + _printResult(result); |
| 72 | + })); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const filename = filenames[0]; |
| 77 | + if (attach) { |
| 78 | + console.log(`Validator attached to file: ${filename}`); |
| 79 | + watchFile(filename, async () => { |
| 80 | + console.log('File changed'); |
| 81 | + const result = await validatePipelineYaml(filename); |
| 82 | + _printResult(result); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + // even with --attach option validates file for first time |
| 87 | + const result = await validatePipelineYaml(filename); |
| 88 | + _printResult(result); |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +module.exports = validateCmd; |
0 commit comments