Open History to see the last commit
1import { intro } from '@clack/prompts'
2
3import {
4 finalizeAddOns,
5 getFrameworkById,
6 getPackageManager,
7 readConfigFile,
8} from '@tanstack/cta-engine'
9
10import {
11 getProjectName,
12 selectAddOns,
13 selectGit,
14 selectPackageManager,
15 selectRouterType,
16 selectTailwind,
17 selectToolchain,
18 selectTypescript,
19} from './ui-prompts.js'
20
21import type { Options } from '@tanstack/cta-engine'
22
23import type { CliOptions } from './types.js'
24
25export async function promptForCreateOptions(
26 cliOptions: CliOptions,
27 {
28 forcedAddOns = [],
29 forcedMode,
30 }: {
31 forcedAddOns?: Array<string>
32 forcedMode?: string
33 },
34): Promise<Required<Options> | undefined> {
35 const options = {} as Required<Options>
36
37 options.framework = getFrameworkById(cliOptions.framework || 'react-cra')!
38
39 options.projectName = cliOptions.projectName || (await getProjectName())
40
41 // Router type selection
42 if (forcedMode) {
43 options.mode = forcedMode
44 } else if (cliOptions.template) {
45 options.mode =
46 cliOptions.template === 'file-router' ? 'file-router' : 'code-router'
47 } else {
48 options.mode = await selectRouterType()
49 }
50
51 // TypeScript selection (if using Code Router)
52 // TODO: Make this declarative
53 options.typescript =
54 options.mode === 'file-router' || options.framework.id === 'solid'
55 if (
56 forcedMode &&
57 options.framework.supportedModes[forcedMode].forceTypescript
58 ) {
59 options.typescript = true
60 }
61 if (!options.typescript && options.mode === 'code-router') {
62 options.typescript = await selectTypescript()
63 }
64
65 // Tailwind selection
66 if (!cliOptions.tailwind && options.framework.id === 'react-cra') {
67 options.tailwind = await selectTailwind()
68 } else {
69 options.tailwind = true
70 }
71
72 // Package manager selection
73 if (cliOptions.packageManager) {
74 options.packageManager = cliOptions.packageManager
75 } else {
76 const detectedPackageManager = await getPackageManager()
77 options.packageManager =
78 detectedPackageManager || (await selectPackageManager())
79 }
80
81 // Toolchain selection
82 const toolchain = await selectToolchain(
83 options.framework,
84 cliOptions.toolchain,
85 )
86
87 // Add-ons selection
88 const addOns: Set<string> = new Set()
89
90 if (toolchain) {
91 addOns.add(toolchain)
92 }
93
94 for (const addOn of forcedAddOns) {
95 addOns.add(addOn)
96 }
97
98 if (Array.isArray(cliOptions.addOns)) {
99 for (const addOn of cliOptions.addOns) {
100 addOns.add(addOn)
101 }
102 } else {
103 for (const addOn of await selectAddOns(
104 options.framework,
105 options.mode,
106 'add-on',
107 'What add-ons would you like for your project?',
108 forcedAddOns,
109 )) {
110 addOns.add(addOn)
111 }
112
113 for (const addOn of await selectAddOns(
114 options.framework,
115 options.mode,
116 'example',
117 'Would you like any examples?',
118 forcedAddOns,
119 )) {
120 addOns.add(addOn)
121 }
122 }
123
124 options.chosenAddOns = Array.from(
125 await finalizeAddOns(options.framework, options.mode, Array.from(addOns)),
126 )
127
128 if (options.chosenAddOns.length) {
129 options.tailwind = true
130 options.typescript = true
131 }
132
133 options.git = cliOptions.git || (await selectGit())
134
135 return options
136}
137
138export async function promptForAddOns(): Promise<Array<string>> {
139 const config = await readConfigFile(process.cwd())
140
141 if (!config) {
142 console.error('No config file found')
143 process.exit(1)
144 }
145
146 const framework = getFrameworkById(config.framework)
147
148 if (!framework) {
149 console.error(`Unknown framework: ${config.framework}`)
150 process.exit(1)
151 }
152
153 intro(`Adding new add-ons to '${config.projectName}'`)
154
155 const addOns: Set<string> = new Set()
156
157 for (const addOn of await selectAddOns(
158 framework,
159 config.mode!,
160 'add-on',
161 'What add-ons would you like for your project?',
162 config.chosenAddOns,
163 )) {
164 addOns.add(addOn)
165 }
166
167 for (const addOn of await selectAddOns(
168 framework,
169 config.mode!,
170 'example',
171 'Would you like any examples?',
172 config.chosenAddOns,
173 )) {
174 addOns.add(addOn)
175 }
176
177 return Array.from(addOns)
178}