diff --git a/src/commands/pretty.ts b/src/commands/pretty.ts index f47fb484ee6..5954f076168 100644 --- a/src/commands/pretty.ts +++ b/src/commands/pretty.ts @@ -5,7 +5,9 @@ import Command from '../core/base'; import { load } from '../core/models/SpecificationFile'; import { ValidationError } from '../core/errors/validation-error'; import { prettyFlags } from '../core/flags/pretty.flags'; - +import { + retrieveFileFormat, +} from '../core/models/SpecificationFile'; export default class Pretty extends Command { static readonly description = 'Format AsyncAPI specification file'; @@ -40,11 +42,18 @@ export default class Pretty extends Command { let formatted: string; try { - const yamlDoc = yaml.parseDocument(content); - - formatted = yamlDoc.toString({ - lineWidth: 0, - }); + const fileFormat = retrieveFileFormat(this.specFile.text()); + if (fileFormat === 'yaml') { + const yamlDoc = yaml.parseDocument(content); + formatted = yamlDoc.toString({ + lineWidth: 0, + }); + } else if (fileFormat === 'json') { + const jsonObj = JSON.parse(content); + formatted = JSON.stringify(jsonObj, null, 2); + } else { + throw new Error('Unsupported file format'); + } } catch (err) { this.error(`Error formatting file: ${err}`); } diff --git a/test/fixtures/badFormatAsyncapi.json b/test/fixtures/badFormatAsyncapi.json new file mode 100644 index 00000000000..db8b7cae44c --- /dev/null +++ b/test/fixtures/badFormatAsyncapi.json @@ -0,0 +1,38 @@ +{ + "asyncapi": "2.2.0", + "info": { + "title": "Account Service", + "version": "1.0.0", + "description": + "This service is in charge of processing user signups" + }, + "channels": { + "user/signedup": { + "subscribe": { + "message": { + "$ref": "#/components/messages/UserSignedUp" + } + } + } + }, + "components": { + "messages": { + "UserSignedUp": { + "payload": { + "type": "object", + "properties": { + "displayName": { + "type": "string", + "description": "Name of the user" + }, + "email": { + "type": "string", + "format": "email", + "description": "Email of the user" + } + } + } + } + } + } + } \ No newline at end of file diff --git a/test/integration/pretty.test.ts b/test/integration/pretty.test.ts index 165c902e72e..29dc57244ea 100644 --- a/test/integration/pretty.test.ts +++ b/test/integration/pretty.test.ts @@ -5,6 +5,7 @@ import { expect } from '@oclif/test'; const testHelper = new TestHelper(); const badFormatPath = './test/fixtures/asyncapi_v1.yml'; const validFormatPath = './test/fixtures/asyncapiValid_v1.yml'; +const badFormatPathJson = './test/fixtures/badFormatAsyncapi.json'; describe('pretty', () => { describe('with file paths', () => { @@ -47,5 +48,17 @@ describe('pretty', () => { expect(ctx.stderr).to.equal(''); done(); }); + + test + .stderr() + .stdout() + .command(['pretty', badFormatPathJson]) + .it('should log the information file has been beautified json file', (ctx, done) => { + expect(ctx.stdout).to.contain( + `Asyncapi document ${badFormatPathJson} has been beautified in-place`, + ); + expect(ctx.stderr).to.equal(''); + done(); + }); }); });