From e9126ddc7c733c18fee60c96035c6a5ae520dfb8 Mon Sep 17 00:00:00 2001 From: utnim2 Date: Thu, 19 Sep 2024 01:34:08 +0530 Subject: [PATCH] added the `format` flag --- src/commands/convert.ts | 9 +++++---- src/core/flags/convert.flags.ts | 15 +++++++++++---- test/integration/convert.test.ts | 23 ++++++++++++----------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/commands/convert.ts b/src/commands/convert.ts index 5c9e54b46a3..4386cb92d1b 100644 --- a/src/commands/convert.ts +++ b/src/commands/convert.ts @@ -38,7 +38,8 @@ export default class Convert extends Command { // Determine if the input is OpenAPI or AsyncAPI const specJson = this.specFile.toJson(); - const isOpenAPI = 'openapi' in specJson; + const isOpenAPI = flags['format'] === 'openapi'; + const isAsyncAPI = flags['format'] === 'asyncapi'; // CONVERSION if (isOpenAPI) { @@ -46,12 +47,12 @@ export default class Convert extends Command { perspective: flags['perspective'] as 'client' | 'server' }); this.log(`🎉 The OpenAPI document has been successfully converted to AsyncAPI version ${green(flags['target-version'])}!`); - } else { + } else if (isAsyncAPI) { convertedFile = convert(this.specFile.text(), flags['target-version'] as AsyncAPIConvertVersion); if (this.specFile.getFilePath()) { - this.log(`🎉 The ${cyan(this.specFile.getFilePath())} file has been successfully converted to version ${green(flags['target-version'])}!`); + this.log(`🎉 The ${cyan(this.specFile.getFilePath())} file has been successfully converted to version ${green(flags['target-version'])}!!`); } else if (this.specFile.getFileURL()) { - this.log(`🎉 The URL ${cyan(this.specFile.getFileURL())} has been successfully converted to version ${green(flags['target-version'])}!`); + this.log(`🎉 The URL ${cyan(this.specFile.getFileURL())} has been successfully converted to version ${green(flags['target-version'])}!!`); } } diff --git a/src/core/flags/convert.flags.ts b/src/core/flags/convert.flags.ts index 4503807b8ac..c747368883f 100644 --- a/src/core/flags/convert.flags.ts +++ b/src/core/flags/convert.flags.ts @@ -4,12 +4,19 @@ export const convertFlags = (latestVersion: string) => { return { help: Flags.help({ char: 'h' }), output: Flags.string({ char: 'o', description: 'path to the file where the result is saved' }), - 'target-version': Flags.string({ char: 't', description: 'asyncapi version to convert to (for both asyncapi and openapi conversions)', default: latestVersion }), + format: Flags.string({ + char: 'f', + description: 'Specify the format to convert from (openapi or asyncapi)', + options: ['openapi', 'asyncapi'], + required: true, + default: 'asyncapi', + }), + 'target-version': Flags.string({ char: 't', description: 'asyncapi version to convert to', default: latestVersion }), perspective: Flags.string({ char: 'p', - description: 'perspective to use when converting OpenAPI to AsyncAPI (client or server). Note: This option is only applicable for OpenAPI to AsyncAPI conversions.', + description: 'Perspective to use when converting OpenAPI to AsyncAPI (client or server). Note: This option is only applicable for OpenAPI to AsyncAPI conversions.', options: ['client', 'server'], - default: 'server' - }) + default: 'server', + }), }; }; diff --git a/test/integration/convert.test.ts b/test/integration/convert.test.ts index 1d645e27537..5fd2bf11454 100644 --- a/test/integration/convert.test.ts +++ b/test/integration/convert.test.ts @@ -86,7 +86,7 @@ describe('convert', () => { testHelper.unsetCurrentContext(); testHelper.createDummyContextFile(); }) - .command(['convert']) + .command(['convert', '-f', 'asyncapi']) .it('throws error message if no current context', (ctx, done) => { expect(ctx.stdout).to.equal(''); expect(ctx.stderr).to.equal('ContextError: No context is set as current, please set a current context.\n'); @@ -108,7 +108,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert']) + .command(['convert', '-f', 'asyncapi']) .it('throws error message if no context file exists', (ctx, done) => { expect(ctx.stdout).to.equal(''); expect(ctx.stderr).to.equal(`error locating AsyncAPI document: ${NO_CONTEXTS_SAVED}\n`); @@ -128,7 +128,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', filePath, '-t=2.3.0']) + .command(['convert', filePath, '-f', 'asyncapi', '-t=2.3.0']) .it('works when supported target-version is passed', (ctx, done) => { expect(ctx.stdout).to.contain('asyncapi: 2.3.0'); expect(ctx.stderr).to.equal(''); @@ -138,7 +138,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', filePath, '-t=2.95.0']) + .command(['convert', filePath, '-f', 'asyncapi', '-t=2.95.0']) .it('should throw error if non-supported target-version is passed', (ctx, done) => { expect(ctx.stdout).to.equal(''); expect(ctx.stderr).to.contain('Error: Cannot convert'); @@ -158,7 +158,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', filePath, '-o=./test/fixtures/specification_output.yml']) + .command(['convert', filePath, '-f', 'asyncapi', '-o=./test/fixtures/specification_output.yml']) .it('works when .yml file is passed', (ctx, done) => { expect(ctx.stdout).to.contain(`The ${filePath} file has been successfully converted to version 3.0.0!!`); expect(fs.existsSync('./test/fixtures/specification_output.yml')).to.equal(true); @@ -170,7 +170,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', JSONFilePath, '-o=./test/fixtures/specification_output.json']) + .command(['convert', JSONFilePath, '-f', 'asyncapi', '-o=./test/fixtures/specification_output.json']) .it('works when .json file is passed', (ctx, done) => { expect(ctx.stdout).to.contain(`The ${JSONFilePath} file has been successfully converted to version 3.0.0!!`); expect(fs.existsSync('./test/fixtures/specification_output.json')).to.equal(true); @@ -179,6 +179,7 @@ describe('convert', () => { done(); }); }); + describe('with OpenAPI input', () => { beforeEach(() => { testHelper.createDummyContextFile(); @@ -191,7 +192,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', openAPIFilePath]) + .command(['convert', openAPIFilePath, '-f', 'openapi']) .it('works when OpenAPI file path is passed', (ctx, done) => { expect(ctx.stdout).to.contain('The OpenAPI document has been successfully converted to AsyncAPI version 3.0.0!'); expect(ctx.stderr).to.equal(''); @@ -201,7 +202,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', openAPIFilePath, '-p=client']) + .command(['convert', openAPIFilePath, '-f', 'openapi', '-p=client']) .it('works when OpenAPI file path is passed with client perspective', (ctx, done) => { expect(ctx.stdout).to.contain('The OpenAPI document has been successfully converted to AsyncAPI version 3.0.0!'); expect(ctx.stderr).to.equal(''); @@ -211,7 +212,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', openAPIFilePath, '-p=server']) + .command(['convert', openAPIFilePath, '-f', 'openapi','-p=server']) .it('works when OpenAPI file path is passed with server perspective', (ctx, done) => { expect(ctx.stdout).to.contain('The OpenAPI document has been successfully converted to AsyncAPI version 3.0.0!'); expect(ctx.stderr).to.equal(''); @@ -221,7 +222,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', openAPIFilePath, '-p=invalid']) + .command(['convert', openAPIFilePath, '-f', 'openapi', '-p=invalid']) .it('should throw error if invalid perspective is passed', (ctx, done) => { expect(ctx.stdout).to.equal(''); expect(ctx.stderr).to.contain('Error: Expected --perspective=invalid to be one of: client, server'); @@ -231,7 +232,7 @@ describe('convert', () => { test .stderr() .stdout() - .command(['convert', openAPIFilePath, '-o=./test/fixtures/openapi_converted_output.yml']) + .command(['convert', openAPIFilePath, '-f', 'openapi', '-o=./test/fixtures/openapi_converted_output.yml']) .it('works when OpenAPI file is converted and output is saved', (ctx, done) => { expect(ctx.stdout).to.contain('🎉 The OpenAPI document has been successfully converted to AsyncAPI version 3.0.0!'); expect(fs.existsSync('./test/fixtures/openapi_converted_output.yml')).to.equal(true);