From d663e8a785f7949c91ad4e00633af5188bf9205c Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 27 Jul 2021 16:52:28 +0530 Subject: [PATCH 01/17] chore: made the cli error messages constant --- src/hooks/context/models.ts | 13 +++++++------ src/messages.tsx | 11 +++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 src/messages.tsx diff --git a/src/hooks/context/models.ts b/src/hooks/context/models.ts index 06e728cbafa..77a944c5c0d 100644 --- a/src/hooks/context/models.ts +++ b/src/hooks/context/models.ts @@ -1,3 +1,4 @@ +import * as messages from '../../messages'; export interface Context { current: string, store: { @@ -15,41 +16,41 @@ export class SpecFileNotFoundError extends Error { export class ContextFileNotFoundError extends Error { constructor() { super(); - this.message = 'No contexts saved yet, run asyncapi --help to know more.'; + this.message = messages.NO_CONTEXTS_SAVED; } } export class ContextNotFoundError extends Error { constructor() { super(); - this.message = 'This context key does not exist.'; + this.message = messages.CONTEXT_NOT_FOUND; } } export class KeyNotFoundError extends Error { constructor() { super(); - this.message = 'Key not found.'; + this.message = messages.KEY_NOT_FOUND; } } export class DeletingCurrentContextError extends Error { constructor() { super(); - this.message = 'You are trying to delete a context that is currently in use.'; + this.message = messages.DELETING_CURRENT_CONTEXT; } } export class MissingCurrentContextError extends Error { constructor() { super(); - this.message = 'No context is set as current, please set a current context.'; + this.message = messages.MISSING_CURRENT_CONTEXT; } } export class MissingArgumentstError extends Error { constructor() { super(); - this.message = 'Missing arguments'; + this.message = messages.MISSING_ARGUMENTS; } } diff --git a/src/messages.tsx b/src/messages.tsx new file mode 100644 index 00000000000..31363443d5d --- /dev/null +++ b/src/messages.tsx @@ -0,0 +1,11 @@ +export const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to know more'; + +export const CONTEXT_NOT_FOUND = 'This context key does not exists.'; + +export const KEY_NOT_FOUND = 'Key not found'; + +export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that is currently in use.'; + +export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.'; + +export const MISSING_ARGUMENTS = 'Missing arguments.'; From 89a5881bc3b40afceea0f8099b9132634d31c6aa Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 27 Jul 2021 20:27:46 +0530 Subject: [PATCH 02/17] feat: created help command constant --- src/messages.tsx | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/messages.tsx b/src/messages.tsx index 31363443d5d..2c3c6f1af8f 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -9,3 +9,40 @@ export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.'; export const MISSING_ARGUMENTS = 'Missing arguments.'; + +const HELP_FLAG = '-h, --help display help for command'; + +export const Help = { + root: { + usage: 'asyncapi [options] [command]', + shortHelp: '', + options: [ + '-v, --version output the version number', + HELP_FLAG + ] + }, + validate: { + usage: 'asyncapi validate [options]', + options: [ + '-f, --file Path of the spec file', + '-c, --context context name to use', + '-w, --watch watch mode', + HELP_FLAG + ] + }, + context: { + usage: 'asyncapi context [options] [command]', + shortDesc: 'Manage contexts', + longDesc: 'Store key-value pair of contexts and the CLI will load your contexts \n automatically making your commands shorter', + options: [ + HELP_FLAG + ], + commands: [ + 'list list all saved commands', + 'current display current context in use', + 'use set context as current context', + 'add add/update context', + 'remove remove a context' + ] + } +}; From 8d556d5590c461ea409203a00ade61bfc7a5ea51 Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 27 Jul 2021 20:38:00 +0530 Subject: [PATCH 03/17] chore: added desc for validate command --- src/messages.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/messages.tsx b/src/messages.tsx index 2c3c6f1af8f..5b8b42292a8 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -15,7 +15,6 @@ const HELP_FLAG = '-h, --help display help for command'; export const Help = { root: { usage: 'asyncapi [options] [command]', - shortHelp: '', options: [ '-v, --version output the version number', HELP_FLAG @@ -23,6 +22,7 @@ export const Help = { }, validate: { usage: 'asyncapi validate [options]', + shortDesc: 'Validate an AsyncApi file', options: [ '-f, --file Path of the spec file', '-c, --context context name to use', From 2bb4ebdd236be2e98a9c9be12d7bc9da61f29b82 Mon Sep 17 00:00:00 2001 From: Souvik Date: Wed, 28 Jul 2021 13:08:48 +0530 Subject: [PATCH 04/17] feat: created help class to generate help messages --- src/messages.tsx | 80 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/src/messages.tsx b/src/messages.tsx index 5b8b42292a8..d7772c4de7f 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -10,39 +10,69 @@ export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set export const MISSING_ARGUMENTS = 'Missing arguments.'; -const HELP_FLAG = '-h, --help display help for command'; +export interface CommandHelp { + name: string, + usage: string, + shortDescription?: string, + longDescription?: string, + flags?: string[], + commands?: string[] +} -export const Help = { - root: { +export class Help { + private _helpFlag = '-h, --help display help for command'; + private root: CommandHelp = { + name: 'root', usage: 'asyncapi [options] [command]', - options: [ - '-v, --version output the version number', - HELP_FLAG + flags: [ + this._helpFlag, + '-v, -version output the version number' ] - }, - validate: { + } + + private validate: CommandHelp = { + name: 'validate', usage: 'asyncapi validate [options]', - shortDesc: 'Validate an AsyncApi file', - options: [ - '-f, --file Path of the spec file', - '-c, --context context name to use', + shortDescription: 'Validate an asyncapi file', + flags: [ + '-f, --file Path of the spec file', + '-c, --context Context name to use', '-w, --watch watch mode', - HELP_FLAG + this._helpFlag ] - }, - context: { + } + + private context: CommandHelp = { + name: 'context', usage: 'asyncapi context [options] [command]', - shortDesc: 'Manage contexts', - longDesc: 'Store key-value pair of contexts and the CLI will load your contexts \n automatically making your commands shorter', - options: [ - HELP_FLAG + shortDescription: 'Manage contexts', + flags: [ + this._helpFlag ], commands: [ - 'list list all saved commands', - 'current display current context in use', - 'use set context as current context', - 'add add/update context', - 'remove remove a context' + 'list list all saved contexts', + 'current see current context', + 'use set a context as current', + 'add add/update context', + 'remove remove a context' ] } -}; + + rootHelp(): string { + let helpString = ''; + helpString += `usage: ${this.root.usage}\n\n`; + helpString += 'options:\n'; + if (this.root.flags) { + for (const flag of this.root.flags) { + helpString += ` ${flag} \n`; + } + } + + helpString += '\n'; + + helpString += 'commads:\n'; + helpString += ` context [options] [command] ${this.context.shortDescription} \n`; + helpString += ` validate [options] ${this.validate.shortDescription}\n`; + return helpString; + } +} From 764baff46eeba9e0c418a0465495aea8381623c6 Mon Sep 17 00:00:00 2001 From: Souvik Date: Wed, 28 Jul 2021 19:53:49 +0530 Subject: [PATCH 05/17] feat: changed order of usage params --- src/messages.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/messages.tsx b/src/messages.tsx index d7772c4de7f..25bba99ef57 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -44,7 +44,7 @@ export class Help { private context: CommandHelp = { name: 'context', - usage: 'asyncapi context [options] [command]', + usage: 'asyncapi context [command] [options]', shortDescription: 'Manage contexts', flags: [ this._helpFlag @@ -71,7 +71,7 @@ export class Help { helpString += '\n'; helpString += 'commads:\n'; - helpString += ` context [options] [command] ${this.context.shortDescription} \n`; + helpString += ` context [command] [options] ${this.context.shortDescription} \n`; helpString += ` validate [options] ${this.validate.shortDescription}\n`; return helpString; } From 0e0575d428fbcfcee9768447bb4859dfadf4f581 Mon Sep 17 00:00:00 2001 From: Souvik Date: Wed, 28 Jul 2021 19:55:50 +0530 Subject: [PATCH 06/17] feat: added long description for context --- src/messages.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/messages.tsx b/src/messages.tsx index 25bba99ef57..79e2ffb4d64 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -46,6 +46,7 @@ export class Help { name: 'context', usage: 'asyncapi context [command] [options]', shortDescription: 'Manage contexts', + longDescription: 'Context is what makes it easier for you to work with multiple AsyncAPI files. You can add multiple different files to a context. This 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', flags: [ this._helpFlag ], From df0666773e4b61d659d345f98d2de9d46949fce7 Mon Sep 17 00:00:00 2001 From: Souvik Date: Thu, 29 Jul 2021 14:28:47 +0530 Subject: [PATCH 07/17] feat: created new help message builder and added tests --- src/help-message.spec.ts | 30 ++++++++++++ src/help-message.ts | 100 +++++++++++++++++++++++++++++++++++++++ src/messages.tsx | 68 -------------------------- 3 files changed, 130 insertions(+), 68 deletions(-) create mode 100644 src/help-message.spec.ts create mode 100644 src/help-message.ts diff --git a/src/help-message.spec.ts b/src/help-message.spec.ts new file mode 100644 index 00000000000..76ccf227bf4 --- /dev/null +++ b/src/help-message.spec.ts @@ -0,0 +1,30 @@ +import { HelpMessageBuilder } from './help-message'; + +let helpBuilder: HelpMessageBuilder; + +describe('HelpMessageBuilder should', () => { + beforeAll(() => { + helpBuilder = new HelpMessageBuilder(); + }); + it('return root Help message', () => { + expect(typeof helpBuilder.showHelp()).toMatch('string'); + expect(helpBuilder.showHelp()).toMatch( + 'usage: asyncapi [options] [command]\n\n'+ + 'flags:\n'+ + ' -h, --help display help for command\n'+ + ' -v, --version output the version number\n'+ + '\n'+ + 'commands:\n'+ + ' validate [options] [command] Validate asyncapi file\n'+ + ' context [options] [command] Manage context\n' + ); + }); + + it('return validate help message', () => { + expect(typeof helpBuilder.showCommandHelp('validate')).toMatch('string'); + }); + + it('return context help message', () => { + expect(typeof helpBuilder.showCommandHelp('context')).toMatch('string'); + }); +}); diff --git a/src/help-message.ts b/src/help-message.ts new file mode 100644 index 00000000000..bceb20852f6 --- /dev/null +++ b/src/help-message.ts @@ -0,0 +1,100 @@ +import { injectable, container } from 'tsyringe'; + +export type CommandName = 'validate' | 'context'; + +export type Command = { + [name in CommandName]: { + usage: string; + shortDescription: string; + longDescription?: string; + flags: string[]; + subCommands?: string[]; + }; +}; + +@injectable() +export class HelpMessage { + private helpFlag = '-h, --help display help for command'; + + readonly usage: string = 'asyncapi [options] [command]'; + + readonly flags = [ + this.helpFlag, + '-v, --version output the version number', + ]; + + readonly commands: Command = { + validate: { + usage: 'asyncapi validate [options]', + shortDescription: 'Validate asyncapi file', + flags: [ + this.helpFlag, + '-f, --file Path of the spec file', + '-c, --context Context to use', + '-w, --watch Watch mode' + ] + }, + context: { + usage: 'asyncapi context [command] [options]', + shortDescription: 'Manage context', + longDescription: 'Context is what makes it easier for you to work with multiple AsyncAPI files.\nYou can add multiple different files to a context.\nThis way you do not have to pass --file flag with path to the file every time but just --context flag with reference name.\nYou can also set a default context, so neither --file nor --context flags are needed', + flags: [this.helpFlag], + subCommands: [ + 'list list all saved contexts', + 'current see current context', + 'use set a context as current', + 'add add/update context', + 'remove remove a context' + ] + } + } +} + +export class HelpMessageBuilder { + private helpMessage: HelpMessage = container.resolve(HelpMessage); + + showHelp() { + let helpText = ''; + helpText += `usage: ${this.helpMessage.usage}\n\n`; + helpText += 'flags:\n'; + for (const flag of this.helpMessage.flags) { + helpText += ` ${flag}\n`; + } + helpText += '\n'; + + if (this.helpMessage.commands) { + helpText += 'commands:\n'; + for (const [name, obj] of Object.entries(this.helpMessage.commands)) { + helpText += ` ${name} [options] [command] ${obj.shortDescription}\n`; + } + } + + return helpText; + } + + showCommandHelp(command: CommandName) { + let helpText = ''; + // eslint-disable-next-line security/detect-object-injection + const commandHelpObject = this.helpMessage.commands[command]; + helpText += `usage: ${commandHelpObject.usage}\n\n`; + + if (commandHelpObject.longDescription) { + helpText += `${commandHelpObject.longDescription}\n\n`; + } + + helpText += 'flags: \n'; + for (const flag of commandHelpObject.flags) { + helpText += ` ${flag}\n`; + } + + if (commandHelpObject.subCommands) { + helpText += '\n'; + helpText += 'commands:\n'; + for (const command of commandHelpObject.subCommands) { + helpText += ` ${command}\n`; + } + } + + return helpText; + } +} diff --git a/src/messages.tsx b/src/messages.tsx index 79e2ffb4d64..31363443d5d 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -9,71 +9,3 @@ export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.'; export const MISSING_ARGUMENTS = 'Missing arguments.'; - -export interface CommandHelp { - name: string, - usage: string, - shortDescription?: string, - longDescription?: string, - flags?: string[], - commands?: string[] -} - -export class Help { - private _helpFlag = '-h, --help display help for command'; - private root: CommandHelp = { - name: 'root', - usage: 'asyncapi [options] [command]', - flags: [ - this._helpFlag, - '-v, -version output the version number' - ] - } - - private validate: CommandHelp = { - name: 'validate', - usage: 'asyncapi validate [options]', - shortDescription: 'Validate an asyncapi file', - flags: [ - '-f, --file Path of the spec file', - '-c, --context Context name to use', - '-w, --watch watch mode', - this._helpFlag - ] - } - - private context: CommandHelp = { - name: 'context', - usage: 'asyncapi context [command] [options]', - shortDescription: 'Manage contexts', - longDescription: 'Context is what makes it easier for you to work with multiple AsyncAPI files. You can add multiple different files to a context. This 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', - flags: [ - this._helpFlag - ], - commands: [ - 'list list all saved contexts', - 'current see current context', - 'use set a context as current', - 'add add/update context', - 'remove remove a context' - ] - } - - rootHelp(): string { - let helpString = ''; - helpString += `usage: ${this.root.usage}\n\n`; - helpString += 'options:\n'; - if (this.root.flags) { - for (const flag of this.root.flags) { - helpString += ` ${flag} \n`; - } - } - - helpString += '\n'; - - helpString += 'commads:\n'; - helpString += ` context [command] [options] ${this.context.shortDescription} \n`; - helpString += ` validate [options] ${this.validate.shortDescription}\n`; - return helpString; - } -} From 9e154bc3473f33886a52688ac05dcce2384797b6 Mon Sep 17 00:00:00 2001 From: Souvik Date: Thu, 29 Jul 2021 16:56:19 +0530 Subject: [PATCH 08/17] feat: made changes as suggested. --- src/components/Context/context.spec.tsx | 7 ++++--- src/components/Context/contexterror.tsx | 14 -------------- src/help-message.ts | 3 +-- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/components/Context/context.spec.tsx b/src/components/Context/context.spec.tsx index 1c4592e6e10..1940e08733a 100644 --- a/src/components/Context/context.spec.tsx +++ b/src/components/Context/context.spec.tsx @@ -2,6 +2,7 @@ import React from 'react'; import { render } from 'ink-testing-library'; import { ListContexts, ShowCurrentContext, AddContext, SetCurrent } from './Context'; import { ContextTestingHelper } from '../../constants'; +import * as messages from '../../messages'; const testing = new ContextTestingHelper(); @@ -9,7 +10,7 @@ describe('listing contexts', () => { test('should render error when no context file found', () => { testing.deleteDummyContextFile(); const { lastFrame } = render(); - expect(lastFrame()).toMatch('No contexts saved yet.'); + expect(lastFrame()).toMatch(messages.NO_CONTEXTS_SAVED); }); test('Should render the context list', () => { @@ -27,7 +28,7 @@ describe('rendering current context', () => { testing.deleteDummyContextFile(); const { lastFrame } = render(); const message = lastFrame(); - expect(message).toMatch('No contexts saved yet.'); + expect(message).toMatch(messages.NO_CONTEXTS_SAVED); }); test('showing current context ', () => { @@ -49,7 +50,7 @@ describe('SetContext ', () => { test('Should render error message is key is not in store', () => { testing.createDummyContextFile(); const { lastFrame } = render(); - expect(lastFrame()).toMatch('The context you are trying to use is not present'); + expect(lastFrame()).toMatch(messages.KEY_NOT_FOUND); }); test('Should render the update context', () => { diff --git a/src/components/Context/contexterror.tsx b/src/components/Context/contexterror.tsx index db5c8545229..0c4cca4f867 100644 --- a/src/components/Context/contexterror.tsx +++ b/src/components/Context/contexterror.tsx @@ -1,21 +1,7 @@ import React from 'react'; import { Text } from 'ink'; -import { ContextFileNotFoundError, DeletingCurrentContextError, KeyNotFoundError } from '../../hooks/context'; - const ContextError: React.FunctionComponent<{ error: Error }> = ({ error }) => { - if (error instanceof ContextFileNotFoundError) { - return No contexts saved yet.; - } - - if (error instanceof KeyNotFoundError) { - return The context you are trying to use is not present; - } - - if (error instanceof DeletingCurrentContextError) { - return You are trying to delete a context that is set as current.; - } - return {error.message}; }; diff --git a/src/help-message.ts b/src/help-message.ts index bceb20852f6..f7ad17970c8 100644 --- a/src/help-message.ts +++ b/src/help-message.ts @@ -74,8 +74,7 @@ export class HelpMessageBuilder { showCommandHelp(command: CommandName) { let helpText = ''; - // eslint-disable-next-line security/detect-object-injection - const commandHelpObject = this.helpMessage.commands[command]; + const commandHelpObject = this.helpMessage.commands[command as CommandName]; helpText += `usage: ${commandHelpObject.usage}\n\n`; if (commandHelpObject.longDescription) { From d5681487603352da93658cd922057d8b9dec122a Mon Sep 17 00:00:00 2001 From: Souvik Date: Thu, 29 Jul 2021 17:08:41 +0530 Subject: [PATCH 09/17] refactor: updated constant messages with literals --- src/components/Context/context.spec.tsx | 2 +- src/hooks/context/hook.spec.ts | 5 +++-- src/hooks/context/hooks.tsx | 5 +++-- src/messages.tsx | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/components/Context/context.spec.tsx b/src/components/Context/context.spec.tsx index 1940e08733a..4b1b73b19c8 100644 --- a/src/components/Context/context.spec.tsx +++ b/src/components/Context/context.spec.tsx @@ -42,7 +42,7 @@ describe('AddContext ', () => { test('should return message', () => { testing.createDummyContextFile(); const { lastFrame } = render(); - expect(lastFrame()).toMatch('New context added'); + expect(lastFrame()).toMatch(messages.NEW_CONTEXT_ADDED); }); }); diff --git a/src/hooks/context/hook.spec.ts b/src/hooks/context/hook.spec.ts index 6546cb64dab..70bcef469ed 100644 --- a/src/hooks/context/hook.spec.ts +++ b/src/hooks/context/hook.spec.ts @@ -2,6 +2,7 @@ import { useContextFile, useSpecfile } from './hooks'; import { ContextFileNotFoundError, KeyNotFoundError, ContextNotFoundError } from './models'; import { ContextTestingHelper } from '../../constants'; import { SpecificationFile } from '../validation'; +import * as messages from '../../messages'; const testingVariables = new ContextTestingHelper(); @@ -42,7 +43,7 @@ describe('useContextFile().addContext ', () => { testingVariables.deleteDummyContextFile(); const { response, error } = useContextFile().addContext('home', new SpecificationFile('./test/specification.yml')); expect(error).toBeUndefined(); - expect(response).toMatch('New context added'); + expect(response).toMatch(messages.NEW_CONTEXT_ADDED); testingVariables.deleteDummyContextFile(); }); @@ -50,7 +51,7 @@ describe('useContextFile().addContext ', () => { testingVariables.createDummyContextFile(); const { response, error } = useContextFile().addContext('home', new SpecificationFile('./test/specification.yml')); expect(error).toBeUndefined(); - expect(response).toMatch('New context added'); + expect(response).toMatch(messages.NEW_CONTEXT_ADDED); }); test('Auto set current when when adding context for the fist time', () => { diff --git a/src/hooks/context/hooks.tsx b/src/hooks/context/hooks.tsx index 5104fa956af..dddfd3c2430 100644 --- a/src/hooks/context/hooks.tsx +++ b/src/hooks/context/hooks.tsx @@ -2,6 +2,7 @@ import { Context, ContextFileNotFoundError, ContextNotFoundError, MissingCurrent import { ContextService } from './contextService'; import { container } from 'tsyringe'; import { SpecificationFile } from '../validation'; +import * as messages from '../../messages'; export type Result = { response?: any, @@ -36,14 +37,14 @@ export const useContextFile = (): any => { const ctx = contextService.loadContextFile(); const updatedContext = contextService.addContext(ctx, key, specFile); contextService.save(updatedContext); - const response = 'New context added'; + const response = messages.NEW_CONTEXT_ADDED; return { response }; } catch (error) { if (error instanceof ContextFileNotFoundError) { const context: Context = { current: '', store: {} }; const newContext = contextService.addContext(context, key, specFile); contextService.save(contextService.updateCurrent(newContext, key)); - const response = 'New context added'; + const response = messages.NEW_CONTEXT_ADDED; return { response }; } return { error }; diff --git a/src/messages.tsx b/src/messages.tsx index 31363443d5d..01264737892 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -9,3 +9,5 @@ export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.'; export const MISSING_ARGUMENTS = 'Missing arguments.'; + +export const NEW_CONTEXT_ADDED = 'New context added.'; From b0a7d6f87144ca217ed5653b8b3b5aded2ef5a05 Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 3 Aug 2021 14:56:51 +0530 Subject: [PATCH 10/17] feat: added delete messages in constants and replaced in tests. --- src/hooks/context/hook.spec.ts | 4 ++-- src/hooks/context/hooks.tsx | 4 ++-- src/messages.tsx | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hooks/context/hook.spec.ts b/src/hooks/context/hook.spec.ts index 70bcef469ed..e39ec83a9ea 100644 --- a/src/hooks/context/hook.spec.ts +++ b/src/hooks/context/hook.spec.ts @@ -94,13 +94,13 @@ describe('useContextFile().deleteContext ', () => { testingVariables.createDummyContextFile(); const { response, error } = useContextFile().deleteContext('code'); expect(error).toBeUndefined(); - expect(response).toMatch('context deleted successfully'); + expect(response).toMatch(messages.CONTEXT_DELETED); }); test('return error if deleting current context', () => { testingVariables.createDummyContextFile(); const { response, error } = useContextFile().deleteContext('home'); - expect(response).toMatch('context deleted successfully'); + expect(response).toMatch(messages.CONTEXT_DELETED); expect(error).toBeUndefined(); }); }); diff --git a/src/hooks/context/hooks.tsx b/src/hooks/context/hooks.tsx index dddfd3c2430..1231111eb71 100644 --- a/src/hooks/context/hooks.tsx +++ b/src/hooks/context/hooks.tsx @@ -66,11 +66,11 @@ export const useContextFile = (): any => { const ctx = contextService.loadContextFile(); if (Object.keys(ctx.store).length === 1) { contextService.deleteContextFile(); - return { response: 'context deleted successfully' }; + return { response: messages.CONTEXT_DELETED }; } const updatedContext = contextService.deleteContext(ctx, key); contextService.save(updatedContext); - const response = 'context deleted successfully'; + const response = messages.CONTEXT_DELETED; return { response }; } catch (error) { return { error }; diff --git a/src/messages.tsx b/src/messages.tsx index 01264737892..9bf12f99895 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -11,3 +11,5 @@ export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set export const MISSING_ARGUMENTS = 'Missing arguments.'; export const NEW_CONTEXT_ADDED = 'New context added.'; + +export const CONTEXT_DELETED = 'context deleted successfully'; From 80ecc08b97842c98ac7968b44ece54cb09e9726c Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 3 Aug 2021 15:04:15 +0530 Subject: [PATCH 11/17] fix: response not matching issue. --- src/hooks/context/hooks.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/context/hooks.tsx b/src/hooks/context/hooks.tsx index 857474db221..21bfea1d35d 100644 --- a/src/hooks/context/hooks.tsx +++ b/src/hooks/context/hooks.tsx @@ -45,7 +45,7 @@ export const useContextFile = (): any => { try { const newContext = contextService.addContext(context, key, specFile); contextService.save(contextService.updateCurrent(newContext, key)); - const response = 'New context added'; + const response = messages.NEW_CONTEXT_ADDED; return { response }; } catch (error) { return { error }; From f9954997ce24d9e88be844febd6c42c8cbcc61a3 Mon Sep 17 00:00:00 2001 From: Souvik Date: Thu, 5 Aug 2021 16:59:06 +0530 Subject: [PATCH 12/17] feat: converted validation messages into constant. --- src/hooks/validation/hook.spec.ts | 6 ++++-- src/hooks/validation/hook.tsx | 5 +++-- src/messages.tsx | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/hooks/validation/hook.spec.ts b/src/hooks/validation/hook.spec.ts index 4422aac7e3d..b21bd2c1a33 100644 --- a/src/hooks/validation/hook.spec.ts +++ b/src/hooks/validation/hook.spec.ts @@ -2,6 +2,7 @@ import { container } from 'tsyringe'; import { SpecificationFile, ValidationInput, ValidationResponse } from './models'; import { ValidationService } from './ValidationService'; import { useValidate } from './hook'; +import {ValidationMessage} from '../../messages'; function ValidationServiceMock() { return { @@ -39,7 +40,8 @@ describe('UseValidate should', () => { expect(useValidateResponse.success).toBeFalsy(); expect(useValidateResponse.message).toEqual(''); - expect(useValidateResponse.errors[0]).toBe(`File: ${invalidFileValidationInput.file.getSpecificationName()} does not exists or is not a file!`); + + expect(useValidateResponse.errors[0]).toBe(ValidationMessage(invalidFileValidationInput.file.getSpecificationName()).error()); }); test('return success when the validation is correct', async () => { @@ -47,7 +49,7 @@ describe('UseValidate should', () => { const useValidateResponse = await useValidate().validate(fileThatExistsValidationInput); expect(useValidateResponse.success).toBeTruthy(); - expect(useValidateResponse.message).toEqual(`File: ${fileThatExistsValidationInput.file.getSpecificationName()} successfully validated!`); + expect(useValidateResponse.message).toEqual(ValidationMessage(fileThatExistsValidationInput.file.getSpecificationName()).message()); }); test('return validation service errors when the validation has failed', async () => { diff --git a/src/hooks/validation/hook.tsx b/src/hooks/validation/hook.tsx index 8523b2c5e9d..b467044bf2b 100644 --- a/src/hooks/validation/hook.tsx +++ b/src/hooks/validation/hook.tsx @@ -2,6 +2,7 @@ import { container } from 'tsyringe'; import { UseValidateResponse, ValidationInput, ValidationResponse } from './models'; import { ValidationService } from './ValidationService'; +import { ValidationMessage } from '../../messages'; export function useValidate() { const validationService: ValidationService = container.resolve(ValidationService); @@ -10,11 +11,11 @@ export function useValidate() { async validate({ file }: ValidationInput): Promise { try { if (file.isNotValid()) { - return Promise.resolve(UseValidateResponse.withError(`File: ${file.getSpecificationName()} does not exists or is not a file!`)); + return Promise.resolve(UseValidateResponse.withError(ValidationMessage(file.getSpecificationName()).error())); } const response: ValidationResponse = await validationService.execute(file); if (response.success) { - return Promise.resolve(UseValidateResponse.withMessage(`File: ${file.getSpecificationName()} successfully validated!`)); + return Promise.resolve(UseValidateResponse.withMessage(ValidationMessage(file.getSpecificationName()).message())); } return Promise.resolve(UseValidateResponse.withErrors(response.errors)); } catch (error) { diff --git a/src/messages.tsx b/src/messages.tsx index 9bf12f99895..b32af5c0448 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -13,3 +13,8 @@ export const MISSING_ARGUMENTS = 'Missing arguments.'; export const NEW_CONTEXT_ADDED = 'New context added.'; export const CONTEXT_DELETED = 'context deleted successfully'; + +export const ValidationMessage = (filePath: string) => ({ + error: () => `File: ${filePath} does not exists or is not a file!`, + message: () => `File: ${filePath} successfully validated!` +}); From 336341ecb4146c68d1f66030a9c28e2fc69f306b Mon Sep 17 00:00:00 2001 From: Souvik Date: Mon, 9 Aug 2021 13:19:19 +0530 Subject: [PATCH 13/17] feat: updated message for adding new context --- src/components/Context/context.spec.tsx | 2 +- src/help-message.ts | 4 ++-- src/hooks/context/hook.spec.ts | 6 +++--- src/hooks/context/hooks.tsx | 4 ++-- src/messages.tsx | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/components/Context/context.spec.tsx b/src/components/Context/context.spec.tsx index 4b1b73b19c8..c61712c3879 100644 --- a/src/components/Context/context.spec.tsx +++ b/src/components/Context/context.spec.tsx @@ -42,7 +42,7 @@ describe('AddContext ', () => { test('should return message', () => { testing.createDummyContextFile(); const { lastFrame } = render(); - expect(lastFrame()).toMatch(messages.NEW_CONTEXT_ADDED); + expect(lastFrame()).toMatch(messages.NEW_CONTEXT_ADDED('home')); }); }); diff --git a/src/help-message.ts b/src/help-message.ts index f7ad17970c8..e620d05b66f 100644 --- a/src/help-message.ts +++ b/src/help-message.ts @@ -29,7 +29,7 @@ export class HelpMessage { shortDescription: 'Validate asyncapi file', flags: [ this.helpFlag, - '-f, --file Path of the spec file', + '-f, --file Path of the AsyncAPI file', '-c, --context Context to use', '-w, --watch Watch mode' ] @@ -42,7 +42,7 @@ export class HelpMessage { subCommands: [ 'list list all saved contexts', 'current see current context', - 'use set a context as current', + 'use set given context as default/current', 'add add/update context', 'remove remove a context' ] diff --git a/src/hooks/context/hook.spec.ts b/src/hooks/context/hook.spec.ts index e39ec83a9ea..8b5a719758a 100644 --- a/src/hooks/context/hook.spec.ts +++ b/src/hooks/context/hook.spec.ts @@ -43,7 +43,7 @@ describe('useContextFile().addContext ', () => { testingVariables.deleteDummyContextFile(); const { response, error } = useContextFile().addContext('home', new SpecificationFile('./test/specification.yml')); expect(error).toBeUndefined(); - expect(response).toMatch(messages.NEW_CONTEXT_ADDED); + expect(response).toMatch(messages.NEW_CONTEXT_ADDED('home')); testingVariables.deleteDummyContextFile(); }); @@ -51,14 +51,14 @@ describe('useContextFile().addContext ', () => { testingVariables.createDummyContextFile(); const { response, error } = useContextFile().addContext('home', new SpecificationFile('./test/specification.yml')); expect(error).toBeUndefined(); - expect(response).toMatch(messages.NEW_CONTEXT_ADDED); + expect(response).toMatch(messages.NEW_CONTEXT_ADDED('home')); }); test('Auto set current when when adding context for the fist time', () => { testingVariables.deleteDummyContextFile(); const { response, error } = useContextFile().addContext('home', new SpecificationFile('./test/specification.yml')); expect(error).toBeUndefined(); - expect(response).toMatch('New context added'); + expect(response).toMatch(messages.NEW_CONTEXT_ADDED('home')); const { response: res, error: err } = useContextFile().current(); expect(err).toBeUndefined(); expect(res?.key).toMatch('home'); diff --git a/src/hooks/context/hooks.tsx b/src/hooks/context/hooks.tsx index 21bfea1d35d..7481bc08290 100644 --- a/src/hooks/context/hooks.tsx +++ b/src/hooks/context/hooks.tsx @@ -37,7 +37,7 @@ export const useContextFile = (): any => { const ctx = contextService.loadContextFile(); const updatedContext = contextService.addContext(ctx, key, specFile); contextService.save(updatedContext); - const response = messages.NEW_CONTEXT_ADDED; + const response = messages.NEW_CONTEXT_ADDED(key); return { response }; } catch (error) { if (error instanceof ContextFileNotFoundError) { @@ -45,7 +45,7 @@ export const useContextFile = (): any => { try { const newContext = contextService.addContext(context, key, specFile); contextService.save(contextService.updateCurrent(newContext, key)); - const response = messages.NEW_CONTEXT_ADDED; + const response = messages.NEW_CONTEXT_ADDED(key); return { response }; } catch (error) { return { error }; diff --git a/src/messages.tsx b/src/messages.tsx index b32af5c0448..2282061204a 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -1,4 +1,4 @@ -export const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to know more'; +export const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to learn more'; export const CONTEXT_NOT_FOUND = 'This context key does not exists.'; @@ -10,7 +10,7 @@ export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set export const MISSING_ARGUMENTS = 'Missing arguments.'; -export const NEW_CONTEXT_ADDED = 'New context added.'; +export const NEW_CONTEXT_ADDED = (contextName: string) => `New context added.\nYou can set it as your current context:\n asyncapi context use ${contextName}\nYou can use this context when needed with --context flag`; export const CONTEXT_DELETED = 'context deleted successfully'; From 05b56590aa73a49e62094a7210b11c102e6cc830 Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 10 Aug 2021 12:47:05 +0530 Subject: [PATCH 14/17] feat: updated the contextNotFouncError and removed KeyNotFoundError --- src/components/Context/context.spec.tsx | 2 +- src/hooks/context/contextService.ts | 4 ++-- src/hooks/context/hook.spec.ts | 4 ++-- src/hooks/context/hooks.tsx | 4 ++-- src/hooks/context/models.ts | 11 ++--------- src/messages.tsx | 4 +--- 6 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/components/Context/context.spec.tsx b/src/components/Context/context.spec.tsx index c61712c3879..7da7eb9fb03 100644 --- a/src/components/Context/context.spec.tsx +++ b/src/components/Context/context.spec.tsx @@ -50,7 +50,7 @@ describe('SetContext ', () => { test('Should render error message is key is not in store', () => { testing.createDummyContextFile(); const { lastFrame } = render(); - expect(lastFrame()).toMatch(messages.KEY_NOT_FOUND); + expect(lastFrame()).toMatch(messages.CONTEXT_NOT_FOUND('name')); }); test('Should render the update context', () => { diff --git a/src/hooks/context/contextService.ts b/src/hooks/context/contextService.ts index fe45399c4bd..95a1ad185f0 100644 --- a/src/hooks/context/contextService.ts +++ b/src/hooks/context/contextService.ts @@ -1,5 +1,5 @@ import { injectable } from 'tsyringe'; -import { Context, ContextFileNotFoundError,KeyNotFoundError, SpecFileNotFoundError } from './models'; +import { Context, ContextFileNotFoundError, ContextNotFoundError, SpecFileNotFoundError } from './models'; import { CONTEXTFILE_PATH } from '../../constants'; import * as fs from 'fs'; import * as path from 'path'; @@ -38,7 +38,7 @@ export class ContextService { } updateCurrent(context: Context, key: string): Context { - if (!context.store[String(key)]) {throw new KeyNotFoundError();} + if (!context.store[String(key)]) {throw new ContextNotFoundError(key);} context.current = key; return context; } diff --git a/src/hooks/context/hook.spec.ts b/src/hooks/context/hook.spec.ts index 8b5a719758a..a786a99c254 100644 --- a/src/hooks/context/hook.spec.ts +++ b/src/hooks/context/hook.spec.ts @@ -1,5 +1,5 @@ import { useContextFile, useSpecfile } from './hooks'; -import { ContextFileNotFoundError, KeyNotFoundError, ContextNotFoundError } from './models'; +import { ContextFileNotFoundError, ContextNotFoundError } from './models'; import { ContextTestingHelper } from '../../constants'; import { SpecificationFile } from '../validation'; import * as messages from '../../messages'; @@ -78,7 +78,7 @@ describe('useContextFile.updateCurrent ', () => { testingVariables.createDummyContextFile(); const { response, error } = useContextFile().setCurrent('name'); expect(response).toBeUndefined(); - expect(error instanceof KeyNotFoundError).toBeTruthy(); + expect(error instanceof ContextNotFoundError).toBeTruthy(); }); test('Should update the current context', () => { diff --git a/src/hooks/context/hooks.tsx b/src/hooks/context/hooks.tsx index 7481bc08290..1fa8f0b0962 100644 --- a/src/hooks/context/hooks.tsx +++ b/src/hooks/context/hooks.tsx @@ -103,7 +103,7 @@ export const useContextFile = (): any => { try { const ctx = contextService.loadContextFile(); const ctxValue = ctx.store[String(key)]; - if (!ctxValue) { throw new ContextNotFoundError(); } + if (!ctxValue) { throw new ContextNotFoundError(key); } const response = new SpecificationFile(ctxValue); return { response }; } catch (error) { @@ -137,7 +137,7 @@ export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => { if (flags.context) { const ctxFile = ctx.store[flags.context]; - if (!ctxFile) { throw new ContextNotFoundError(); } + if (!ctxFile) { throw new ContextNotFoundError(flags.context); } const specFile = new SpecificationFile(ctxFile); return { specFile }; } diff --git a/src/hooks/context/models.ts b/src/hooks/context/models.ts index 77a944c5c0d..39aca90192f 100644 --- a/src/hooks/context/models.ts +++ b/src/hooks/context/models.ts @@ -21,16 +21,9 @@ export class ContextFileNotFoundError extends Error { } export class ContextNotFoundError extends Error { - constructor() { - super(); - this.message = messages.CONTEXT_NOT_FOUND; - } -} - -export class KeyNotFoundError extends Error { - constructor() { + constructor(contextName: string) { super(); - this.message = messages.KEY_NOT_FOUND; + this.message = messages.CONTEXT_NOT_FOUND(contextName); } } diff --git a/src/messages.tsx b/src/messages.tsx index 2282061204a..4d3b6b3a7d7 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -1,8 +1,6 @@ export const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to learn more'; -export const CONTEXT_NOT_FOUND = 'This context key does not exists.'; - -export const KEY_NOT_FOUND = 'Key not found'; +export const CONTEXT_NOT_FOUND = (contextName: string) => `Context ${contextName} does not exists.`; export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that is currently in use.'; From d82bf13e3261b4b4df3f7284332647c14f28f5ce Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 10 Aug 2021 12:49:14 +0530 Subject: [PATCH 15/17] chore: removed unused messages --- src/hooks/context/models.ts | 7 ------- src/messages.tsx | 2 -- 2 files changed, 9 deletions(-) diff --git a/src/hooks/context/models.ts b/src/hooks/context/models.ts index 39aca90192f..0fb57fbde54 100644 --- a/src/hooks/context/models.ts +++ b/src/hooks/context/models.ts @@ -27,13 +27,6 @@ export class ContextNotFoundError extends Error { } } -export class DeletingCurrentContextError extends Error { - constructor() { - super(); - this.message = messages.DELETING_CURRENT_CONTEXT; - } -} - export class MissingCurrentContextError extends Error { constructor() { super(); diff --git a/src/messages.tsx b/src/messages.tsx index 4d3b6b3a7d7..63d83eba037 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -2,8 +2,6 @@ export const NO_CONTEXTS_SAVED = 'No contexts saved yet, run asyncapi --help to export const CONTEXT_NOT_FOUND = (contextName: string) => `Context ${contextName} does not exists.`; -export const DELETING_CURRENT_CONTEXT = 'You are tyring to delete a context that is currently in use.'; - export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set a current context.'; export const MISSING_ARGUMENTS = 'Missing arguments.'; From d3bf105f83228e9edd4aa6731eb8995b042ee38d Mon Sep 17 00:00:00 2001 From: Souvik Date: Tue, 10 Aug 2021 15:39:25 +0530 Subject: [PATCH 16/17] chore: updated NEW_CONTEXT_ADDED message --- src/messages.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/messages.tsx b/src/messages.tsx index 63d83eba037..199689ae98c 100644 --- a/src/messages.tsx +++ b/src/messages.tsx @@ -6,7 +6,7 @@ export const MISSING_CURRENT_CONTEXT = 'No context is set as current, please set export const MISSING_ARGUMENTS = 'Missing arguments.'; -export const NEW_CONTEXT_ADDED = (contextName: string) => `New context added.\nYou can set it as your current context:\n asyncapi context use ${contextName}\nYou can use this context when needed with --context flag`; +export const NEW_CONTEXT_ADDED = (contextName: string) => `New context added.\n\nYou can set it as your current context:\n asyncapi context use ${contextName}\nYou can use this context when needed with --context flag: asyncapi validate --context ${contextName}`; export const CONTEXT_DELETED = 'context deleted successfully'; From 8257dcacae7bf35101c32648bcc40448e49afea4 Mon Sep 17 00:00:00 2001 From: Souvik Date: Wed, 11 Aug 2021 08:18:20 +0530 Subject: [PATCH 17/17] chore: updated SpecFIleNotFoundError message --- src/hooks/context/contextService.ts | 2 +- src/hooks/context/models.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hooks/context/contextService.ts b/src/hooks/context/contextService.ts index 95a1ad185f0..b5e17324de6 100644 --- a/src/hooks/context/contextService.ts +++ b/src/hooks/context/contextService.ts @@ -26,7 +26,7 @@ export class ContextService { } addContext(context: Context, key: string, specFile: SpecificationFile): Context { - if (specFile.isNotValid()) {throw new SpecFileNotFoundError();} + if (specFile.isNotValid()) {throw new SpecFileNotFoundError(specFile.getSpecificationName());} context.store[String(key)] = specFile.getSpecificationName(); return context; } diff --git a/src/hooks/context/models.ts b/src/hooks/context/models.ts index 0fb57fbde54..46f1a462122 100644 --- a/src/hooks/context/models.ts +++ b/src/hooks/context/models.ts @@ -1,15 +1,15 @@ import * as messages from '../../messages'; export interface Context { - current: string, - store: { - [name: string]: string - } + current: string, + store: { + [name: string]: string + } } export class SpecFileNotFoundError extends Error { - constructor() { + constructor(specPath: string) { super(); - this.message = 'specification file not found in that path.'; + this.message = messages.ValidationMessage(specPath).error(); } }