From 0753151770f131f53822b0e5db1fa3cf7abe7673 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Thu, 3 Feb 2022 04:20:25 +0300 Subject: [PATCH] Create lint.js --- scripts/commands/lint.js | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 scripts/commands/lint.js diff --git a/scripts/commands/lint.js b/scripts/commands/lint.js new file mode 100644 index 00000000..48c32794 --- /dev/null +++ b/scripts/commands/lint.js @@ -0,0 +1,74 @@ +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()