Skip to content

Commit

Permalink
feat: inspect command to show number of components,servers and channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi-204 committed Jan 14, 2025
1 parent 4b73ebd commit 1f5b54d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/commands/inspect.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
7 changes: 7 additions & 0 deletions src/core/flags/inspect.flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Flags } from "@oclif/core";

export const inspectFlags = () => {
return {
help: Flags.help({ char: 'h' }),
};
};
9 changes: 9 additions & 0 deletions src/core/utils/numberOfChannels.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions src/core/utils/numberOfComponents.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions src/core/utils/numberOfServers.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 1f5b54d

Please sign in to comment.