0
# Code Generation
1
2
Tools for generating new packages, applications, and workspace components with customizable templates and interactive prompts for rapid development setup.
3
4
## Capabilities
5
6
### Generate Command
7
8
Generate new applications, packages, or run custom generators with configurable templates and interactive workflows.
9
10
```bash { .api }
11
turbo generate [generator] [options]
12
turbo gen [generator] [options] # shorthand
13
turbo g [generator] [options] # shorter shorthand
14
15
# Interactive generation
16
turbo generate
17
18
# Generate with specific generator
19
turbo generate my-custom-generator
20
21
# Generate with configuration
22
turbo generate --config=./gen-config.js
23
```
24
25
**Generate Options:**
26
27
```bash { .api }
28
--tag <tag> # Generator tag/version (default: "latest")
29
--config <file> / -c # Generator configuration file
30
--root <path> / -r # Repository root directory
31
--args <args...> / -a # Arguments passed to generator
32
33
# Generation examples
34
turbo generate --tag=canary
35
turbo generate --config=./custom-gen.js my-generator
36
turbo generate my-generator --args="name=my-app" "template=react"
37
```
38
39
**Usage Examples:**
40
41
```bash
42
# Interactive generator selection
43
turbo generate
44
45
# Generate with specific configuration
46
turbo generate --config=./generators/workspace.js
47
48
# Generate with custom arguments
49
turbo generate my-app-generator --args="name=new-app" "framework=nextjs"
50
51
# Use canary version of generators
52
turbo generate --tag=canary my-generator
53
```
54
55
### Workspace Generation
56
57
Create new workspaces (packages/applications) within your monorepo with template-based scaffolding.
58
59
```bash { .api }
60
turbo generate workspace [options]
61
turbo gen workspace [options] # shorthand
62
turbo gen w [options] # shorter shorthand
63
64
# Generate empty workspace
65
turbo generate workspace --name=my-new-app --empty
66
67
# Generate workspace from template
68
turbo generate workspace --name=web-app --copy=@myorg/template-app
69
70
# Generate workspace from GitHub template
71
turbo generate workspace --copy="https://github.com/user/repo/tree/main/apps/example"
72
```
73
74
**Workspace Options:**
75
76
```bash { .api }
77
--name <name> / -n # Workspace name
78
--empty / -b # Generate empty workspace (default: true)
79
--copy <source> / -c # Copy from existing workspace or GitHub URL
80
--destination <path> / -d # Output destination directory
81
--type <type> / -t # Workspace type
82
--root <path> / -r # Repository root
83
--example-path <path> / -p # GitHub subdirectory path
84
--show-all-dependencies # Show all available dependencies
85
```
86
87
**Workspace Source Types:**
88
89
```bash { .api }
90
# Local workspace as template
91
--copy=@myorg/existing-app
92
93
# GitHub repository (main branch)
94
--copy="https://github.com/user/repo"
95
96
# GitHub repository with specific branch
97
--copy="https://github.com/user/repo/tree/feature-branch"
98
99
# GitHub repository with subdirectory
100
--copy="https://github.com/user/repo/tree/main/apps/example"
101
102
# GitHub with branch and subdirectory (use --example-path)
103
--copy="https://github.com/user/repo/tree/feature/branch"
104
--example-path="apps/example"
105
```
106
107
**Usage Examples:**
108
109
```bash
110
# Create empty React application
111
turbo generate workspace --name=new-react-app --type=app --empty
112
113
# Copy from existing local workspace
114
turbo generate workspace --name=admin-panel --copy=@myorg/web-app
115
116
# Copy from GitHub template
117
turbo generate workspace \
118
--name=new-service \
119
--copy="https://github.com/company/templates/tree/main/services/api"
120
121
# Generate library with custom destination
122
turbo generate workspace \
123
--name=ui-components \
124
--type=lib \
125
--destination=packages/ui
126
127
# Copy complex GitHub template with specific path
128
turbo generate workspace \
129
--name=mobile-app \
130
--copy="https://github.com/company/templates/tree/feature/mobile" \
131
--example-path="apps/react-native"
132
```
133
134
### Custom Generators
135
136
Run custom generators defined in your repository or external packages.
137
138
```bash { .api }
139
turbo generate run [generator] [options]
140
turbo gen run [generator] [options] # shorthand
141
turbo gen r [generator] [options] # shorter shorthand
142
143
# Run custom generator
144
turbo generate run my-component-generator
145
146
# Run with custom config and arguments
147
turbo generate run api-generator \
148
--config=./gen/api-config.js \
149
--args="service=user" "database=postgres"
150
```
151
152
**Custom Generator Options:**
153
154
```bash { .api }
155
--config <file> / -c # Generator configuration file
156
--root <path> / -r # Repository root
157
--args <args...> / -a # Arguments passed to generator
158
```
159
160
**Usage Examples:**
161
162
```bash
163
# Run component generator
164
turbo generate run component-generator --args="name=Button" "variant=primary"
165
166
# Run API generator with config
167
turbo generate run api-generator \
168
--config=./generators/api.config.js \
169
--args="resource=user" "crud=true"
170
171
# Run custom page generator
172
turbo generate run page-generator --args="route=/dashboard" "auth=required"
173
```
174
175
### Generator Configuration
176
177
Configure generators through configuration files and workspace settings.
178
179
```bash { .api }
180
# Generator configuration file examples
181
./turbo/generators/config.js # Default generator config location
182
./generators/custom.config.js # Custom config file location
183
184
# Usage with configuration
185
turbo generate --config=./turbo/generators/workspace.js
186
turbo generate workspace --config=./generators/component.js
187
```
188
189
**Configuration Examples:**
190
191
```javascript
192
// Example generator configuration (JavaScript)
193
module.exports = {
194
name: 'component-generator',
195
description: 'Generates React components with TypeScript',
196
prompts: [
197
{
198
type: 'input',
199
name: 'name',
200
message: 'Component name:',
201
},
202
{
203
type: 'select',
204
name: 'variant',
205
message: 'Component variant:',
206
choices: ['basic', 'form', 'layout'],
207
},
208
],
209
actions: [
210
{
211
type: 'add',
212
path: 'components/{{kebabCase name}}/{{pascalCase name}}.tsx',
213
templateFile: 'templates/component.hbs',
214
},
215
],
216
};
217
```
218
219
## Generator Types
220
221
```typescript { .api }
222
interface GenerateOptions {
223
tag: string;
224
generator_name?: string;
225
config?: string;
226
root?: string;
227
args: string[];
228
command?: GenerateCommand;
229
}
230
231
interface GenerateWorkspaceOptions {
232
name?: string;
233
empty: boolean;
234
copy?: string;
235
destination?: string;
236
type?: string;
237
root?: string;
238
example_path?: string;
239
show_all_dependencies: boolean;
240
}
241
242
interface GeneratorCustomOptions {
243
generator_name?: string;
244
config?: string;
245
root?: string;
246
args: string[];
247
}
248
249
interface GeneratorConfig {
250
name: string;
251
description?: string;
252
prompts?: GeneratorPrompt[];
253
actions: GeneratorAction[];
254
}
255
256
interface GeneratorPrompt {
257
type: "input" | "select" | "multiselect" | "confirm";
258
name: string;
259
message: string;
260
choices?: string[] | PromptChoice[];
261
default?: any;
262
validate?: (input: any) => boolean | string;
263
}
264
265
interface PromptChoice {
266
name: string;
267
value: any;
268
disabled?: boolean;
269
}
270
271
interface GeneratorAction {
272
type: "add" | "modify" | "delete";
273
path: string;
274
template?: string;
275
templateFile?: string;
276
data?: Record<string, any>;
277
}
278
279
interface WorkspaceTemplate {
280
name: string;
281
path: string;
282
type: "app" | "lib" | "tool";
283
framework?: string;
284
language?: string;
285
dependencies: string[];
286
}
287
```
288
289
## Template System
290
291
Turbo generators support various templating approaches:
292
293
**Template Engines:**
294
- Handlebars templates (`.hbs`)
295
- EJS templates (`.ejs`)
296
- Plain text with variable substitution
297
298
**Built-in Helpers:**
299
- `{{pascalCase name}}` - PascalCase conversion
300
- `{{camelCase name}}` - camelCase conversion
301
- `{{kebabCase name}}` - kebab-case conversion
302
- `{{snakeCase name}}` - snake_case conversion
303
- `{{upperCase name}}` - UPPERCASE conversion
304
305
**Template Variables:**
306
- Generator arguments passed via `--args`
307
- User responses from interactive prompts
308
- Built-in variables (workspace root, package manager, etc.)
309
310
**File Operations:**
311
- Add new files from templates
312
- Modify existing files with patches
313
- Delete files and directories
314
- Copy files and directory structures