0
# Code Generation
1
2
Commands for generating code from templates and managing template libraries to accelerate development and maintain consistency across projects.
3
4
## Capabilities
5
6
### Generate from Template
7
8
Generate and scaffold files from a pre-defined template into the workspace.
9
10
```bash { .api }
11
moon generate <name> [dest] [--defaults] [--dry-run] [--force] [--template]
12
```
13
14
**Alias:** `moon g`
15
16
**Arguments:**
17
- `name` (required) - Name of the template to generate from
18
- `dest` (optional) - Destination path relative to workspace root or working directory
19
20
**Options:**
21
- `--defaults` - Use default values for all template variables instead of prompting
22
- `--dry-run` - Run the entire generation process without writing any files
23
- `--force` - Force overwrite any existing files at the destination
24
- `--template` - Create a new template instead of generating from an existing one
25
26
**Usage Examples:**
27
28
```bash
29
# Generate using interactive prompts
30
moon generate react-component
31
32
# Generate to specific destination
33
moon generate react-component ./src/components/NewComponent
34
35
# Use short alias
36
moon g nodejs-app ./apps/my-new-app
37
38
# Use defaults without prompting
39
moon generate typescript-library --defaults
40
41
# Preview generation without creating files
42
moon generate react-component --dry-run
43
44
# Force overwrite existing files
45
moon generate api-endpoint ./src/api/users --force
46
47
# Create a new template
48
moon generate my-template --template
49
```
50
51
### List Available Templates
52
53
Display all templates available for code generation in the workspace.
54
55
```bash { .api }
56
moon templates [--filter <PATTERN>] [--json]
57
```
58
59
**Options:**
60
- `--filter <PATTERN>` - Filter templates based on name or description pattern
61
- `--json` - Output template information in JSON format
62
63
**Usage Examples:**
64
65
```bash
66
# List all available templates
67
moon templates
68
69
# Filter templates by pattern
70
moon templates --filter react
71
72
# Get template info as JSON
73
moon templates --json
74
75
# Filter with wildcard pattern
76
moon templates --filter "*-component"
77
```
78
79
**Sample Output:**
80
```
81
Templates:
82
react-component Generate a React functional component with TypeScript
83
nodejs-api Generate a Node.js API server with Express
84
typescript-library Generate a TypeScript library package
85
nextjs-app Generate a Next.js application
86
documentation Generate documentation files and structure
87
```
88
89
## Template Structure
90
91
Moon templates are organized in the `.moon/templates/` directory:
92
93
```
94
.moon/templates/
95
├── react-component/
96
│ ├── template.yml # Template configuration
97
│ ├── {{name}}.tsx # Component file template
98
│ ├── {{name}}.test.tsx # Test file template
99
│ └── index.ts # Export file template
100
├── nodejs-api/
101
│ ├── template.yml
102
│ ├── src/
103
│ │ ├── app.ts
104
│ │ └── routes/
105
│ ├── package.json
106
│ └── README.md
107
└── shared/ # Shared template partials
108
├── license.txt
109
└── gitignore.txt
110
```
111
112
## Template Configuration
113
114
Templates are configured using `template.yml`:
115
116
```yaml
117
# Example template.yml
118
name: "React Component"
119
description: "Generate a React functional component with TypeScript"
120
destination: "src/components"
121
122
variables:
123
- name: "name"
124
description: "Component name"
125
type: "string"
126
required: true
127
128
- name: "withTests"
129
description: "Include test files"
130
type: "boolean"
131
default: true
132
133
- name: "styling"
134
description: "Styling approach"
135
type: "enum"
136
options: ["css-modules", "styled-components", "tailwind"]
137
default: "css-modules"
138
139
files:
140
- source: "{{name}}.tsx"
141
destination: "{{name}}/{{name}}.tsx"
142
143
- source: "{{name}}.test.tsx"
144
destination: "{{name}}/{{name}}.test.tsx"
145
condition: "{{withTests}}"
146
147
- source: "index.ts"
148
destination: "{{name}}/index.ts"
149
```
150
151
## Template Variables
152
153
Templates support various variable types:
154
155
- **String** - Text input from user
156
- **Boolean** - Yes/no questions
157
- **Enum** - Select from predefined options
158
- **Number** - Numeric input
159
- **Array** - Multiple values
160
161
Variables can be used in:
162
- File names (e.g., `{{name}}.tsx`)
163
- File contents (e.g., `export const {{name}} = () => {}`)
164
- Directory names (e.g., `{{name}}/`)
165
- Conditional file inclusion
166
167
## Template Syntax
168
169
Moon uses Handlebars-style templating:
170
171
```typescript
172
// In template file: {{name}}.tsx
173
import React from 'react';
174
{{#if withTests}}
175
import { render } from '@testing-library/react';
176
{{/if}}
177
178
interface {{name}}Props {
179
{{#each props}}
180
{{name}}: {{type}};
181
{{/each}}
182
}
183
184
export const {{name}}: React.FC<{{name}}Props> = ({
185
{{#each props}}{{name}},{{/each}}
186
}) => {
187
return (
188
<div className="{{kebabCase name}}">
189
{/* {{description}} */}
190
</div>
191
);
192
};
193
194
{{#if withTests}}
195
// Test file content
196
describe('{{name}}', () => {
197
it('renders correctly', () => {
198
render(<{{name}} />);
199
});
200
});
201
{{/if}}
202
```
203
204
## Helper Functions
205
206
Templates include built-in helper functions:
207
208
- `{{camelCase text}}` - Convert to camelCase
209
- `{{pascalCase text}}` - Convert to PascalCase
210
- `{{kebabCase text}}` - Convert to kebab-case
211
- `{{snakeCase text}}` - Convert to snake_case
212
- `{{upperCase text}}` - Convert to UPPER_CASE
213
- `{{lowerCase text}}` - Convert to lower case
214
215
## Creating Custom Templates
216
217
```bash
218
# Create a new template interactively
219
moon generate my-template --template
220
221
# Manual template creation
222
mkdir .moon/templates/my-template
223
touch .moon/templates/my-template/template.yml
224
225
# Edit template.yml with configuration
226
# Add template files with variable placeholders
227
```
228
229
## Template Sharing
230
231
Templates can be shared across projects:
232
233
```bash
234
# Export template
235
moon templates export react-component > react-component.tar.gz
236
237
# Import template
238
moon templates import react-component.tar.gz
239
240
# Sync templates from remote repository
241
moon templates sync --from https://github.com/company/moon-templates
242
```
243
244
## Best Practices
245
246
### Template Organization
247
- Group related templates in logical categories
248
- Use descriptive names and documentation
249
- Include example usage in template descriptions
250
- Version control templates with the workspace
251
252
### Variable Design
253
- Use clear, descriptive variable names
254
- Provide sensible defaults where possible
255
- Include help text for complex options
256
- Validate user input when necessary
257
258
### File Structure
259
- Follow consistent naming conventions
260
- Include necessary configuration files
261
- Provide complete, working examples
262
- Include appropriate documentation and comments