diff --git a/packages/cli/src/validate.ts b/packages/cli/src/validate.ts index 5301623f1..27f2d7b20 100644 --- a/packages/cli/src/validate.ts +++ b/packages/cli/src/validate.ts @@ -17,6 +17,8 @@ interface ValidatorMessage { severity: number; } +const HEADER = ['code', 'message', 'severity', 'pointer']; + export async function validate(input: string, options: ValidateOptions, logger: ILogger): Promise { const [buffer, validator] = await Promise.all([fs.readFile(input), import('gltf-validator')]); return validator @@ -32,14 +34,30 @@ export async function validate(input: string, options: ValidateOptions, logger: }, }) .then(async (report: ValidatorReport) => { - await printIssueSection('error', 0, report, logger, options.format); - await printIssueSection('warning', 1, report, logger, options.format); - await printIssueSection('info', 2, report, logger, options.format); - await printIssueSection('hint', 3, report, logger, options.format); + if (options.format === TableFormat.CSV) { + await printCSV(report); + } else { + await printTable('error', 0, report, logger, options.format); + await printTable('warning', 1, report, logger, options.format); + await printTable('info', 2, report, logger, options.format); + await printTable('hint', 3, report, logger, options.format); + } + return report; + }) + .then((report: ValidatorReport) => { + if (report.issues.messages.some((message) => message.severity === 0)) { + throw new Error('Validation detected errors.'); + } }); } -async function printIssueSection( +async function printCSV(report: ValidatorReport): Promise { + const messages = report.issues.messages; + console.log(await formatTable(TableFormat.CSV, HEADER, messages.map(Object.values))); + return; +} + +async function printTable( header: string, severity: number, report: ValidatorReport, @@ -49,13 +67,7 @@ async function printIssueSection( console.log(formatHeader(header)); const messages = report.issues.messages.filter((msg) => msg.severity === severity); if (messages.length) { - console.log( - (await formatTable( - format, - ['code', 'message', 'severity', 'pointer'], - messages.map((m) => Object.values(m)), - )) + '\n\n', - ); + console.log(await formatTable(format, HEADER, messages.map(Object.values))); } else { logger.info(`No ${header}s found.`); }