This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate.ts
More file actions
482 lines (421 loc) · 12.8 KB
/
create.ts
File metadata and controls
482 lines (421 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import { resolve } from 'node:path'
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
import {
cancel,
confirm,
intro,
isCancel,
log,
multiselect,
note,
outro,
select,
spinner,
text,
} from '@clack/prompts'
import chalk from 'chalk'
import { BINARY_PREFIX, fetchIntegrations, fetchManifest } from '../api/fetch.js'
import { compile } from '../engine/compile.js'
import { writeConfigFile } from '../engine/config-file.js'
import { loadTemplate } from '../engine/custom-addons/template.js'
import type {
CustomTemplateCompiled,
IntegrationCompiled,
ManifestIntegration,
PackageManager,
RouterMode,
} from '../engine/types.js'
interface CreateOptions {
template?: string
packageManager?: PackageManager
integrations?: string
install?: boolean
git?: boolean
tailwind?: boolean
yes?: boolean
targetDir?: string
integrationsPath?: string
}
const PACKAGE_MANAGERS: Array<PackageManager> = [
'pnpm',
'npm',
'yarn',
'bun',
'deno',
]
function detectPackageManager(): PackageManager {
const userAgent = process.env.npm_config_user_agent
if (userAgent) {
if (userAgent.includes('pnpm')) return 'pnpm'
if (userAgent.includes('yarn')) return 'yarn'
if (userAgent.includes('bun')) return 'bun'
if (userAgent.includes('deno')) return 'deno'
}
return 'npm'
}
function validateProjectName(name: string): { valid: boolean; error?: string } {
if (!name) {
return { valid: false, error: 'Project name is required' }
}
if (!/^[a-z0-9-_]+$/.test(name)) {
return {
valid: false,
error:
'Project name can only contain lowercase letters, numbers, hyphens, and underscores',
}
}
return { valid: true }
}
function groupIntegrationsByCategory(
integrations: Array<ManifestIntegration>,
): Record<string, Array<ManifestIntegration>> {
const groups: Record<string, Array<ManifestIntegration>> = {}
for (const integration of integrations) {
const category = integration.category ?? 'other'
groups[category] ??= []
groups[category].push(integration)
}
return groups
}
const CATEGORY_LABELS: Record<string, string> = {
tanstack: 'TanStack',
database: 'Database',
orm: 'ORM',
auth: 'Authentication',
deploy: 'Deployment',
tooling: 'Tooling',
monitoring: 'Monitoring',
api: 'API',
i18n: 'Internationalization',
cms: 'CMS',
other: 'Other',
}
const CATEGORY_ORDER = [
'tanstack',
'database',
'orm',
'auth',
'deploy',
'api',
'monitoring',
'tooling',
'i18n',
'cms',
'other',
]
export async function runCreate(
projectName: string | undefined,
options: CreateOptions,
): Promise<void> {
intro(chalk.bgCyan(chalk.black(' TanStack Start ')))
const s = spinner()
// Determine integrations source
const integrationsPath = options.integrationsPath
// Fetch manifest
s.start('Fetching available integrations...')
let manifest
try {
manifest = await fetchManifest(integrationsPath)
s.stop('Integrations loaded')
} catch (error) {
s.stop('Failed to fetch integrations')
log.error(
'Could not fetch integration manifest. Check your internet connection.',
)
process.exit(1)
}
// Project name
let name = projectName
if (!name && !options.yes) {
const nameInput = await text({
message: 'Project name:',
placeholder: 'my-tanstack-app',
defaultValue: 'my-tanstack-app',
validate: (value) => {
const result = validateProjectName(value)
return result.valid ? undefined : result.error
},
})
if (isCancel(nameInput)) {
cancel('Operation cancelled.')
process.exit(0)
}
name = nameInput
}
name = name ?? 'my-tanstack-app'
const { valid, error } = validateProjectName(name)
if (!valid) {
log.error(error ?? 'Invalid project name')
process.exit(1)
}
// Target directory
const targetDir = options.targetDir
? resolve(options.targetDir)
: resolve(process.cwd(), name)
if (existsSync(targetDir)) {
if (!options.yes) {
const overwrite = await confirm({
message: `Directory ${chalk.cyan(name)} already exists. Overwrite?`,
initialValue: false,
})
if (isCancel(overwrite) || !overwrite) {
cancel('Operation cancelled.')
process.exit(0)
}
}
}
// Package manager
let packageManager: PackageManager = options.packageManager ?? detectPackageManager()
if (!options.packageManager && !options.yes) {
const pmChoice = await select({
message: 'Package manager:',
options: PACKAGE_MANAGERS.map((pm) => ({
value: pm,
label: pm,
})),
initialValue: packageManager,
})
if (isCancel(pmChoice)) {
cancel('Operation cancelled.')
process.exit(0)
}
packageManager = pmChoice
}
// Tailwind
let tailwind = options.tailwind ?? true
if (options.tailwind === undefined && !options.yes) {
const twChoice = await confirm({
message: 'Include Tailwind CSS?',
initialValue: true,
})
if (isCancel(twChoice)) {
cancel('Operation cancelled.')
process.exit(0)
}
tailwind = twChoice
}
// Integrations selection
let selectedIntegrationIds: Array<string> = []
if (options.integrations) {
selectedIntegrationIds = options.integrations.split(',').map((s) => s.trim())
} else if (!options.yes) {
// Group integrations by category for display
const availableIntegrations = manifest.integrations.filter(
(a) => a.type === 'integration' && a.modes.includes('file-router'),
)
const grouped = groupIntegrationsByCategory(availableIntegrations)
// Show category selection first
const categoryOptions = CATEGORY_ORDER.filter((cat) => grouped[cat]?.length)
.map((cat) => ({
value: cat,
label: CATEGORY_LABELS[cat] ?? cat,
hint: `${grouped[cat]!.length} integrations`,
}))
note(
'Use arrow keys to navigate, space to select/deselect, enter to confirm.',
'Integration Selection',
)
const selectedCategories = await multiselect({
message: 'Which categories would you like to explore?',
options: categoryOptions,
required: false,
})
if (isCancel(selectedCategories)) {
cancel('Operation cancelled.')
process.exit(0)
}
// For each selected category, show integration selection
for (const category of selectedCategories) {
const categoryIntegrations = grouped[category]
if (!categoryIntegrations?.length) continue
const integrationChoices = await multiselect({
message: `Select ${CATEGORY_LABELS[category]} integrations:`,
options: categoryIntegrations.map((integration) => ({
value: integration.id,
label: integration.name,
hint: integration.description,
})),
required: false,
})
if (isCancel(integrationChoices)) {
cancel('Operation cancelled.')
process.exit(0)
}
selectedIntegrationIds.push(...(integrationChoices))
}
}
// Resolve dependencies
const integrationMap = new Map<string, ManifestIntegration>(
manifest.integrations.map((a) => [a.id, a]),
)
const resolvedIds = new Set(selectedIntegrationIds)
for (const id of selectedIntegrationIds) {
const integration = integrationMap.get(id)
if (integration?.dependsOn) {
for (const dep of integration.dependsOn) {
resolvedIds.add(dep)
}
}
}
// Show summary
if (resolvedIds.size > 0) {
const integrationList = Array.from(resolvedIds)
.map((id) => {
const integration = integrationMap.get(id)
const isDep =
!selectedIntegrationIds.includes(id) && resolvedIds.has(id)
return ` ${isDep ? chalk.dim('(auto)') : chalk.green('+')} ${integration?.name ?? id}`
})
.join('\n')
note(integrationList, 'Selected Integrations')
}
// Confirm
if (!options.yes) {
const proceed = await confirm({
message: 'Create project?',
initialValue: true,
})
if (isCancel(proceed) || !proceed) {
cancel('Operation cancelled.')
process.exit(0)
}
}
// Fetch full addon definitions
s.start('Preparing project...')
// Load custom template if provided
let customTemplate: CustomTemplateCompiled | undefined
if (options.template) {
try {
customTemplate = await loadTemplate(options.template)
// Add template's integrations to resolved integrations
for (const integrationId of customTemplate.integrations) {
resolvedIds.add(integrationId)
}
// Use template's settings as defaults
tailwind = customTemplate.tailwind
} catch (error) {
s.stop('Failed to load template')
log.error(
error instanceof Error
? error.message
: 'Could not load template from URL.',
)
process.exit(1)
}
}
let chosenIntegrations: Array<IntegrationCompiled> = []
if (resolvedIds.size > 0) {
try {
chosenIntegrations = await fetchIntegrations(Array.from(resolvedIds), integrationsPath)
} catch (error) {
s.stop('Failed to fetch integration details')
log.error('Could not fetch integration definitions.')
process.exit(1)
}
}
// Compile project - use custom template settings if provided
const compileOptions = {
projectName: name,
framework: customTemplate?.framework ?? 'react',
mode: customTemplate?.mode ?? ('file-router' as RouterMode),
typescript: customTemplate?.typescript ?? true,
tailwind,
packageManager,
chosenIntegrations,
integrationOptions: customTemplate?.integrationOptions ?? {},
customTemplate,
}
const output = compile(compileOptions)
s.stop('Project prepared')
// Write files
s.start('Writing files...')
mkdirSync(targetDir, { recursive: true })
for (const [filePath, content] of Object.entries(output.files)) {
const fullPath = resolve(targetDir, filePath)
const dir = resolve(fullPath, '..')
mkdirSync(dir, { recursive: true })
// Handle binary files (base64 encoded with prefix)
if (content.startsWith(BINARY_PREFIX)) {
const base64Data = content.slice(BINARY_PREFIX.length)
writeFileSync(fullPath, Buffer.from(base64Data, 'base64'))
} else {
writeFileSync(fullPath, content, 'utf-8')
}
}
// Write config file for integration/template creation
await writeConfigFile(targetDir, compileOptions)
s.stop('Files written')
// Initialize git
if (options.git !== false) {
s.start('Initializing git repository...')
try {
const { execSync } = await import('node:child_process')
execSync('git init', { cwd: targetDir, stdio: 'ignore' })
s.stop('Git initialized')
} catch {
s.stop('Git initialization skipped')
}
}
// Install dependencies
if (options.install !== false) {
s.start(`Installing dependencies with ${packageManager}...`)
const startTime = Date.now()
const interval = setInterval(() => {
const elapsed = Math.floor((Date.now() - startTime) / 1000)
s.message(`Installing dependencies with ${packageManager}... (${elapsed}s)`)
}, 1000)
try {
const { spawn } = await import('node:child_process')
const installCmd = packageManager === 'yarn' ? 'yarn' : packageManager
const args = packageManager === 'yarn' ? [] : ['install']
await new Promise<void>((resolve, reject) => {
const child = spawn(installCmd, args, {
cwd: targetDir,
stdio: 'ignore',
shell: true,
})
child.on('close', (code) => {
if (code === 0) resolve()
else reject(new Error(`Process exited with code ${code}`))
})
child.on('error', reject)
})
clearInterval(interval)
const total = Math.floor((Date.now() - startTime) / 1000)
s.stop(`Dependencies installed (${total}s)`)
} catch {
clearInterval(interval)
s.stop('Dependency installation failed')
log.warning(`Run "${packageManager} install" manually to install dependencies.`)
}
}
// Show next steps
const relativePath = resolve(process.cwd()) === targetDir ? '.' : name
outro(chalk.green('Project created successfully!'))
console.log()
console.log('Next steps:')
console.log()
if (relativePath !== '.') {
console.log(` ${chalk.cyan('cd')} ${relativePath}`)
}
if (options.install === false) {
console.log(` ${chalk.cyan(packageManager)} install`)
}
console.log(` ${chalk.cyan(packageManager)} ${packageManager === 'npm' ? 'run ' : ''}dev`)
console.log()
// Show env vars if any
if (output.envVars.length > 0) {
console.log(chalk.yellow('Environment variables needed:'))
console.log()
for (const envVar of output.envVars) {
console.log(` ${chalk.cyan(envVar.name)} - ${envVar.description}`)
}
console.log()
console.log(` Add these to your ${chalk.cyan('.env.local')} file.`)
console.log()
}
// Show warnings
for (const warning of output.warnings) {
log.warning(warning)
}
}