Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: json file supported in asyncapi new file command #1713

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading