Skip to content

Commit

Permalink
feat: support command-specific help and improve display quality (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns authored Aug 26, 2021
1 parent 06b557f commit fe64573
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 179 deletions.
81 changes: 58 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,66 @@
$ npm install --global @asyncapi/cli
```

### Getting Started
Go ahead and run command `asyncapi --help` to get complete help for using CLI. If having doubt about any particular command do run `asyncapi <command> --help` to get help for that command.

### CLI
Help string for all the supported commands

- #### `asyncapi --help`
```
USAGE
asyncapi [options] [command]
OPTIONS
-h, --help display help for command
--version output the version number
COMMANDS
validate validate asyncapi file
context Manage contexts
```

- #### `asyncapi validate --help`
```
$ asyncapi --help
Usage
$ asyncapi command options
Commands
validate
Options
-c --context context-name saved in the store
-w --watch Enable watchMode (not implemented yet)
-f --file File path of the specification file
context
current show the current set context
list show the list of all stored contexts
remove <context-name> remove a context from the store
use <context-name> set any context from store as current
add <context-name> <filepath> add/update new context
Examples
$ asyncapi context add dummy ./asyncapi.yml
$ asyncapi validate --context=dummy
$ asyncapi validate --file=./asyncapi.yml
USAGE
asyncapi validate [options]
OPTIONS
-h, --help display help for command
-f, --file <spec-file-path> Path of the asyncapi file
-c, --context <saved-context-name> context name to use
-w, --watch Enable Watch Mode (not implemented yet)
```

- #### `asyncapi context --help`
```
USAGE
asyncapi context [options] [command]
Context is what makes it easier for you to work with multiple AsyncAPI files.
You can add multiple different files to a contextThis way you do not have to pass
--file flag with path to the file every time but just --context flag with reference name
You can also set a default context, so neither --file nor --context flags are needed.
OPTIONS
-h, --help display help for command
COMMANDS
list list of all saved contexts
current see current context
use <context-name> set given context as default/current
add <context-name> <spec-file-path> add/update context
remove <context-name> remove a context
```

> For now --context flag is requried to run validate command
14 changes: 10 additions & 4 deletions src/CliModels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Command = string;
export type Command = string | undefined;
export type HelpMessage = string;
export type Arguments = string[];

Expand All @@ -12,11 +12,13 @@ export class CliInput {
private readonly _command: Command
private readonly _options: Options
private readonly _arguments: Arguments
private readonly _help: boolean | undefined

private constructor(command: Command, options: Options, args: Arguments) {
private constructor(command: Command, options: Options, args: Arguments, help?:boolean) {
this._command = command;
this._options = options;
this._arguments = args;
this._help = help;
}

get command(): Command {
Expand All @@ -31,15 +33,19 @@ export class CliInput {
return this._arguments;
}

get help(): boolean | undefined {
return this._help;
}

static createFromMeow(meowOutput: any): CliInput {
const [command, ...args] = meowOutput.input;
const { context, watch, file } = meowOutput.flags;
return new CliInput(command || 'help', { context, watch, file }, args);
return new CliInput(command, { context, watch, file }, args, meowOutput.flags.help);
}

static createSubCommand(cliInput: CliInput): CliInput {
const [command, ...args] = cliInput.arguments;
return new CliInput(command || 'help', cliInput.options, args);
return new CliInput(command, cliInput.options, args);
}
}

8 changes: 8 additions & 0 deletions src/CommandsRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import React from 'react';
import Validate from './components/Validate/Validate';
import { contextRouter } from './components/Context';
import { CliInput } from './CliModels';
import { CommandName, HelpMessageBuilder } from './help-message';

const commandsDictionary = (cliInput: CliInput): Record<string, any> => ({
validate: <Validate options={cliInput.options} />,
context: contextRouter(cliInput)
});

export const commandsRouter = (cli: any): any => {
const helpMessage = new HelpMessageBuilder();
const cliInput = CliInput.createFromMeow(cli);
if (!cliInput.command) {
return <helpMessage.HelpComponent />;
}
if (cliInput.help) {
return <helpMessage.HelpComponent command={cliInput.command as CommandName} />;
}
return commandsDictionary(cliInput)[cliInput.command];
};
27 changes: 5 additions & 22 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,8 @@ import { render } from 'ink';
import meow from 'meow';
import { commandsRouter } from './CommandsRouter';

const cli = meow(`
Usage
$ asyncapi command options
Commands
validate
Options
-c --context context-name saved in the store
-w --watch Enable watchMode (not implemented yet)
-f --file File path of the specification file
context
current show the current set context
list show the list of all stored context
remove <context-name> remove a context from the store
use <context-name> set any context as current
add <context-name> <filepath> add/update new context
Examples
$ asyncapi context add dummy ./asyncapi.yml
$ asyncapi validate --context=dummy
$ asyncapi validate --file=./asyncapi.yml
`, {
const cli = meow({
autoHelp: false,
flags: {
context: {
alias: 'c',
Expand All @@ -43,6 +23,9 @@ const cli = meow(`
alias: 'f',
type: 'string',
isRequired: false
},
help: {
alias: 'h'
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/components/Context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import React from 'react';

import { ListContexts,AddContext,RemoveContext,ShowCurrentContext,SetCurrent } from './Context';
Expand All @@ -13,5 +14,5 @@ const commandDictionary = (cliInput: CliInput): Record<string, any> => ({

export const contextRouter = (cliInput: CliInput): any => {
const subCommand = CliInput.createSubCommand(cliInput);
return commandDictionary(subCommand)[subCommand.command];
return commandDictionary(subCommand)[subCommand.command!];
};
30 changes: 0 additions & 30 deletions src/help-message.spec.ts

This file was deleted.

99 changes: 0 additions & 99 deletions src/help-message.ts

This file was deleted.

Loading

0 comments on commit fe64573

Please sign in to comment.