AIM Code Generation Templates Guide 📝¶
AIM Templates provide a fast, consistent way to generate source files, configuration boilerplates, or mock tests using interactive prompts and variables.
🏗️ Template Structure¶
All templates are stored inside .ai-context/templates/. Each template is a directory containing:
1. _template.yaml: The template configuration file specifying prompts, target destination, and generation actions.
2. Handlebars Source Files (e.g., main.hbs): File templates containing placeholders like {{name}} or {{kebabCase name}} that are compiled into output files.
📄 1. The Configuration Schema (_template.yaml)¶
Here is a fully-featured example of a _template.yaml file:
# Template configuration name
name: api-endpoint
description: Create an Express/TS API Endpoint
version: 1.0.0
# Target directory relative to project root
destination: src/controllers
# Interactive prompts asked if variables are not supplied via CLI
prompts:
- name: name
type: text
message: "Endpoint name?"
validate: required
- name: method
type: text
message: "HTTP Method?"
default: "GET"
# Actions to perform
actions:
- type: add
template: "controller.hbs"
path: "{{kebabCase name}}.controller.ts"
skipIfExists: true
- type: add
template: "route.hbs"
path: "../routes/{{kebabCase name}}.route.ts"
skipIfExists: true
# Custom feedback messages
messages:
success: |
Endpoint {{pascalCase name}} created successfully!
Generated controller at: src/controllers/{{kebabCase name}}.controller.ts
Generated route at: src/routes/{{kebabCase name}}.route.ts
Prompt Options¶
name: Variable key.type: Variable input type (e.g.text).message: Prompt message printed in terminal.default: Fallback value if user presses Enter.validate: Set torequiredto force input.
Action Options¶
type: Currently supportsadd(compile and create file).template: Source template filename inside the template folder.path: Rendered target output path (relative to the configureddestinationpath).skipIfExists: Set totrueto avoid overwriting existing files.
🔠 2. Variable Formatting Helpers¶
AIM supports several formatting helpers inside template placeholders {{ helper variable }}:
| Helper | Description | Input Example | Output Example |
|---|---|---|---|
kebabCase |
Convert to kebab-case | "Auth Handler" |
auth-handler |
camelCase |
Convert to camelCase | "Auth Handler" |
authHandler |
pascalCase |
Convert to PascalCase | "Auth Handler" |
AuthHandler |
snakeCase |
Convert to snake_case | "Auth Handler" |
auth_handler |
lowerCase |
Convert to lowercase | "Auth Handler" |
auth handler |
upperCase |
Convert to uppercase | "Auth Handler" |
AUTH HANDLER |
🚀 3. Step-by-Step Tutorial¶
Step 3.1: Create Template Scaffold¶
Create a new template named service-layer:
.ai-context/templates/service-layer/ containing default _template.yaml and main.hbs files.
Step 3.2: Configure the Files¶
Let's modify the generated .ai-context/templates/service-layer/_template.yaml:
name: service-layer
description: Create a TypeScript Service class
destination: src/services
prompts:
- name: name
type: text
message: "Service name?"
validate: required
actions:
- type: add
template: "service.hbs"
path: "{{pascalCase name}}Service.ts"
skipIfExists: true
And modify the source template file .ai-context/templates/service-layer/service.hbs:
// Generated by AIM template: service-layer
// Service: {{pascalCase name}}Service
export class {{pascalCase name}}Service {
private serviceName: string = "{{kebabCase name}}";
constructor() {
console.log(`${this.serviceName} service initialized.`);
}
public async execute(): Promise<void> {
// Implement service logic here
}
}
Step 3.3: Dry-Run Verification¶
Verify your output file path and rendering preview without writing files to disk:
Output:
[*] Dry-run mode: Preview of files that would be generated:
-----------------------------------------------------------------
Target Path: src\services\UserAuthenticationService.ts
Content Preview:
// Generated by AIM template: service-layer
// Service: UserAuthenticationService
export class UserAuthenticationService {
private serviceName: string = "user-authentication";
constructor() {
console.log(`${this.serviceName} service initialized.`);
}
public async execute(): Promise<void> {
// Implement service logic here
}
}
-----------------------------------------------------------------
Step 3.4: Generate the File¶
Once verified, run it for real to write files to disk:
Output: