|
| 1 | +import { resolve } from 'node:path' |
| 2 | + |
| 3 | +import { |
| 4 | + CODE_ROUTER, |
| 5 | + DEFAULT_PACKAGE_MANAGER, |
| 6 | + FILE_ROUTER, |
| 7 | + finalizeAddOns, |
| 8 | + getFrameworkById, |
| 9 | + getPackageManager, |
| 10 | + loadRemoteAddOn, |
| 11 | +} from '@tanstack/cta-engine' |
| 12 | + |
| 13 | +import type { Mode, Options, Starter } from '@tanstack/cta-engine' |
| 14 | + |
| 15 | +import type { CliOptions } from './types.js' |
| 16 | + |
| 17 | +export async function normalizeOptions( |
| 18 | + cliOptions: CliOptions, |
| 19 | + forcedMode?: Mode, |
| 20 | + forcedAddOns?: Array<string>, |
| 21 | +): Promise<Options | undefined> { |
| 22 | + const projectName = (cliOptions.projectName ?? '').trim() |
| 23 | + if (!projectName) { |
| 24 | + return undefined |
| 25 | + } |
| 26 | + |
| 27 | + let typescript = |
| 28 | + cliOptions.template === 'typescript' || |
| 29 | + cliOptions.template === 'file-router' || |
| 30 | + cliOptions.framework === 'solid' |
| 31 | + |
| 32 | + let tailwind = !!cliOptions.tailwind |
| 33 | + if (cliOptions.framework === 'solid') { |
| 34 | + tailwind = true |
| 35 | + } |
| 36 | + |
| 37 | + let mode: typeof FILE_ROUTER | typeof CODE_ROUTER = |
| 38 | + cliOptions.template === 'file-router' ? FILE_ROUTER : CODE_ROUTER |
| 39 | + |
| 40 | + const starter = cliOptions.starter |
| 41 | + ? ((await loadRemoteAddOn(cliOptions.starter)) as Starter) |
| 42 | + : undefined |
| 43 | + |
| 44 | + if (starter) { |
| 45 | + tailwind = starter.tailwind |
| 46 | + typescript = starter.typescript |
| 47 | + cliOptions.framework = starter.framework |
| 48 | + mode = starter.mode |
| 49 | + } |
| 50 | + |
| 51 | + const framework = getFrameworkById(cliOptions.framework || 'react-cra')! |
| 52 | + |
| 53 | + async function selectAddOns() { |
| 54 | + // Edge case for Windows Powershell |
| 55 | + if (Array.isArray(cliOptions.addOns) && cliOptions.addOns.length === 1) { |
| 56 | + const parseSeparatedArgs = cliOptions.addOns[0].split(' ') |
| 57 | + if (parseSeparatedArgs.length > 1) { |
| 58 | + cliOptions.addOns = parseSeparatedArgs |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if ( |
| 63 | + Array.isArray(cliOptions.addOns) || |
| 64 | + starter?.dependsOn || |
| 65 | + forcedAddOns || |
| 66 | + cliOptions.toolchain |
| 67 | + ) { |
| 68 | + const selectedAddOns = new Set<string>([ |
| 69 | + ...(starter?.dependsOn || []), |
| 70 | + ...(forcedAddOns || []), |
| 71 | + ]) |
| 72 | + if (cliOptions.addOns && Array.isArray(cliOptions.addOns)) { |
| 73 | + for (const a of cliOptions.addOns) { |
| 74 | + selectedAddOns.add(a) |
| 75 | + } |
| 76 | + } |
| 77 | + if (cliOptions.toolchain) { |
| 78 | + selectedAddOns.add(cliOptions.toolchain) |
| 79 | + } |
| 80 | + |
| 81 | + return await finalizeAddOns( |
| 82 | + framework, |
| 83 | + forcedMode || cliOptions.template === 'file-router' |
| 84 | + ? FILE_ROUTER |
| 85 | + : CODE_ROUTER, |
| 86 | + Array.from(selectedAddOns), |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + return [] |
| 91 | + } |
| 92 | + |
| 93 | + const chosenAddOns = await selectAddOns() |
| 94 | + |
| 95 | + if (chosenAddOns.length) { |
| 96 | + tailwind = true |
| 97 | + typescript = true |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + projectName: projectName, |
| 102 | + targetDir: resolve(process.cwd(), projectName), |
| 103 | + framework, |
| 104 | + mode, |
| 105 | + typescript, |
| 106 | + tailwind, |
| 107 | + packageManager: |
| 108 | + cliOptions.packageManager || |
| 109 | + getPackageManager() || |
| 110 | + DEFAULT_PACKAGE_MANAGER, |
| 111 | + git: !!cliOptions.git, |
| 112 | + chosenAddOns, |
| 113 | + starter, |
| 114 | + variableValues: {}, |
| 115 | + } |
| 116 | +} |
0 commit comments