Open History to see the last commit
1import {
2 cancel,
3 confirm,
4 isCancel,
5 multiselect,
6 note,
7 select,
8 text,
9} from '@clack/prompts'
10
11import {
12 DEFAULT_PACKAGE_MANAGER,
13 SUPPORTED_PACKAGE_MANAGERS,
14 getAllAddOns,
15} from '@tanstack/cta-engine'
16
17import { validateProjectName } from './utils.js'
18import type { AddOn, PackageManager } from '@tanstack/cta-engine'
19
20import type { Framework } from '@tanstack/cta-engine/dist/types/types.js'
21import { InitialData } from '../../cta-ui/src/types'
22
23export async function getProjectName(): Promise<string> {
24 const value = await text({
25 message: 'What would you like to name your project?',
26 defaultValue: 'my-app',
27 validate(value) {
28 if (!value) {
29 return 'Please enter a name'
30 }
31
32 const { valid, error } = validateProjectName(value)
33 if (!valid) {
34 return error
35 }
36 },
37 })
38
39 if (isCancel(value)) {
40 cancel('Operation cancelled.')
41 process.exit(0)
42 }
43
44 return value
45}
46
47export async function selectRouterType(): Promise<string> {
48 const routerType = await select({
49 message: 'Select the router type:',
50 options: [
51 {
52 value: 'file-router',
53 label: 'File Router - File-based routing structure',
54 },
55 {
56 value: 'code-router',
57 label: 'Code Router - Traditional code-based routing',
58 },
59 ],
60 initialValue: 'file-router',
61 })
62
63 if (isCancel(routerType)) {
64 cancel('Operation cancelled.')
65 process.exit(0)
66 }
67
68 return routerType
69}
70
71export async function selectTypescript(): Promise<boolean> {
72 const typescriptEnable = await confirm({
73 message: 'Would you like to use TypeScript?',
74 initialValue: true,
75 })
76 if (isCancel(typescriptEnable)) {
77 cancel('Operation cancelled.')
78 process.exit(0)
79 }
80 return typescriptEnable
81}
82
83export async function selectTailwind(): Promise<boolean> {
84 const tailwind = await confirm({
85 message: 'Would you like to use Tailwind CSS?',
86 initialValue: true,
87 })
88
89 if (isCancel(tailwind)) {
90 cancel('Operation cancelled.')
91 process.exit(0)
92 }
93
94 return tailwind
95}
96
97export async function selectPackageManager(): Promise<PackageManager> {
98 const packageManager = await select({
99 message: 'Select package manager:',
100 options: SUPPORTED_PACKAGE_MANAGERS.map((pm) => ({
101 value: pm,
102 label: pm,
103 })),
104 initialValue: DEFAULT_PACKAGE_MANAGER,
105 })
106 if (isCancel(packageManager)) {
107 cancel('Operation cancelled.')
108 process.exit(0)
109 }
110 return packageManager
111}
112
113// Track if we've shown the multiselect help text
114let hasShownMultiselectHelp = false
115
116export async function selectAddOns(
117 framework: Framework,
118 mode: string,
119 type: string,
120 message: string,
121 forcedAddOns: Array<string> = [],
122): Promise<Array<string>> {
123 const allAddOns = await getAllAddOns(framework, mode)
124 const addOns = allAddOns.filter((addOn) => addOn.type === type)
125 if (addOns.length === 0) {
126 return []
127 }
128
129 // Show help text only once
130 if (!hasShownMultiselectHelp) {
131 note('Use ↑/↓ to navigate • Space to select/deselect • Enter to confirm', 'Keyboard Shortcuts')
132 hasShownMultiselectHelp = true
133 }
134
135 const value = await multiselect({
136 message,
137 options: addOns
138 .filter((addOn) => !forcedAddOns.includes(addOn.id))
139 .map((addOn) => ({
140 value: addOn.id,
141 label: addOn.name,
142 hint: addOn.description,
143 })),
144 required: false,
145 })
146
147 if (isCancel(value)) {
148 cancel('Operation cancelled.')
149 process.exit(0)
150 }
151
152 return value
153}
154
155export async function selectGit(): Promise<boolean> {
156 const git = await confirm({
157 message: 'Would you like to initialize a new git repository?',
158 initialValue: true,
159 })
160 if (isCancel(git)) {
161 cancel('Operation cancelled.')
162 process.exit(0)
163 }
164 return git
165}
166
167export async function selectToolchain(
168 framework: Framework,
169 toolchain?: string,
170): Promise<string | undefined> {
171 const toolchains = new Set<AddOn>()
172 for (const addOn of framework.getAddOns()) {
173 if (addOn.type === 'toolchain') {
174 toolchains.add(addOn)
175 if (toolchain && addOn.id === toolchain) {
176 return toolchain
177 }
178 }
179 }
180
181 const tc = await select({
182 message: 'Select toolchain',
183 options: [
184 {
185 value: undefined,
186 label: 'None',
187 },
188 ...Array.from(toolchains).map((tc) => ({
189 value: tc.id,
190 label: tc.name,
191 })),
192 ],
193 initialValue: undefined,
194 })
195
196 if (isCancel(tc)) {
197 cancel('Operation cancelled.')
198 process.exit(0)
199 }
200
201 return tc
202}
203
204export async function promptForAddOnOptions(
205 addOnIds: Array<string>,
206 framework: Framework,
207): Promise<Record<string, Record<string, any>>> {
208 const addOnOptions: Record<string, Record<string, any>> = {}
209
210 for (const addOnId of addOnIds) {
211 const addOn = framework.getAddOns().find((a) => a.id === addOnId)
212 if (!addOn || !addOn.options) continue
213
214 addOnOptions[addOnId] = {}
215
216 for (const [optionName, option] of Object.entries(addOn.options)) {
217 if (option && typeof option === 'object' && 'type' in option) {
218 if (option.type === 'select') {
219 const selectOption = option as {
220 type: 'select'
221 label: string
222 description?: string
223 default: string
224 options: Array<{ value: string; label: string }>
225 }
226
227 const value = await select({
228 message: `${addOn.name}: ${selectOption.label}`,
229 options: selectOption.options.map((opt) => ({
230 value: opt.value,
231 label: opt.label,
232 })),
233 initialValue: selectOption.default,
234 })
235
236 if (isCancel(value)) {
237 cancel('Operation cancelled.')
238 process.exit(0)
239 }
240
241 addOnOptions[addOnId][optionName] = value
242 }
243 // Future option types can be added here
244 }
245 }
246 }
247
248 return addOnOptions
249}
250
251export async function selectDeployment(
252 framework: Framework,
253 deployment?: string,
254): Promise<string | undefined> {
255 const deployments = new Set<AddOn>()
256 let initialValue: string | undefined = undefined
257 for (const addOn of framework
258 .getAddOns()
259 .sort((a, b) => a.name.localeCompare(b.name))) {
260 if (addOn.type === 'deployment') {
261 deployments.add(addOn)
262 if (deployment && addOn.id === deployment) {
263 return deployment
264 }
265 if (addOn.default) {
266 initialValue = addOn.id
267 }
268 }
269 }
270
271 const dp = await select({
272 message: 'Select deployment adapter',
273 options: [
274 ...Array.from(deployments).map((d) => ({
275 value: d.id,
276 label: d.name,
277 })),
278 ],
279 initialValue: initialValue,
280 })
281
282 if (isCancel(dp)) {
283 cancel('Operation cancelled.')
284 process.exit(0)
285 }
286
287 return dp
288}