Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

ui-prompts.ts

319 lines7.3 KB

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
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
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
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
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
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 allowMultiple: boolean = true,
123): Promise<Array<string>> {
124 const allAddOns = await getAllAddOns(framework, mode)
125 const addOns = allAddOns.filter((addOn) => addOn.type === type)
126 if (addOns.length === 0) {
127 return []
128 }
129​
130 // Show help text only once
131 if (!hasShownMultiselectHelp) {
132 note(
133 'Use ↑/↓ to navigate • Space to select/deselect • Enter to confirm',
134 'Keyboard Shortcuts',
135 )
136 hasShownMultiselectHelp = true
137 }
138​
139 if (allowMultiple) {
140 const value = await multiselect({
141 message,
142 options: addOns
143 .filter((addOn) => !forcedAddOns.includes(addOn.id))
144 .map((addOn) => ({
145 value: addOn.id,
146 label: addOn.name,
147 hint: addOn.description,
148 })),
149 required: false,
150 })
151​
152 if (isCancel(value)) {
153 cancel('Operation cancelled.')
154 process.exit(0)
155 }
156​
157 return value
158 } else {
159 const value = await select({
160 message,
161 options: [
162 {
163 value: 'none',
164 label: 'None',
165 },
166 ...addOns
167 .filter((addOn) => !forcedAddOns.includes(addOn.id))
168 .map((addOn) => ({
169 value: addOn.id,
170 label: addOn.name,
171 hint: addOn.description,
172 })),
173 ],
174 initialValue: 'none',
175 })
176​
177 if (isCancel(value)) {
178 cancel('Operation cancelled.')
179 process.exit(0)
180 }
181​
182 return value === 'none' ? [] : [value]
183 }
185​
186export async function selectGit(): Promise<boolean> {
187 const git = await confirm({
188 message: 'Would you like to initialize a new git repository?',
189 initialValue: true,
190 })
191 if (isCancel(git)) {
192 cancel('Operation cancelled.')
193 process.exit(0)
194 }
195 return git
197​
198export async function selectToolchain(
199 framework: Framework,
200 toolchain?: string,
201): Promise<string | undefined> {
202 const toolchains = new Set<AddOn>()
203 for (const addOn of framework.getAddOns()) {
204 if (addOn.type === 'toolchain') {
205 toolchains.add(addOn)
206 if (toolchain && addOn.id === toolchain) {
207 return toolchain
208 }
209 }
210 }
211​
212 const tc = await select({
213 message: 'Select toolchain',
214 options: [
215 {
216 value: undefined,
217 label: 'None',
218 },
219 ...Array.from(toolchains).map((tc) => ({
220 value: tc.id,
221 label: tc.name,
222 })),
223 ],
224 initialValue: undefined,
225 })
226​
227 if (isCancel(tc)) {
228 cancel('Operation cancelled.')
229 process.exit(0)
230 }
231​
232 return tc
234​
235export async function promptForAddOnOptions(
236 addOnIds: Array<string>,
237 framework: Framework,
238): Promise<Record<string, Record<string, any>>> {
239 const addOnOptions: Record<string, Record<string, any>> = {}
240​
241 for (const addOnId of addOnIds) {
242 const addOn = framework.getAddOns().find((a) => a.id === addOnId)
243 if (!addOn || !addOn.options) continue
244​
245 addOnOptions[addOnId] = {}
246​
247 for (const [optionName, option] of Object.entries(addOn.options)) {
248 if (option && typeof option === 'object' && 'type' in option) {
249 if (option.type === 'select') {
250 const selectOption = option as {
251 type: 'select'
252 label: string
253 description?: string
254 default: string
255 options: Array<{ value: string; label: string }>
256 }
257​
258 const value = await select({
259 message: `${addOn.name}: ${selectOption.label}`,
260 options: selectOption.options.map((opt) => ({
261 value: opt.value,
262 label: opt.label,
263 })),
264 initialValue: selectOption.default,
265 })
266​
267 if (isCancel(value)) {
268 cancel('Operation cancelled.')
269 process.exit(0)
270 }
271​
272 addOnOptions[addOnId][optionName] = value
273 }
274 // Future option types can be added here
275 }
276 }
277 }
278​
279 return addOnOptions
281​
282export async function selectDeployment(
283 framework: Framework,
284 deployment?: string,
285): Promise<string | undefined> {
286 const deployments = new Set<AddOn>()
287 let initialValue: string | undefined = undefined
288 for (const addOn of framework
289 .getAddOns()
290 .sort((a, b) => a.name.localeCompare(b.name))) {
291 if (addOn.type === 'deployment') {
292 deployments.add(addOn)
293 if (deployment && addOn.id === deployment) {
294 return deployment
295 }
296 if (addOn.default) {
297 initialValue = addOn.id
298 }
299 }
300 }
301​
302 const dp = await select({
303 message: 'Select deployment adapter',
304 options: [
305 ...Array.from(deployments).map((d) => ({
306 value: d.id,
307 label: d.name,
308 })),
309 ],
310 initialValue: initialValue,
311 })
312​
313 if (isCancel(dp)) {
314 cancel('Operation cancelled.')
315 process.exit(0)
316 }
317​
318 return dp