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..883b7878a49 --- /dev/null +++ b/src/commands/inspect.ts @@ -0,0 +1,59 @@ +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 } = 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..5ecd40fb7f4 --- /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..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; +}