Skip to content

Commit 74109c6

Browse files
AayushSaini101asyncapi-botShurtu-gal
authored
chore: codebase refactoring and cleanup and documentation update (#1886)
Co-authored-by: AayushSaini101 <kumaraayush9810@gmail.com> Co-authored-by: Chan <bot+chan@asyncapi.io> Co-authored-by: Ashish Padhy <ashishpadhy1729@gmail.com>
1 parent b46df80 commit 74109c6

25 files changed

+1703
-315
lines changed

DEVELOPMENT.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@ npm install
2828
To run all tests locally:
2929

3030
- CLI tests: `npm run cli:test`
31+
- Unit tests: `npm run unit:test`
3132
- Github action tests: `npm run action:test`
33+
- Single test file: `npm run test:one -- <path-to-test-file>`
3234

3335
### Adding tests
3436

3537
1. Create new test files in the appropriate directory under `test/`:
38+
- Unit tests: `test/unit/<category>/<name>.test.ts`
39+
- Integration tests: `test/integration/<command>.test.ts`
3640

3741
2. Follow the existing test patterns.
3842

3943
3. Run your new tests using the commands mentioned above.
4044

45+
> 📘 **For detailed debugging and testing guidelines**, see the [Debugging & Testing Guide](/docs/debugging-testing.md).
46+
4147
## Release process
4248

4349
To release a major/minor/patch:
@@ -137,5 +143,9 @@ If you encounter any issues during development or testing, please check the foll
137143
1. Ensure you're using the correct Node.js version (24.0.0 or higher) and npm version (8.19.0 or higher).
138144
2. Clear the `node_modules` directory and reinstall dependencies if you encounter unexpected behavior.
139145
3. For Docker-related issues, make sure Docker is running and you have sufficient permissions.
146+
4. For permission errors, try: `sudo chown -R $(whoami) ./lib ./node_modules`
147+
5. For path alias issues, rebuild the project: `npm run build`
148+
149+
> 📘 **For comprehensive debugging help**, see the [Debugging & Testing Guide](/docs/debugging-testing.md).
140150
141151
If problems persist, please open an issue on the GitHub repository.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CLI to work with your AsyncAPI files. Currently under development, we are workin
1313
- [Installation](#installation)
1414
- [Usage](#usage)
1515
- [Architecture](#architecture)
16+
- [Debugging & Testing](#debugging--testing)
1617
- [Github Action](#github-action)
1718
- [Contributing](#contributing)
1819
* [Set up development environment](#set-up-development-environment)
@@ -30,6 +31,9 @@ The [usage guide](/docs/usage.md) provides information about different ways to u
3031
## Architecture
3132
The [architecture guide](/docs/architecture.md) provides information about the architecture.
3233

34+
## Debugging & Testing
35+
The [debugging & testing guide](/docs/debugging-testing.md) provides step-by-step instructions for debugging CLI commands, API endpoints, and services, along with comprehensive testing guidelines.
36+
3337
## Github Action
3438

3539
The AsyncAPI CLI can be used as a GitHub Action. You can find more information in the [GitHub Action guide](https://www.asyncapi.com/docs/tools/cli/github-action).

docs/architecture.md

Lines changed: 123 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,150 @@ title: 'CLI Architecture'
33
weight: 40
44
---
55

6-
The AsyncAPI CLI uses oclif (Open CLI Framework) as its core framework, which enables developers to build powerful and scalable command-line applications.
6+
# CLI Architecture
77

8-
**Structure of the AsyncAPI CLI**: The CLI is primarily divided into two components: commands and the core part.
8+
## Overview
99

10-
1. **Command Component**: The commands include all the necessary functionalities that help developers interact with features like creating new AsyncAPI projects, validating AsyncAPI files, formatting AsyncAPI files, and more.
11-
2. **Core Component**: The core part of the CLI contains various utilities that facilitate the efficient creation of new commands.
10+
The AsyncAPI CLI is built with [oclif](https://oclif.io/) and provides both command-line operations and a REST API server for working with AsyncAPI specifications.
1211

1312
---
1413

15-
### Detailed Explanation of Key Directories in the CLI
16-
17-
#### `src/commands/`
18-
- **Purpose:** Implements the CLI commands available to the user.
19-
- **Subdirectories:**
20-
- `config/`: Stores configuration-related files for commands.
21-
- `generate/`: Generates typed models or other artifacts like clients, applications, or documentation using AsyncAPI Generator templates.
22-
- **Files:**
23-
- `fromTemplate.ts`: Contains logic for generating files using templates.
24-
- `models.ts`: Defines the models used during generation.
25-
- `new/`: Create a new AsyncAPI project, specification files, or templates for clients and applications.
26-
- **Files:**
27-
- `file.ts`: Handles file creation logic.
28-
- `template.ts`: Manages templates for new projects.
29-
- `start/`: Implements starting functionalities like launching a local server or studio.
30-
- **Files:**
31-
- `studio.ts`: Integrates with the AsyncAPI Studio.
32-
33-
- **Standalone Files:**
34-
- `bundle.ts`: Bundles one or multiple AsyncAPI documents and their references together.
35-
- `convert.ts`: Converts AsyncAPI documents from older to newer versions or transforms OpenAPI/Postman-collection documents into AsyncAPI.
36-
- `diff.ts`: Compares two AsyncAPI documents.
37-
- `format.ts`: Converts AsyncAPI documents from any format to YAML, YML, or JSON.
38-
- `optimize.ts`: Optimizes AsyncAPI documents for performance.
39-
- `pretty.ts`: Beautifies the AsyncAPI spec file (indentation, styling) in place or outputs the formatted spec to a new file.
40-
- `validate.ts`: Validates AsyncAPI documents for correctness.
14+
## Architecture Diagram
15+
16+
```
17+
┌─────────────────────────────────────────────────┐
18+
│ Entry Points │
19+
│ ┌──────────┐ ┌──────────┐ │
20+
│ │ CLI │ │ API │ │
21+
│ │ (oclif) │ │ (Express)│ │
22+
│ └────┬─────┘ └────┬─────┘ │
23+
└───────┼─────────────────────────┼───────────────┘
24+
└───────────┬─────────────┘
25+
26+
┌───────────────────────┐
27+
│ Domain Services │
28+
│ Validation, Generator│
29+
│ Convert, Config │
30+
└───────────┬───────────┘
31+
32+
┌───────────────────────┐
33+
│ Domain Models │
34+
│ Specification,Context│
35+
└───────────┬───────────┘
36+
37+
┌───────────────────────┐
38+
│ Utilities │
39+
│ Logger, Helpers │
40+
└───────────────────────┘
41+
```
4142

4243
---
4344

44-
#### `src/core/`
45-
- **Purpose:** Provides foundational components and utilities for the CLI.
46-
- **Subdirectories:**
47-
- `errors/`: Centralized error definitions.
48-
- `flags/`: Defines CLI flags and their behavior.
49-
- `hooks/`: Event hooks used for customization.
50-
- `models/`: Core data models used across the application.
51-
- `utils/`: Utility functions for common operations.
52-
53-
- **Standalone Files:**
54-
- `base.ts`: Base class or logic for CLI commands.
55-
- `global.d.ts`: Global TypeScript definitions.
56-
- `globals.ts`: Stores global variables and configurations.
57-
- `parser.ts`: Parses AsyncAPI documents.
45+
## Directory Structure
46+
47+
```
48+
src/
49+
├── apps/
50+
│ ├── cli/ # CLI commands & internals
51+
│ └── api/ # REST API (Express)
52+
├── domains/
53+
│ ├── models/ # Specification, Context
54+
│ └── services/ # Business logic
55+
├── errors/ # Custom errors
56+
├── interfaces/ # TypeScript types
57+
└── utils/ # Utilities
58+
```
5859

5960
---
6061

61-
#### `test/`
62-
- **Purpose:** Implements the test suite for the CLI.
63-
- **Subdirectories:**
64-
- `fixtures/`: Contains mock data or files for testing.
65-
- `hooks/`: Tests related to hooks.
66-
- `integration/`: Integration tests to verify end-to-end functionality.
67-
- `system/`: System-level tests.
68-
- `unit/`: Unit tests for individual modules or functions.
62+
## Core Components
63+
64+
### CLI Application
65+
66+
| Component | Description |
67+
|-----------|-------------|
68+
| **Entry Points** | `bin/run` (dev), `bin/run_bin` (prod) |
69+
| **Base Command** | Metrics, parser integration, error handling |
70+
71+
**Commands:**
72+
- **Core:** `validate`, `convert`, `format`, `optimize`, `diff`, `bundle`
73+
- **Generation:** `generate client`, `generate models`, `generate fromTemplate`
74+
- **Config:** `config context`, `config analytics`, `config versions`
75+
- **Utility:** `new file`, `new template`, `start api|studio|preview`, `pretty`
76+
77+
### API Server
78+
79+
**Endpoints:** `/v1/validate`, `/v1/parse`, `/v1/generate`, `/v1/convert`, `/v1/bundle`, `/v1/diff`, `/v1/docs`, `/v1/help`, `/v1/version`
80+
81+
**Features:** Express with Helmet security, CORS, compression, RFC 7807 error responses
82+
83+
### Domain Services
84+
85+
All services extend `BaseService` and return `ServiceResult<T>`:
86+
87+
| Service | Purpose |
88+
|---------|---------|
89+
| `ValidationService` | Validates specs with Spectral, calculates scores |
90+
| `GeneratorService` | Generates code/models |
91+
| `ConvertService` | Converts between AsyncAPI/OpenAPI formats |
92+
| `ConfigService` | Manages CLI config and contexts |
93+
| `ArchiverService` | Creates ZIP archives |
94+
95+
### Domain Models
96+
97+
| Model | Purpose |
98+
|-------|---------|
99+
| **Specification** | Loads from file, URL, or context; auto-detects `asyncapi.json\|yml\|yaml` |
100+
| **Context** | Manages multiple AsyncAPI contexts; stored in `~/.asyncapi/` |
101+
102+
### Error Classes
103+
104+
`ContextError`, `SpecificationFileError`, `ValidationError`, `GeneratorError`, `DiffError`
69105

70106
---
71107

72-
### Use Cases
108+
## Execution Flow
73109

74-
1. **Generate AsyncAPI Artifacts:**
75-
- Use the `generate` command to create client/server code, documentation, or other artifacts based on AsyncAPI templates.
110+
**CLI Command:**
111+
```
112+
User Command → oclif → Base Command → Domain Service → ServiceResult
113+
```
76114

77-
2. **Create New Projects:**
78-
- The `new` command helps users scaffold new AsyncAPI projects with predefined templates.
115+
**API Request:**
116+
```
117+
HTTP Request → Express → Controller → Domain Service → HTTP Response
118+
```
79119

80-
3. **Validate AsyncAPI Documents:**
81-
- The `validate` command ensures AsyncAPI documents conform to the specification.
120+
---
82121

83-
4. **Optimize and Format Documents:**
84-
- The `optimize` and `pretty` commands provide tools for improving document readability and performance.
122+
## Extension Points
85123

86-
5. **Compare Documents:**
87-
- The `diff` command enables comparison between two AsyncAPI documents to track changes.
124+
| Add | Steps |
125+
|-----|-------|
126+
| **New Command** | Create in `src/apps/cli/commands/`, extend `Command`, implement `run()` |
127+
| **New API Endpoint** | Create controller in `src/apps/api/controllers/`, register in `index.ts` |
128+
| **New Service** | Create in `src/domains/services/`, extend `BaseService`, return `ServiceResult<T>` |
88129

89-
6. **Integration with AsyncAPI Studio:**
90-
- The `start` command integrates with the AsyncAPI Studio for editing and visualizing documents.
130+
---
131+
132+
## Configuration
91133

92-
7. **Convert Between Formats:**
93-
- The `convert` command supports converting AsyncAPI documents between formats like YAML and JSON.
134+
| Config | Location |
135+
|--------|----------|
136+
| CLI Context | `~/.asyncapi/contexts.json`, `~/.asyncapi/.current` |
137+
| Analytics | `~/.asyncapi-analytics` |
138+
139+
**Environment Variables:**
140+
- `NODE_ENV``development` | `production` | `test`
141+
- `PORT` — API server port (default: 3000)
142+
- `ASYNCAPI_METRICS_*` — Metrics configuration
94143

95144
---
96145

97-
This structure ensures the CLI is modular, scalable, and easy to maintain. Let me know if you need further clarification or additional details!
146+
## Technology Stack
147+
148+
| Category | Technologies |
149+
|----------|--------------|
150+
| **Core** | oclif, TypeScript, Express |
151+
| **AsyncAPI** | @asyncapi/parser, generator, converter, bundler, diff, optimizer |
152+
| **Supporting** | winston, ajv, chalk, @clack/prompts |

docs/contributing-prs.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: 'Contributing via Pull Requests'
3+
weight: 50
4+
---
5+
6+
# Contributing via Pull Requests
7+
8+
## Getting Started
9+
10+
1. **Open an issue first** (unless it's a trivial fix)
11+
2. **Set up environment** — Follow [DEVELOPMENT.md](../DEVELOPMENT.md)
12+
3. **Create a branch** — Use prefixes: `feat/`, `fix/`, `docs/`, `refactor/`, `test/`, `chore/`
13+
14+
---
15+
16+
## PR Title Format
17+
18+
Follow [Conventional Commits](https://www.conventionalcommits.org/):
19+
20+
| Type | Description | Release |
21+
|------|-------------|---------|
22+
| `feat:` | New feature | MINOR |
23+
| `fix:` | Bug fix | PATCH |
24+
| `docs:` | Documentation | None |
25+
| `chore:` | Maintenance | None |
26+
| `test:` | Tests only | None |
27+
| `refactor:` | Code refactoring | None |
28+
29+
**Breaking changes:** Add `!``feat!:`, `fix!:`
30+
31+
**Examples:**
32+
-`feat: add AsyncAPI 3.0 validation support`
33+
-`fix: resolve context loading with special characters`
34+
-`Added new feature`
35+
-`fix bug`
36+
37+
---
38+
39+
## PR Checklist
40+
41+
**Before submitting:**
42+
- [ ] Branch synced with `main`
43+
- [ ] `npm run build` passes
44+
- [ ] `npm run cli:test` passes
45+
- [ ] `npm run lint` passes (max 5 warnings)
46+
- [ ] Documentation updated (if needed)
47+
48+
**Code quality:**
49+
- [ ] Follows existing code patterns
50+
- [ ] TypeScript types used (avoid `any`)
51+
- [ ] Error handling implemented
52+
- [ ] Tests added for new functionality
53+
- [ ] No `console.log` or commented code
54+
55+
---
56+
57+
## Code Standards
58+
59+
| Area | Guideline |
60+
|------|-----------|
61+
| **TypeScript** | Explicit types, interfaces for objects, prefer `const` |
62+
| **Organization** | Follow existing structure, use path aliases (`@/`, `@cli/`, `@domains/`) |
63+
| **Errors** | Use custom errors from `src/errors/`, return `ServiceResult<T>` from services |
64+
| **Commands** | Extend base `Command`, use domain services for business logic |
65+
66+
---
67+
68+
## Testing
69+
70+
**Add tests for:**
71+
- New commands or API endpoints
72+
- Bug fixes (regression tests)
73+
- New domain services
74+
- Complex business logic
75+
76+
```bash
77+
npm run cli:test # All tests
78+
npm run unit:test # Unit tests only
79+
```
80+
81+
---
82+
83+
## Best Practices
84+
85+
| ❌ Avoid | ✅ Do |
86+
|----------|-------|
87+
| Large PRs (>500 lines) | Small, focused PRs |
88+
| Multiple concerns in one PR | One issue per PR |
89+
| Skipping tests | Comprehensive tests |
90+
| Hardcoded values | Externalize configuration |
91+
| Force push to main | Rebase instead of merge |
92+
93+
---
94+
95+
## Review Process
96+
97+
1. CI runs automated checks
98+
2. Maintainers review code
99+
3. Address feedback promptly
100+
4. PR merged when approved
101+
102+
---
103+
104+
## Quick Reference
105+
106+
```bash
107+
# Setup
108+
npm install && npx lefthook install
109+
110+
# Before PR
111+
npm run build && npm run lint && npm run cli:test
112+
```
113+
114+
**Quality over speed** — Write good code, tests, and documentation.

0 commit comments

Comments
 (0)