Open History to see the last commit
1import {
2 cancel,
3 confirm,
4 isCancel,
5 multiselect,
6 select,
7 text,
8} from '@clack/prompts'
9
10import {
11 DEFAULT_PACKAGE_MANAGER,
12 SUPPORTED_PACKAGE_MANAGERS,
13 getAllAddOns,
14} from '@tanstack/cta-engine'
15
16import type { AddOn, PackageManager } from '@tanstack/cta-engine'
17
18import type { Framework } from '@tanstack/cta-engine/dist/types/types.js'
19
20export async function getProjectName(): Promise<string> {
21 const value = await text({
22 message: 'What would you like to name your project?',
23 defaultValue: 'my-app',
24 validate(value) {
25 if (!value) {
26 return 'Please enter a name'
27 }
28 },
29 })
30
31 if (isCancel(value)) {
32 cancel('Operation cancelled.')
33 process.exit(0)
34 }
35
36 return value
37}
38
39export async function selectRouterType(): Promise<string> {
40 const routerType = await select({
41 message: 'Select the router type:',
42 options: [
43 {
44 value: 'file-router',
45 label: 'File Router - File-based routing structure',
46 },
47 {
48 value: 'code-router',
49 label: 'Code Router - Traditional code-based routing',
50 },
51 ],
52 initialValue: 'file-router',
53 })
54
55 if (isCancel(routerType)) {
56 cancel('Operation cancelled.')
57 process.exit(0)
58 }
59
60 return routerType
61}
62
63export async function selectTypescript(): Promise<boolean> {
64 const typescriptEnable = await confirm({
65 message: 'Would you like to use TypeScript?',
66 initialValue: true,
67 })
68 if (isCancel(typescriptEnable)) {
69 cancel('Operation cancelled.')
70 process.exit(0)
71 }
72 return typescriptEnable
73}
74
75export async function selectTailwind(): Promise<boolean> {
76 const tailwind = await confirm({
77 message: 'Would you like to use Tailwind CSS?',
78 initialValue: true,
79 })
80
81 if (isCancel(tailwind)) {
82 cancel('Operation cancelled.')
83 process.exit(0)
84 }
85
86 return tailwind
87}
88
89export async function selectPackageManager(): Promise<PackageManager> {
90 const packageManager = await select({
91 message: 'Select package manager:',
92 options: SUPPORTED_PACKAGE_MANAGERS.map((pm) => ({
93 value: pm,
94 label: pm,
95 })),
96 initialValue: DEFAULT_PACKAGE_MANAGER,
97 })
98 if (isCancel(packageManager)) {
99 cancel('Operation cancelled.')
100 process.exit(0)
101 }
102 return packageManager
103}
104
105export async function selectAddOns(
106 framework: Framework,
107 mode: string,
108 type: string,
109 message: string,
110 forcedAddOns: Array<string> = [],
111): Promise<Array<string>> {
112 const allAddOns = await getAllAddOns(framework, mode)
113 const addOns = allAddOns.filter((addOn) => addOn.type === type)
114 if (addOns.length === 0) {
115 return []
116 }
117
118 const value = await multiselect({
119 message,
120 options: addOns
121 .filter((addOn) => !forcedAddOns.includes(addOn.id))
122 .map((addOn) => ({
123 value: addOn.id,
124 label: addOn.name,
125 hint: addOn.description,
126 })),
127 required: false,
128 })
129
130 if (isCancel(value)) {
131 cancel('Operation cancelled.')
132 process.exit(0)
133 }
134
135 return value
136}
137
138export async function selectGit(): Promise<boolean> {
139 const git = await confirm({
140 message: 'Would you like to initialize a new git repository?',
141 initialValue: true,
142 })
143 if (isCancel(git)) {
144 cancel('Operation cancelled.')
145 process.exit(0)
146 }
147 return git
148}
149
150export async function selectToolchain(
151 framework: Framework,
152 toolchain?: string,
153): Promise<string | undefined> {
154 const toolchains = new Set<AddOn>()
155 for (const addOn of framework.getAddOns()) {
156 if (addOn.type === 'toolchain') {
157 toolchains.add(addOn)
158 if (toolchain && addOn.id === toolchain) {
159 return toolchain
160 }
161 }
162 }
163
164 const tc = await select({
165 message: 'Select toolchain',
166 options: [
167 {
168 value: undefined,
169 label: 'None',
170 },
171 ...Array.from(toolchains).map((tc) => ({
172 value: tc.id,
173 label: tc.name,
174 })),
175 ],
176 initialValue: undefined,
177 })
178
179 if (isCancel(tc)) {
180 cancel('Operation cancelled.')
181 process.exit(0)
182 }
183
184 return tc
185}