Skip to content

Commit

Permalink
Implemented logic to output diagnostics log by adding additional outp…
Browse files Browse the repository at this point in the history
…ut flag
  • Loading branch information
SahilDahekar committed Nov 6, 2024
1 parent 7eb3402 commit 018bcc4
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import { red, yellow, green, cyan } from 'chalk';
import type { Diagnostic } from '@asyncapi/parser/cjs';
import type Command from './base';
import type { Specification } from './models/SpecificationFile';
import { promises } from 'fs';
import path from 'path';

const { writeFile } = promises;

type DiagnosticsFormat = 'stylish' | 'json' | 'junit' | 'html' | 'text' | 'teamcity' | 'pretty';

const formatExtensions: Record<DiagnosticsFormat, string> = {
stylish: '.txt',
json: '.json',
junit: '.xml',
html: '.html',
text: '.txt',
teamcity: '.txt',
pretty: '.txt',
};

export type SeverityKind = 'error' | 'warn' | 'info' | 'hint';

Expand Down Expand Up @@ -56,13 +72,18 @@ export function validationFlags({ logDiagnostics = true }: ValidationFlagsOption
options: ['error', 'warn', 'info', 'hint'] as const,
default: 'error',
})(),
'output': Flags.string({

Check failure on line 75 in src/core/parser.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Unnecessarily quoted property 'output' found
description: 'The output file name. Omitting this flag the result will be printed in the console.',
char: 'o'
})
};
}

export interface ValidateOptions {
'log-diagnostics'?: boolean;
'diagnostics-format'?: `${OutputFormat}`;
'fail-severity'?: SeverityKind;
'output'?: string;
}

export async function validate(command: Command, specFile: Specification, options: ValidateOptions = {}) {
Expand All @@ -86,14 +107,24 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S
if (hasFailSeverity(diagnostics, failSeverity)) {
if (logDiagnostics) {
command.logToStderr(`\n${sourceString} and/or referenced documents have governance issues.`);
command.logToStderr(formatOutput(diagnostics, diagnosticsFormat, failSeverity));
const diagnosticsOutput = formatOutput(diagnostics, diagnosticsFormat, failSeverity);
if (options.output) {
writeValidationDiagnostic(options.output, command, diagnosticsFormat, diagnosticsOutput);
} else {
command.log(diagnosticsOutput);
}
}
return ValidationStatus.INVALID;
}

if (logDiagnostics) {
command.log(`\n${sourceString} is valid but has (itself and/or referenced documents) governance issues.`);
command.log(formatOutput(diagnostics, diagnosticsFormat, failSeverity));
const diagnosticsOutput = formatOutput(diagnostics, diagnosticsFormat, failSeverity);
if (options.output) {
writeValidationDiagnostic(options.output, command, diagnosticsFormat, diagnosticsOutput);
} else {
command.log(diagnosticsOutput);
}
}
} else if (logDiagnostics) {
command.log(`\n${sourceString} is valid! ${sourceString} and referenced documents don't have governance issues.`);
Expand Down Expand Up @@ -145,3 +176,18 @@ function hasFailSeverity(diagnostics: Diagnostic[], failSeverity: SeverityKind)
const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
return diagnostics.some(diagnostic => diagnostic.severity <= diagnosticSeverity);
}

async function writeValidationDiagnostic(outputPath: string, command: Command, format: DiagnosticsFormat, formatOutput: string){

Check failure on line 180 in src/core/parser.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing space before opening brace
const expectedExtension = formatExtensions[format];

Check warning on line 181 in src/core/parser.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Variable Assigned to Object Injection Sink
const actualExtension = path.extname(outputPath);

// Validate file extension against diagnostics format
if (expectedExtension && (actualExtension !== expectedExtension)) {
command.logToStderr(`Invalid file extension for format "${format}". Expected extension: "${expectedExtension}"`);
} else {
await writeFile(path.resolve(process.cwd(), outputPath), formatOutput, {
encoding: 'utf-8',
}).catch(err => console.log(err));
}

Check failure on line 191 in src/core/parser.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Block must not be padded by blank lines

}

0 comments on commit 018bcc4

Please sign in to comment.