Skip to content

Commit

Permalink
update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Oct 18, 2022
1 parent 6f95f9c commit aa5fcab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 71 deletions.
2 changes: 1 addition & 1 deletion scripts/fetch-asyncapi-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const buildCLIListFromExamples = async () => {

try {
const { document } = await parser.parse(exampleContent);
if (!document) return; // Failed for somereason to parse this spec file (document is undefined), ignore for now
if (!document) {return;} // Failed for somereason to parse this spec file (document is undefined), ignore for now
const title = document.info().title();
const protocols = listAllProtocolsForFile(document);
return {
Expand Down
26 changes: 18 additions & 8 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,15 @@ export default class Diff extends Command {
}

try {
const { document: newFirstDocumentParsed, status: firstDocumentStatus } = await parse(this, firstDocument, flags);
const { document: newSecondDocumentParsed, status: secondDocumentStatus } = await parse(this, secondDocument, flags);
const parsed = await parseDocuments(this, firstDocument, secondDocument, flags);

if (!newFirstDocumentParsed || !newSecondDocumentParsed || firstDocumentStatus === 'invalid' || secondDocumentStatus === 'invalid') {
if (!parsed) {
return;
}

const firstDocumentParsed = convertToOldAPI(newFirstDocumentParsed);
const secondDocumentParsed = convertToOldAPI(newSecondDocumentParsed);

const diffOutput = diff.diff(
firstDocumentParsed.json(),
secondDocumentParsed.json(),
parsed.firstDocumentParsed.json(),
parsed.secondDocumentParsed.json(),
{
override: overrides,
outputType: outputFormat as diff.OutputType, // NOSONAR
Expand Down Expand Up @@ -185,6 +181,20 @@ export default class Diff extends Command {
}
}

async function parseDocuments(command: Command, firstDocument: Specification, secondDocument: Specification, flags: Record<string, any>) {
const { document: newFirstDocumentParsed, status: firstDocumentStatus } = await parse(command, firstDocument, flags);
const { document: newSecondDocumentParsed, status: secondDocumentStatus } = await parse(command, secondDocument, flags);

if (!newFirstDocumentParsed || !newSecondDocumentParsed || firstDocumentStatus === 'invalid' || secondDocumentStatus === 'invalid') {
return;
}

const firstDocumentParsed = convertToOldAPI(newFirstDocumentParsed);
const secondDocumentParsed = convertToOldAPI(newSecondDocumentParsed);

return { firstDocumentParsed, secondDocumentParsed };
}

/**
* Reads the file from give path and parses it as JSON
* @param path The path to override file
Expand Down
37 changes: 1 addition & 36 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Validate extends Command {
help: Flags.help({ char: 'h' }),
watch: watchFlag(),
...validationFlags(),
}
};

static args = [
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
Expand All @@ -31,39 +31,4 @@ export default class Validate extends Command {

await validate(this, specFile, flags);
}

// private async validate(specFile: Specification, format: `${OutputFormat}`, failSeverity: SeveritytKind) {
// const diagnostics = await parser.validate(specFile.text(), { source: specFile.getSource() });
// if (diagnostics.length) {
// if (this.hasFailSeverity(diagnostics, failSeverity)) {
// this.logToStderr(`\n${specFile.toDetails()} and/or referenced documents have governance issues.\n`);
// this.logToStderr(this.formatOutput(diagnostics, format, failSeverity));
// this.exit(1);
// } else {
// this.log(`\n${specFile.toDetails()} and/or referenced documents have governance issues.\n`);
// this.log(this.formatOutput(diagnostics, format, failSeverity));
// }
// } else {
// this.log(`\n${specFile.toDetails()} successfully validated! ${specFile.toDetails()} and referenced documents don't have governance issues.`);
// }
// }

// private formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}`, failSeverity: SeveritytKind) {
// const options = { failSeverity: getDiagnosticSeverity(failSeverity) };
// switch(format) {
// case 'stylish': return stylish(diagnostics, options);
// case 'json': return json(diagnostics, options);
// case 'junit': return junit(diagnostics, options);
// case 'html': return html(diagnostics, options);
// case 'text': return text(diagnostics, options);
// case 'teamcity': return teamcity(diagnostics, options);
// case 'pretty': return pretty(diagnostics, options);
// default: return stylish(diagnostics, options);
// }
// }

// private hasFailSeverity(diagnostics: Diagnostic[], failSeverity: SeveritytKind) {
// const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
// return diagnostics.some(diagnostic => diagnostic.severity <= diagnosticSeverity);
// }
}
43 changes: 17 additions & 26 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function validationFlags() {
options: ['error', 'warn', 'info', 'hint'],
default: 'error',
}),
}
};
}

interface ValidateOptions {
Expand All @@ -51,24 +51,18 @@ interface ValidateOptions {
'fail-severity'?: SeveritytKind;
}

interface ParseOptions {
'log-diagnostics'?: boolean;
'diagnostics-format'?: `${OutputFormat}`;
'fail-severity'?: SeveritytKind;
}

export async function validate(command: Command, specFile: Specification, options: ValidateOptions = {}) {
const diagnostics = await parser.validate(specFile.text(), { source: specFile.getSource() });
return logDiagnostics(diagnostics, command, specFile, options);
}

export async function parse(command: Command, specFile: Specification, options: ParseOptions = {}) {
export async function parse(command: Command, specFile: Specification, options: ValidateOptions = {}) {
const { document, diagnostics } = await parser.parse(specFile.text(), { source: specFile.getSource() });
const status = logDiagnostics(diagnostics, command, specFile, options);
return { document, status };
}

function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: Specification, options: ParseOptions = {}): 'valid' | 'invalid' {
function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: Specification, options: ValidateOptions = {}): 'valid' | 'invalid' {
const logDiagnostics = options['log-diagnostics'];
const failSeverity = options['fail-severity'] || 'error';
const diagnosticsFormat = options['diagnostics-format'] || 'stylish';
Expand All @@ -81,31 +75,28 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S
}
command.exit(1);
return 'invalid';
} else {
if (logDiagnostics) {
command.log(`\n${specFile.toDetails()} and/or referenced documents have governance issues.\n`);
command.log(formatOutput(diagnostics, diagnosticsFormat, failSeverity));
}
}
} else {
}
if (logDiagnostics) {
command.log(`\n${specFile.toDetails()} is valid! ${specFile.toDetails()} and referenced documents don't have governance issues.`);
command.log(`\n${specFile.toDetails()} and/or referenced documents have governance issues.\n`);
command.log(formatOutput(diagnostics, diagnosticsFormat, failSeverity));
}
} else if (logDiagnostics) {
command.log(`\n${specFile.toDetails()} is valid! ${specFile.toDetails()} and referenced documents don't have governance issues.`);
}
return 'valid';
}

function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}`, failSeverity: SeveritytKind) {
const options = { failSeverity: getDiagnosticSeverity(failSeverity) };
switch(format) {
case 'stylish': return stylish(diagnostics, options);
case 'json': return json(diagnostics, options);
case 'junit': return junit(diagnostics, options);
case 'html': return html(diagnostics, options);
case 'text': return text(diagnostics, options);
case 'teamcity': return teamcity(diagnostics, options);
case 'pretty': return pretty(diagnostics, options);
default: return stylish(diagnostics, options);
switch (format) {
case 'stylish': return stylish(diagnostics, options);
case 'json': return json(diagnostics, options);
case 'junit': return junit(diagnostics, options);
case 'html': return html(diagnostics, options);
case 'text': return text(diagnostics, options);
case 'teamcity': return teamcity(diagnostics, options);
case 'pretty': return pretty(diagnostics, options);
default: return stylish(diagnostics, options);
}
}

Expand Down

0 comments on commit aa5fcab

Please sign in to comment.