Skip to content

Commit

Permalink
Add support to beautify the json asyncapi document
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSaini101 committed Nov 20, 2024
1 parent 4d8faed commit aee81be
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/commands/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`);
}
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/badFormatAsyncapi.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions test/integration/pretty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit aee81be

Please sign in to comment.