const chalk = require('chalk')
const libxml = require('libxmljs')
const { program } = require('commander')
const { logger, file } = require('../core')
const xsd = `
`
program.argument('', 'Path to file to validate').parse(process.argv)
async function main() {
if (!program.args.length) {
logger.error('required argument "filepath" not specified')
}
let errors = []
for (const filepath of program.args) {
const xml = await file.read(filepath)
let localErrors = []
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) {
logger.info(`\n${chalk.underline(filepath)}`)
localErrors.forEach(error => {
const position = `${error.line}:${error.column}`
logger.error(` ${chalk.gray(position.padEnd(4, ' '))} ${error.message.trim()}`)
})
errors = errors.concat(localErrors)
}
}
if (errors.length) {
logger.error(chalk.red(`\n${errors.length} error(s)`))
process.exit(1)
}
}
main()