-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathcommand-line.ts
More file actions
256 lines (218 loc) · 6.43 KB
/
command-line.ts
File metadata and controls
256 lines (218 loc) · 6.43 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
import { resolve } from 'node:path'
import fs from 'node:fs'
import {
DEFAULT_PACKAGE_MANAGER,
finalizeAddOns,
getFrameworkById,
getPackageManager,
loadStarter,
populateAddOnOptionsDefaults,
} from '@tanstack/create'
import {
getCurrentDirectoryName,
sanitizePackageName,
validateProjectName,
} from './utils.js'
import type { Options } from '@tanstack/create'
import type { CliOptions } from './types.js'
const SUPPORTED_LEGACY_TEMPLATES = new Set([
'file-router',
'typescript',
'tsx',
])
export function validateLegacyCreateFlags(cliOptions: CliOptions): {
warnings: Array<string>
error?: string
} {
const warnings: Array<string> = []
if (cliOptions.routerOnly) {
warnings.push(
'The --router-only flag is deprecated and ignored. `tanstack create` already creates router-based apps.',
)
}
if (cliOptions.tailwind === true) {
warnings.push(
'The --tailwind flag is deprecated and ignored. Tailwind is always enabled in TanStack Start scaffolds.',
)
}
if (cliOptions.tailwind === false) {
warnings.push(
'The --no-tailwind flag is deprecated and ignored. Tailwind opt-out is intentionally unsupported to keep add-on permutations maintainable; remove Tailwind after scaffolding if needed.',
)
}
if (!cliOptions.template) {
return { warnings }
}
const template = cliOptions.template.toLowerCase().trim()
if (template === 'javascript' || template === 'js' || template === 'jsx') {
return {
warnings,
error:
'JavaScript/JSX templates are not supported. TanStack Start file-router templates are TypeScript-only.',
}
}
if (!SUPPORTED_LEGACY_TEMPLATES.has(template)) {
return {
warnings,
error: `Invalid --template value: ${cliOptions.template}. Supported values are: file-router, typescript, tsx.`,
}
}
warnings.push(
'The --template flag is deprecated. TypeScript/TSX is the default and only supported template.',
)
return { warnings }
}
export async function normalizeOptions(
cliOptions: CliOptions,
forcedAddOns?: Array<string>,
opts?: {
disableNameCheck?: boolean
forcedDeployment?: string
},
): Promise<Options | undefined> {
let projectName = (cliOptions.projectName ?? '').trim()
let targetDir: string
// Handle "." as project name - use current directory
if (projectName === '.') {
projectName = sanitizePackageName(getCurrentDirectoryName())
targetDir = resolve(process.cwd())
} else {
targetDir = resolve(process.cwd(), projectName)
}
if (!projectName && !opts?.disableNameCheck) {
return undefined
}
if (projectName) {
const { valid, error } = validateProjectName(projectName)
if (!valid) {
console.error(error)
process.exit(1)
}
}
// Mode is always file-router (TanStack Start)
let mode = 'file-router'
const starter = cliOptions.starter
? await loadStarter(cliOptions.starter)
: undefined
// TypeScript and Tailwind are always enabled with TanStack Start
const typescript = true
const tailwind = true
if (starter) {
cliOptions.framework = starter.framework
mode = starter.mode
}
const framework = getFrameworkById(cliOptions.framework || 'react-cra')!
async function selectAddOns() {
// Edge case for Windows Powershell
if (Array.isArray(cliOptions.addOns) && cliOptions.addOns.length === 1) {
const parseSeparatedArgs = cliOptions.addOns[0].split(' ')
if (parseSeparatedArgs.length > 1) {
cliOptions.addOns = parseSeparatedArgs
}
}
if (
Array.isArray(cliOptions.addOns) ||
starter?.dependsOn ||
forcedAddOns ||
cliOptions.toolchain ||
cliOptions.deployment
) {
const selectedAddOns = new Set<string>([
...(starter?.dependsOn || []),
...(forcedAddOns || []),
])
if (cliOptions.addOns && Array.isArray(cliOptions.addOns)) {
for (const a of cliOptions.addOns) {
if (a.toLowerCase() === 'start') {
continue
}
selectedAddOns.add(a)
}
}
if (cliOptions.toolchain) {
selectedAddOns.add(cliOptions.toolchain)
}
if (cliOptions.deployment) {
selectedAddOns.add(cliOptions.deployment)
}
if (!cliOptions.deployment && opts?.forcedDeployment) {
selectedAddOns.add(opts.forcedDeployment)
}
return await finalizeAddOns(framework, mode, Array.from(selectedAddOns))
}
return []
}
const chosenAddOns = await selectAddOns()
// Handle add-on configuration option
let addOnOptionsFromCLI = {}
if (cliOptions.addOnConfig) {
try {
addOnOptionsFromCLI = JSON.parse(cliOptions.addOnConfig)
} catch (error) {
console.error('Error parsing add-on config:', error)
process.exit(1)
}
}
return {
projectName: projectName,
targetDir,
framework,
mode,
typescript,
tailwind,
packageManager:
cliOptions.packageManager ||
getPackageManager() ||
DEFAULT_PACKAGE_MANAGER,
git: !!cliOptions.git,
install: cliOptions.install,
chosenAddOns,
addOnOptions: {
...populateAddOnOptionsDefaults(chosenAddOns),
...addOnOptionsFromCLI,
},
starter: starter,
}
}
export function validateDevWatchOptions(cliOptions: CliOptions): {
valid: boolean
error?: string
} {
if (!cliOptions.devWatch) {
return { valid: true }
}
// Validate watch path exists
const watchPath = resolve(process.cwd(), cliOptions.devWatch)
if (!fs.existsSync(watchPath)) {
return {
valid: false,
error: `Watch path does not exist: ${watchPath}`,
}
}
// Validate it's a directory
const stats = fs.statSync(watchPath)
if (!stats.isDirectory()) {
return {
valid: false,
error: `Watch path is not a directory: ${watchPath}`,
}
}
// Ensure target directory is specified
if (!cliOptions.projectName && !cliOptions.targetDir) {
return {
valid: false,
error: 'Project name or target directory is required for dev watch mode',
}
}
// Check for framework structure
const hasAddOns = fs.existsSync(resolve(watchPath, 'add-ons'))
const hasAssets = fs.existsSync(resolve(watchPath, 'assets'))
const hasFrameworkJson = fs.existsSync(resolve(watchPath, 'framework.json'))
if (!hasAddOns && !hasAssets && !hasFrameworkJson) {
return {
valid: false,
error: `Watch path does not appear to be a valid framework directory: ${watchPath}`,
}
}
return { valid: true }
}