Skip to content

Commit

Permalink
chore: json file supported in asyncapi new file command (#1713)
Browse files Browse the repository at this point in the history
Co-authored-by: Sumitwarrior7 <[email protected]>
Co-authored-by: Moderator <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2025
1 parent 0c7928d commit 819b585
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-seas-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/cli": patch
---

[Fix]: Json file supported in asyncapi new file command
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
/yarn.lock
/assets/examples/**
!assets/examples/default-example.yaml
!assets/examples/tutorial.yml
!assets/examples/tutorial.yml'
!assets/examples/default-example.json
node_modules
/test/integration/generate/models/
test.asyncapi-cli
Expand Down
51 changes: 51 additions & 0 deletions assets/examples/default-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"asyncapi": "3.0.0",
"info": {
"title": "Account Service",
"version": "1.0.0",
"description": "This service is in charge of processing user signups"
},
"channels": {
"userSignedUp": {
"address": "user/signedup",
"messages": {
"UserSignedUp": {
"$ref": "#/components/messages/UserSignedUp"
}
}
}
},
"operations": {
"onUserSignUp": {
"action": "receive",
"channel": {
"$ref": "#/channels/userSignedUp"
},
"messages": [
{
"$ref": "#/channels/userSignedUp/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"
}
}
}
}
}
}
}
21 changes: 18 additions & 3 deletions src/commands/new/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { fileFlags } from '../../core/flags/new/file.flags';

const { writeFile, readFile } = fPromises;
const DEFAULT_ASYNCAPI_FILE_NAME = 'asyncapi.yaml';
const DEFAULT_ASYNCAPI_TEMPLATE = 'default-example.yaml';
const DEFAULT_ASYNCAPI_YAML_TEMPLATE = 'default-example.yaml';
const DEFAULT_ASYNCAPI_JSON_TEMPLATE = 'default-example.json';

interface IExample{
name: string,
Expand Down Expand Up @@ -49,7 +50,14 @@ export default class NewFile extends Command {
}

const fileName = flags['file-name'] || DEFAULT_ASYNCAPI_FILE_NAME;
const template = flags['example'] || DEFAULT_ASYNCAPI_TEMPLATE;
// Determine template based on file extension
let default_template;
if (fileName.endsWith('.json')) {
default_template = DEFAULT_ASYNCAPI_JSON_TEMPLATE;
} else {
default_template = DEFAULT_ASYNCAPI_YAML_TEMPLATE;
}
const template = flags['example'] || default_template;

await this.createAsyncapiFile(fileName, template);

Expand Down Expand Up @@ -124,7 +132,14 @@ export default class NewFile extends Command {
}

fileName = fileName || DEFAULT_ASYNCAPI_FILE_NAME;
selectedTemplate = selectedTemplate || DEFAULT_ASYNCAPI_TEMPLATE;
// Determine template based on file extension
let default_template;
if (fileName.endsWith('.json')) {
default_template = DEFAULT_ASYNCAPI_JSON_TEMPLATE;
} else {
default_template = DEFAULT_ASYNCAPI_YAML_TEMPLATE;
}
selectedTemplate = selectedTemplate || default_template;

await this.createAsyncapiFile(fileName, selectedTemplate);
fileName = fileName.includes('.') ? fileName : `${fileName}.yaml`;
Expand Down

0 comments on commit 819b585

Please sign in to comment.