-
-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathoptions.ts
More file actions
257 lines (222 loc) · 6.5 KB
/
options.ts
File metadata and controls
257 lines (222 loc) · 6.5 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
import fs from 'node:fs'
import { cancel, confirm, intro, isCancel } from '@clack/prompts'
import {
finalizeAddOns,
getFrameworkById,
getPackageManager,
populateAddOnOptionsDefaults,
readConfigFile,
} from '@tanstack/cta-engine'
import {
getProjectName,
promptForAddOnOptions,
selectAddOns,
selectDeployment,
selectGit,
selectPackageManager,
selectRouterType,
selectTailwind,
selectToolchain,
selectTypescript,
} from './ui-prompts.js'
import {
getCurrentDirectoryName,
sanitizePackageName,
validateProjectName,
} from './utils.js'
import type { Options } from '@tanstack/cta-engine'
import type { CliOptions } from './types.js'
export async function promptForCreateOptions(
cliOptions: CliOptions,
{
forcedAddOns = [],
forcedMode,
showDeploymentOptions = false,
}: {
forcedAddOns?: Array<string>
forcedMode?: string
showDeploymentOptions?: boolean
},
): Promise<Required<Options> | undefined> {
const options = {} as Required<Options>
options.framework = getFrameworkById(cliOptions.framework || 'react-cra')!
// Validate project name
if (cliOptions.projectName) {
// Handle "." as project name - use sanitized current directory name
if (cliOptions.projectName === '.') {
options.projectName = sanitizePackageName(getCurrentDirectoryName())
} else {
options.projectName = cliOptions.projectName
}
const { valid, error } = validateProjectName(options.projectName)
if (!valid) {
console.error(error)
process.exit(1)
}
} else {
options.projectName = await getProjectName()
}
// Check if target directory is empty
if (
!cliOptions.force &&
fs.existsSync(options.projectName) &&
fs.readdirSync(options.projectName).length > 0
) {
const shouldContinue = await confirm({
message: `Target directory ${options.projectName} is not empty. Do you want to continue?`,
initialValue: true,
})
if (isCancel(shouldContinue) || !shouldContinue) {
cancel('Operation cancelled.')
process.exit(0)
}
}
// Router type selection
if (forcedMode) {
options.mode = forcedMode
} else if (cliOptions.template) {
options.mode =
cliOptions.template === 'file-router' ? 'file-router' : 'code-router'
} else {
options.mode = await selectRouterType()
}
// TypeScript selection (if using Code Router)
// TODO: Make this declarative
options.typescript =
options.mode === 'file-router' || options.framework.id === 'solid'
if (
forcedMode &&
options.framework.supportedModes[forcedMode].forceTypescript
) {
options.typescript = true
}
if (!options.typescript && options.mode === 'code-router') {
options.typescript = await selectTypescript()
}
// Package manager selection
if (cliOptions.packageManager) {
options.packageManager = cliOptions.packageManager
} else {
const detectedPackageManager = await getPackageManager()
options.packageManager =
detectedPackageManager || (await selectPackageManager())
}
// Toolchain selection
const toolchain = await selectToolchain(
options.framework,
cliOptions.toolchain,
)
// Deployment selection
const deployment = showDeploymentOptions
? await selectDeployment(options.framework, cliOptions.deployment)
: undefined
// Add-ons selection
const addOns: Set<string> = new Set()
if (toolchain) {
addOns.add(toolchain)
}
if (deployment) {
addOns.add(deployment)
}
for (const addOn of forcedAddOns) {
addOns.add(addOn)
}
if (Array.isArray(cliOptions.addOns)) {
for (const addOn of cliOptions.addOns) {
addOns.add(addOn)
}
} else {
for (const addOn of await selectAddOns(
options.framework,
options.mode,
'add-on',
'What add-ons would you like for your project?',
forcedAddOns,
)) {
addOns.add(addOn)
}
for (const addOn of await selectAddOns(
options.framework,
options.mode,
'example',
'Would you like an example?',
forcedAddOns,
false,
)) {
addOns.add(addOn)
}
}
options.chosenAddOns = Array.from(
await finalizeAddOns(options.framework, options.mode, Array.from(addOns)),
)
if (options.chosenAddOns.length) {
options.typescript = true
}
// Tailwind selection
// Only treat add-ons as requiring tailwind if they explicitly have "tailwind": true
const addOnsRequireTailwind = options.chosenAddOns.some(
(addOn) => addOn.tailwind === true,
)
if (addOnsRequireTailwind) {
// If any add-on explicitly requires tailwind, enable it automatically
options.tailwind = true
} else if (cliOptions.tailwind !== undefined) {
// User explicitly provided a CLI flag, respect it
options.tailwind = !!cliOptions.tailwind
} else if (options.framework.id === 'react-cra') {
// Only show prompt for react-cra when no CLI flag and no add-ons require it
options.tailwind = await selectTailwind()
} else {
// For other frameworks (like solid), default to true
options.tailwind = true
}
// Prompt for add-on options in interactive mode
if (Array.isArray(cliOptions.addOns)) {
// Non-interactive mode: use defaults
options.addOnOptions = populateAddOnOptionsDefaults(options.chosenAddOns)
} else {
// Interactive mode: prompt for options
const userOptions = await promptForAddOnOptions(
options.chosenAddOns.map((a) => a.id),
options.framework,
)
const defaultOptions = populateAddOnOptionsDefaults(options.chosenAddOns)
// Merge user options with defaults
options.addOnOptions = { ...defaultOptions, ...userOptions }
}
options.git = cliOptions.git || (await selectGit())
return options
}
export async function promptForAddOns(): Promise<Array<string>> {
const config = await readConfigFile(process.cwd())
if (!config) {
console.error('No config file found')
process.exit(1)
}
const framework = getFrameworkById(config.framework)
if (!framework) {
console.error(`Unknown framework: ${config.framework}`)
process.exit(1)
}
intro(`Adding new add-ons to '${config.projectName}'`)
const addOns: Set<string> = new Set()
for (const addOn of await selectAddOns(
framework,
config.mode!,
'add-on',
'What add-ons would you like for your project?',
config.chosenAddOns,
)) {
addOns.add(addOn)
}
for (const addOn of await selectAddOns(
framework,
config.mode!,
'example',
'Would you like any examples?',
config.chosenAddOns,
)) {
addOns.add(addOn)
}
return Array.from(addOns)
}