From 1f5b54d16e9029c14311b3864958c123a9096881 Mon Sep 17 00:00:00 2001 From: Adi-204 Date: Tue, 14 Jan 2025 23:31:29 +0530 Subject: [PATCH 1/5] feat: inspect command to show number of components,servers and channels --- package-lock.json | 4 +- src/commands/inspect.ts | 57 ++++++++++++++++++++++++++++ src/core/flags/inspect.flags.ts | 7 ++++ src/core/utils/numberOfChannels.ts | 9 +++++ src/core/utils/numberOfComponents.ts | 9 +++++ src/core/utils/numberOfServers.ts | 9 +++++ 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/commands/inspect.ts create mode 100644 src/core/flags/inspect.flags.ts create mode 100644 src/core/utils/numberOfChannels.ts create mode 100644 src/core/utils/numberOfComponents.ts create mode 100644 src/core/utils/numberOfServers.ts diff --git a/package-lock.json b/package-lock.json index 30b14fe7b7b..9df7a061c9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@asyncapi/cli", - "version": "2.14.1", + "version": "2.15.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@asyncapi/cli", - "version": "2.14.1", + "version": "2.15.0", "license": "Apache-2.0", "dependencies": { "@asyncapi/avro-schema-parser": "^3.0.23", diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts new file mode 100644 index 00000000000..ac8d7326269 --- /dev/null +++ b/src/commands/inspect.ts @@ -0,0 +1,57 @@ +import { Args } from '@oclif/core'; +import Command from '../core/base'; +import { parse } from '../core/parser'; +import { load } from '../core/models/SpecificationFile'; +import { inspectFlags } from '../core/flags/inspect.flags'; +import { proxyFlags } from '../core/flags/proxy.flags'; +import { numberOfChannels } from '../core/utils/numberOfChannels'; +import { numberOfServers } from '../core/utils/numberOfServers'; +import { ValidationError } from '../core/errors/validation-error'; +import { numberOfComponents } from '../core/utils/numberOfComponents'; + +export default class Inspect extends Command { + static description = 'Show the number of servers, channels, and components in AsyncAPI files'; + + static flags = { + ...inspectFlags(), + ...proxyFlags(), // Merge proxyFlags with validateFlags + }; + + static args = { + 'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }), + proxyHost: Args.string({ description: 'Name of the Proxy Host', required: false }), + proxyPort: Args.string({ description: 'Name of the Port of the ProxyHost', required: false }), + }; + + async run() { + const { args, flags } = await this.parse(Inspect); + let filePath = args['spec-file']; + const proxyHost = flags['proxyHost']; + const proxyPort = flags['proxyPort']; + if (proxyHost && proxyPort) { + const proxyUrl = `http://${proxyHost}:${proxyPort}`; + filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl + } + try { + this.specFile = await load(filePath); + } catch (err: any) { + if (err.message.includes('Failed to download')) { + throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.'); + } else { + this.error( + new ValidationError({ + type: 'invalid-file', + filepath: filePath, + }) + ); + } + } + let { document } = await parse(this, this.specFile); + const channels = await numberOfChannels(document); + const servers = await numberOfServers(document); + const components = await numberOfComponents(document); + this.log(`The total number of Servers in asyncapi document is ${servers}`); + this.log(`The total number of Channels in asyncapi document is ${channels}`); + this.log(`The total number of Components in asyncapi document is ${components}`); + } +} diff --git a/src/core/flags/inspect.flags.ts b/src/core/flags/inspect.flags.ts new file mode 100644 index 00000000000..ffb7e09e2e8 --- /dev/null +++ b/src/core/flags/inspect.flags.ts @@ -0,0 +1,7 @@ +import { Flags } from "@oclif/core"; + +export const inspectFlags = () => { + return { + help: Flags.help({ char: 'h' }), + }; +}; diff --git a/src/core/utils/numberOfChannels.ts b/src/core/utils/numberOfChannels.ts new file mode 100644 index 00000000000..d4cfd3081cc --- /dev/null +++ b/src/core/utils/numberOfChannels.ts @@ -0,0 +1,9 @@ +import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; + +export async function numberOfChannels(document: AsyncAPIDocumentInterface | undefined) { + let countChannels = 0; + if (document?.channels().length) { + countChannels = document?.channels().length; + } + return countChannels; +} diff --git a/src/core/utils/numberOfComponents.ts b/src/core/utils/numberOfComponents.ts new file mode 100644 index 00000000000..2f8ab725e33 --- /dev/null +++ b/src/core/utils/numberOfComponents.ts @@ -0,0 +1,9 @@ +import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; + +export async function numberOfComponents(document: AsyncAPIDocumentInterface | undefined) { + let countComponents = 0; + if (document?.components().json()) { + countComponents = document?.components().json.length; + } + return countComponents; +} diff --git a/src/core/utils/numberOfServers.ts b/src/core/utils/numberOfServers.ts new file mode 100644 index 00000000000..3f7d5df88af --- /dev/null +++ b/src/core/utils/numberOfServers.ts @@ -0,0 +1,9 @@ +import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; + +export async function numberOfServers(document: AsyncAPIDocumentInterface | undefined) { + let countServers = 0; + if (document?.servers().length) { + countServers = document?.servers().length; + } + return countServers; +} From ebc8af922401689871f969c75e3221159f17260f Mon Sep 17 00:00:00 2001 From: Adi-204 Date: Tue, 14 Jan 2025 23:51:18 +0530 Subject: [PATCH 2/5] fix: quality gate issue --- src/commands/inspect.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts index ac8d7326269..5897ec84018 100644 --- a/src/commands/inspect.ts +++ b/src/commands/inspect.ts @@ -10,14 +10,14 @@ import { ValidationError } from '../core/errors/validation-error'; import { numberOfComponents } from '../core/utils/numberOfComponents'; export default class Inspect extends Command { - static description = 'Show the number of servers, channels, and components in AsyncAPI files'; + static readonly description = 'Show the number of servers, channels, and components in AsyncAPI files'; - static flags = { + static readonly flags = { ...inspectFlags(), ...proxyFlags(), // Merge proxyFlags with validateFlags }; - static args = { + static readonly args = { 'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }), proxyHost: Args.string({ description: 'Name of the Proxy Host', required: false }), proxyPort: Args.string({ description: 'Name of the Port of the ProxyHost', required: false }), From 8dabafec0923559ac705a7d565838d58016b68f3 Mon Sep 17 00:00:00 2001 From: Adi-204 Date: Thu, 16 Jan 2025 00:07:10 +0530 Subject: [PATCH 3/5] fix: Changes in number of components calculation --- src/commands/inspect.ts | 24 +++++++++++++----------- src/core/utils/numberOfComponents.ts | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts index 5897ec84018..883b7878a49 100644 --- a/src/commands/inspect.ts +++ b/src/commands/inspect.ts @@ -14,7 +14,7 @@ export default class Inspect extends Command { static readonly flags = { ...inspectFlags(), - ...proxyFlags(), // Merge proxyFlags with validateFlags + ...proxyFlags(), }; static readonly args = { @@ -25,28 +25,30 @@ export default class Inspect extends Command { async run() { const { args, flags } = await this.parse(Inspect); + let filePath = args['spec-file']; + const proxyHost = flags['proxyHost']; + const proxyPort = flags['proxyPort']; + if (proxyHost && proxyPort) { const proxyUrl = `http://${proxyHost}:${proxyPort}`; - filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl + filePath = `${filePath}+${proxyUrl}`; } + try { this.specFile = await load(filePath); - } catch (err: any) { + } + catch (err: any) { if (err.message.includes('Failed to download')) { throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.'); - } else { - this.error( - new ValidationError({ - type: 'invalid-file', - filepath: filePath, - }) - ); + } + else { + this.error(new ValidationError({type: 'invalid-file',filepath: filePath})); } } - let { document } = await parse(this, this.specFile); + const { document } = await parse(this, this.specFile); const channels = await numberOfChannels(document); const servers = await numberOfServers(document); const components = await numberOfComponents(document); diff --git a/src/core/utils/numberOfComponents.ts b/src/core/utils/numberOfComponents.ts index 2f8ab725e33..5ecd40fb7f4 100644 --- a/src/core/utils/numberOfComponents.ts +++ b/src/core/utils/numberOfComponents.ts @@ -3,7 +3,7 @@ import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; export async function numberOfComponents(document: AsyncAPIDocumentInterface | undefined) { let countComponents = 0; if (document?.components().json()) { - countComponents = document?.components().json.length; + countComponents = Object.keys(document?.components().json()).length; } return countComponents; } From 4955a753f40714a3f115b0bd67371e1ba0e51db5 Mon Sep 17 00:00:00 2001 From: Adi-204 Date: Fri, 24 Jan 2025 15:01:48 +0530 Subject: [PATCH 4/5] fix: added file parsing error handling --- src/commands/inspect.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts index 883b7878a49..96d3d6b3a1a 100644 --- a/src/commands/inspect.ts +++ b/src/commands/inspect.ts @@ -45,10 +45,14 @@ export default class Inspect extends Command { throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.'); } else { - this.error(new ValidationError({type: 'invalid-file',filepath: filePath})); + this.error(new ValidationError({ type: 'invalid-file', filepath: filePath })); } } - const { document } = await parse(this, this.specFile); + const { document, status } = await parse(this, this.specFile); + if (!document || status === 'invalid') { + this.log('Input is not a correct AsyncAPI document so it cannot be processed.'); + return; + } const channels = await numberOfChannels(document); const servers = await numberOfServers(document); const components = await numberOfComponents(document); From 877fd15471e16a056c764d316db5ecc6584732bf Mon Sep 17 00:00:00 2001 From: Adi-204 Date: Fri, 24 Jan 2025 15:09:45 +0530 Subject: [PATCH 5/5] fix: lint issues --- src/commands/inspect.ts | 96 ++++++++++++++-------------- src/core/flags/inspect.flags.ts | 2 +- src/core/utils/numberOfChannels.ts | 10 +-- src/core/utils/numberOfComponents.ts | 10 +-- src/core/utils/numberOfServers.ts | 10 +-- 5 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts index 96d3d6b3a1a..0238e13b197 100644 --- a/src/commands/inspect.ts +++ b/src/commands/inspect.ts @@ -10,54 +10,52 @@ import { ValidationError } from '../core/errors/validation-error'; import { numberOfComponents } from '../core/utils/numberOfComponents'; export default class Inspect extends Command { - static readonly description = 'Show the number of servers, channels, and components in AsyncAPI files'; - - static readonly flags = { - ...inspectFlags(), - ...proxyFlags(), - }; - - static readonly args = { - 'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }), - proxyHost: Args.string({ description: 'Name of the Proxy Host', required: false }), - proxyPort: Args.string({ description: 'Name of the Port of the ProxyHost', required: false }), - }; - - async run() { - const { args, flags } = await this.parse(Inspect); - - let filePath = args['spec-file']; - - const proxyHost = flags['proxyHost']; - - const proxyPort = flags['proxyPort']; - - if (proxyHost && proxyPort) { - const proxyUrl = `http://${proxyHost}:${proxyPort}`; - filePath = `${filePath}+${proxyUrl}`; - } - - try { - this.specFile = await load(filePath); - } - catch (err: any) { - if (err.message.includes('Failed to download')) { - throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.'); - } - else { - this.error(new ValidationError({ type: 'invalid-file', filepath: filePath })); - } - } - const { document, status } = await parse(this, this.specFile); - if (!document || status === 'invalid') { - this.log('Input is not a correct AsyncAPI document so it cannot be processed.'); - return; - } - const channels = await numberOfChannels(document); - const servers = await numberOfServers(document); - const components = await numberOfComponents(document); - this.log(`The total number of Servers in asyncapi document is ${servers}`); - this.log(`The total number of Channels in asyncapi document is ${channels}`); - this.log(`The total number of Components in asyncapi document is ${components}`); + static readonly description = 'Show the number of servers, channels, and components in AsyncAPI files'; + + static readonly flags = { + ...inspectFlags(), + ...proxyFlags(), + }; + + static readonly args = { + 'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }), + proxyHost: Args.string({ description: 'Name of the Proxy Host', required: false }), + proxyPort: Args.string({ description: 'Name of the Port of the ProxyHost', required: false }), + }; + + async run() { + const { args, flags } = await this.parse(Inspect); + + let filePath = args['spec-file']; + + const proxyHost = flags['proxyHost']; + + const proxyPort = flags['proxyPort']; + + if (proxyHost && proxyPort) { + const proxyUrl = `http://${proxyHost}:${proxyPort}`; + filePath = `${filePath}+${proxyUrl}`; + } + + try { + this.specFile = await load(filePath); + } catch (err: any) { + if (err.message.includes('Failed to download')) { + throw new Error('Proxy Connection Error: Unable to establish a connection to the proxy check hostName or PortNumber.'); + } else { + this.error(new ValidationError({ type: 'invalid-file', filepath: filePath })); + } + } + const { document, status } = await parse(this, this.specFile); + if (!document || status === 'invalid') { + this.log('Input is not a correct AsyncAPI document so it cannot be processed.'); + return; } + const channels = await numberOfChannels(document); + const servers = await numberOfServers(document); + const components = await numberOfComponents(document); + this.log(`The total number of Servers in asyncapi document is ${servers}`); + this.log(`The total number of Channels in asyncapi document is ${channels}`); + this.log(`The total number of Components in asyncapi document is ${components}`); + } } diff --git a/src/core/flags/inspect.flags.ts b/src/core/flags/inspect.flags.ts index ffb7e09e2e8..cd4bbc96fae 100644 --- a/src/core/flags/inspect.flags.ts +++ b/src/core/flags/inspect.flags.ts @@ -1,4 +1,4 @@ -import { Flags } from "@oclif/core"; +import { Flags } from '@oclif/core'; export const inspectFlags = () => { return { diff --git a/src/core/utils/numberOfChannels.ts b/src/core/utils/numberOfChannels.ts index d4cfd3081cc..ec193694e80 100644 --- a/src/core/utils/numberOfChannels.ts +++ b/src/core/utils/numberOfChannels.ts @@ -1,9 +1,9 @@ import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; export async function numberOfChannels(document: AsyncAPIDocumentInterface | undefined) { - let countChannels = 0; - if (document?.channels().length) { - countChannels = document?.channels().length; - } - return countChannels; + let countChannels = 0; + if (document?.channels().length) { + countChannels = document?.channels().length; + } + return countChannels; } diff --git a/src/core/utils/numberOfComponents.ts b/src/core/utils/numberOfComponents.ts index 5ecd40fb7f4..64d6343a613 100644 --- a/src/core/utils/numberOfComponents.ts +++ b/src/core/utils/numberOfComponents.ts @@ -1,9 +1,9 @@ import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; export async function numberOfComponents(document: AsyncAPIDocumentInterface | undefined) { - let countComponents = 0; - if (document?.components().json()) { - countComponents = Object.keys(document?.components().json()).length; - } - return countComponents; + let countComponents = 0; + if (document?.components().json()) { + countComponents = Object.keys(document?.components().json()).length; + } + return countComponents; } diff --git a/src/core/utils/numberOfServers.ts b/src/core/utils/numberOfServers.ts index 3f7d5df88af..96d3a963d6b 100644 --- a/src/core/utils/numberOfServers.ts +++ b/src/core/utils/numberOfServers.ts @@ -1,9 +1,9 @@ import { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs/models'; export async function numberOfServers(document: AsyncAPIDocumentInterface | undefined) { - let countServers = 0; - if (document?.servers().length) { - countServers = document?.servers().length; - } - return countServers; + let countServers = 0; + if (document?.servers().length) { + countServers = document?.servers().length; + } + return countServers; }