Skip to content

Commit

Permalink
feat(cli): Reformat 'validate' output as valid CSV, fail on validatio…
Browse files Browse the repository at this point in the history
…n error (#1252)
  • Loading branch information
donmccurdy authored Jan 28, 2024
1 parent 5a5a944 commit ee69988
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/cli/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const [buffer, validator] = await Promise.all([fs.readFile(input), import('gltf-validator')]);
return validator
Expand All @@ -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<void> {
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,
Expand All @@ -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.`);
}
Expand Down

0 comments on commit ee69988

Please sign in to comment.