diff --git a/src/commands/inspect.ts b/src/commands/inspect.ts new file mode 100644 index 00000000000..0238e13b197 --- /dev/null +++ b/src/commands/inspect.ts @@ -0,0 +1,61 @@ +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 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 new file mode 100644 index 00000000000..cd4bbc96fae --- /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..ec193694e80 --- /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..64d6343a613 --- /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 = Object.keys(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..96d3a963d6b --- /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; +}