Skip to content

Commit

Permalink
added the format flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Sep 18, 2024
1 parent 345abb9 commit e9126dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ 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) {
convertedFile = convertOpenAPI(this.specFile.text(), specJson.openapi as OpenAPIConvertVersion, {
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'])}!!`);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/core/flags/convert.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
};
};
23 changes: 12 additions & 11 deletions test/integration/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`);
Expand All @@ -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('');
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -179,6 +179,7 @@ describe('convert', () => {
done();
});
});

describe('with OpenAPI input', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
Expand All @@ -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('');
Expand All @@ -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('');
Expand All @@ -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('');
Expand All @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit e9126dd

Please sign in to comment.