Skip to content

Commit 469fabd

Browse files
committed
chore: more config file fixes
1 parent 33a7e64 commit 469fabd

File tree

5 files changed

+111
-6
lines changed

5 files changed

+111
-6
lines changed

packages/cta-cli/src/cli.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { launchUI } from '@tanstack/cta-ui'
2222

2323
import { runMCPServer } from './mcp.js'
2424

25-
import { promptForOptions } from './options.js'
25+
import { promptForCreateOptions, promptForAddOns } from './options.js'
2626
import { normalizeOptions } from './command-line.js'
2727

2828
import { createUIEnvironment } from './ui-environment.js'
@@ -113,6 +113,13 @@ export function cli({
113113
forcedRouterMode: forcedMode,
114114
forcedAddOns,
115115
})
116+
} else if (parsedAddOns.length < 1) {
117+
const addOns = await promptForAddOns()
118+
if (addOns.length) {
119+
await addToApp(environment, addOns, process.cwd(), {
120+
forced: program.opts().forced,
121+
})
122+
}
116123
} else {
117124
await addToApp(environment, parsedAddOns, process.cwd(), {
118125
forced: program.opts().forced,
@@ -303,7 +310,7 @@ export function cli({
303310
intro(`Creating a new ${appName} app in ${projectName}...`)
304311
} else {
305312
intro(`Let's configure your ${appName} application`)
306-
finalOptions = await promptForOptions(cliOptions, {
313+
finalOptions = await promptForCreateOptions(cliOptions, {
307314
forcedMode,
308315
forcedAddOns,
309316
})

packages/cta-cli/src/options.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
finalizeAddOns,
55
getFrameworkById,
66
getPackageManager,
7+
readConfigFile,
78
} from '@tanstack/cta-engine'
89

910
import {
@@ -20,8 +21,9 @@ import {
2021
import type { Mode, Options } from '@tanstack/cta-engine'
2122

2223
import type { CliOptions } from './types.js'
24+
import { intro } from '@clack/prompts'
2325

24-
export async function promptForOptions(
26+
export async function promptForCreateOptions(
2527
cliOptions: CliOptions,
2628
{
2729
forcedAddOns = [],
@@ -126,3 +128,45 @@ export async function promptForOptions(
126128

127129
return options
128130
}
131+
132+
export async function promptForAddOns(): Promise<Array<string>> {
133+
const config = await readConfigFile(process.cwd())
134+
135+
if (!config) {
136+
console.error('No config file found')
137+
process.exit(1)
138+
}
139+
140+
const framework = getFrameworkById(config.framework)
141+
142+
if (!framework) {
143+
console.error(`Unknown framework: ${config.framework}`)
144+
process.exit(1)
145+
}
146+
147+
intro(`Adding new add-ons to '${config.projectName}'`)
148+
149+
const addOns: Set<string> = new Set()
150+
151+
for (const addOn of await selectAddOns(
152+
framework,
153+
config.mode!,
154+
'add-on',
155+
'What add-ons would you like for your project?',
156+
config.chosenAddOns,
157+
)) {
158+
addOns.add(addOn)
159+
}
160+
161+
for (const addOn of await selectAddOns(
162+
framework,
163+
config.mode!,
164+
'example',
165+
'Would you like any examples?',
166+
config.chosenAddOns,
167+
)) {
168+
addOns.add(addOn)
169+
}
170+
171+
return Array.from(addOns)
172+
}

packages/cta-engine/src/add-to-app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from './file-helpers.js'
1818
import { mergePackageJSON } from './package-json.js'
1919
import { runSpecialSteps } from './special-steps/index.js'
20+
import { loadStarter } from './custom-add-ons/starter.js'
2021

2122
import type { Environment, Mode, Options } from './types.js'
2223
import type { PersistedOptions } from './config-file.js'
@@ -40,7 +41,7 @@ async function createOptions(
4041
): Promise<Options> {
4142
const framework = getFrameworkById(json.framework)
4243

43-
// TODO: Load the starter
44+
const starter = json.starter ? await loadStarter(json.starter) : undefined
4445

4546
return {
4647
...json,
@@ -52,6 +53,7 @@ async function createOptions(
5253
...addOns,
5354
]),
5455
targetDir,
56+
starter,
5557
} as Options
5658
}
5759

packages/cta-engine/src/config-file.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,24 @@ export async function readConfigFileFromEnvironment(
4646
const configFile = resolve(targetDir, CONFIG_FILE)
4747
const config = await environment.readFile(configFile)
4848

49-
// TODO: Look for old config files and convert them to the new format
49+
const originalJSON = JSON.parse(config)
5050

51-
return JSON.parse(config)
51+
// Look for markers out outdated config files and upgrade the format on the fly (it will be written in the updated version after we add add-ons)
52+
if (originalJSON.existingAddOns) {
53+
if (originalJSON.framework === 'react') {
54+
originalJSON.framework = 'react-cra'
55+
}
56+
originalJSON.chosenAddOns = originalJSON.existingAddOns
57+
delete originalJSON.existingAddOns
58+
delete originalJSON.addOns
59+
if (originalJSON.toolchain && originalJSON.toolchain !== 'none') {
60+
originalJSON.chosenAddOns.push(originalJSON.toolchain)
61+
}
62+
delete originalJSON.toolchain
63+
delete originalJSON.variableValues
64+
}
65+
66+
return originalJSON
5267
} catch {
5368
return null
5469
}

packages/cta-engine/tests/config-file.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,41 @@ describe('readConfigFileFromEnvironment', () => {
6565
const config = await readConfigFileFromEnvironment(environment, targetDir)
6666
expect(config).toEqual(persistedOptions)
6767
})
68+
69+
it('should upgrade old config files', async () => {
70+
const targetDir = 'test-dir'
71+
const persistedOptions = {
72+
framework: 'react',
73+
projectName: 'foo',
74+
typescript: false,
75+
tailwind: false,
76+
packageManager: 'pnpm',
77+
toolchain: 'none',
78+
mode: 'code-router',
79+
git: true,
80+
variableValues: {},
81+
version: 1,
82+
existingAddOns: [],
83+
}
84+
const { output, environment } = createMemoryEnvironment()
85+
environment.writeFile(
86+
resolve(targetDir, CONFIG_FILE),
87+
JSON.stringify(persistedOptions, null, 2),
88+
)
89+
const config = await readConfigFileFromEnvironment(environment, targetDir)
90+
91+
environment.finishRun()
92+
93+
expect(config).toEqual({
94+
framework: 'react-cra',
95+
projectName: 'foo',
96+
typescript: false,
97+
tailwind: false,
98+
packageManager: 'pnpm',
99+
chosenAddOns: [],
100+
git: true,
101+
mode: 'code-router',
102+
version: 1,
103+
})
104+
})
68105
})

0 commit comments

Comments
 (0)