import chalk from 'chalk' import libxml, { ValidationError } from 'libxmljs2' import { program } from 'commander' import { Storage, File } from '@freearhey/core' const xsd = ` ` program.argument('[filepath]', 'Path to *.channels.xml files to validate').parse(process.argv) async function main() { const storage = new Storage() let errors: ValidationError[] = [] const files = program.args.length ? program.args : await storage.list('sites/**/*.channels.xml') for (const filepath of files) { const file = new File(filepath) if (file.extension() !== 'xml') continue const xml = await storage.load(filepath) let localErrors: ValidationError[] = [] try { const xsdDoc = libxml.parseXml(xsd) const doc = libxml.parseXml(xml) if (!doc.validate(xsdDoc)) { localErrors = doc.validationErrors } } catch (error) { localErrors.push(error) } if (localErrors.length) { console.log(`\n${chalk.underline(filepath)}`) localErrors.forEach((error: ValidationError) => { const position = `${error.line}:${error.column}` console.log(` ${chalk.gray(position.padEnd(4, ' '))} ${error.message.trim()}`) }) errors = errors.concat(localErrors) } } if (errors.length) { console.log(chalk.red(`\n${errors.length} error(s)`)) process.exit(1) } } main()