Code Generation Guide
TSDIAPI provides a powerful code generation system that allows plugins to register their own generators. This guide explains how to use and create code generators.
🚀 Using Plugin Generators
Basic Usage
# Navigate to your API directory
cd src/api
# Generate code using a plugin's generator
tsdiapi generate <pluginName> <generatorName>
Example Usage
# Generate code using any plugin's generator
tsdiapi generate <pluginName> <generatorName>
This will:
- Show available generators in the plugin
- Prompt for required parameters
- Generate the necessary files
- Update Prisma schema if needed
- Add new endpoints to Swagger
🔧 Generator Configuration
Plugins can register generators with the following configuration:
{
"name": "feature", // Generator name
"description": "Generate a feature", // Generator description
"dependencies": [ // Required dependencies
"@tsdiapi/inforu",
"@tsdiapi/email"
],
"files": [ // Files to generate
{
"source": "generators/feature/*.*",
"destination": "{{name}}",
"overwrite": false,
"isHandlebarsTemplate": true
}
],
"args": [ // Generator arguments
{
"name": "userModelName",
"description": "Prisma model name for users",
"inquirer": {
"type": "input",
"message": "Enter the Prisma model name for users:",
"default": "User"
}
}
],
"prismaScripts": [ // Prisma schema updates
{
"command": "ADD MODEL {{pascalCase userModelName}} ({id String @id @default(cuid())});",
"description": "Add User model to Prisma schema"
}
],
"postMessages": [ // Success messages
"✅ Feature {{name}} created successfully!"
]
}
📦 Plugin Generators
Overview
Each plugin can register its own generators. These generators can:
- Create new features
- Generate services
- Set up modules
- Update Prisma schema
- Add Swagger documentation
Discovering Generators
# List available generators in a plugin
tsdiapi generate <pluginName> --list
Common Generator Types
-
Feature Generators
- Create complete feature modules
- Set up controllers, services, and schemas
- Configure routes and endpoints
-
Service Generators
- Generate service classes
- Set up dependency injection
- Create base functionality
-
Schema Generators
- Create TypeBox schemas
- Generate validation rules
- Set up Swagger documentation
-
Database Generators
- Update Prisma schema
- Create database models
- Set up migrations
🔄 Generator Workflow
-
Selection:
- Choose the generator from available options
- Plugin may offer multiple generators
-
Configuration:
- Enter required parameters
- Set default values
- Configure model names
-
Generation:
- Create new files
- Update existing files
- Modify Prisma schema
-
Post-Generation:
- Show success messages
- Display next steps
- Update Swagger documentation
📝 Example Generation Process
-
Start Generation:
cd src/api
tsdiapi generate <pluginName> <generatorName> -
Select Generator:
? Select generator:
❯ generator1
generator2
generator3 -
Configure Parameters:
? Enter required parameter: value
? Choose option: option1 -
Generated Files:
src/api/feature/
├── feature.controller.ts
├── feature.service.ts
├── feature.module.ts
└── feature.schema.ts
🔒 Best Practices
-
Naming Conventions:
- Use kebab-case for generator names
- Follow consistent file naming
- Use descriptive parameter names
-
File Organization:
- Keep generator templates organized
- Use clear directory structure
- Document template variables
-
Error Handling:
- Validate inputs
- Provide clear error messages
- Handle file conflicts
-
Documentation:
- Document generator purpose
- Explain required parameters
- Provide usage examples