Skip to content

Commit 02161b4

Browse files
committed
chore: first pass on API cleanup
1 parent 307c47f commit 02161b4

29 files changed

+217
-349
lines changed

packages/cta-cli/src/cli.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function cli({
6060
forcedMode?: Mode
6161
forcedAddOns?: Array<string>
6262
}) {
63-
const environment = createUIEnvironment()
63+
const environment = createUIEnvironment(appName, false)
6464

6565
const program = new Command()
6666

@@ -250,11 +250,8 @@ export function cli({
250250
})
251251
}
252252

253-
await createApp(finalOptions!, {
254-
environment: createUIEnvironment(),
253+
await createApp(environment, finalOptions!, {
255254
cwd: options.targetDir || undefined,
256-
name,
257-
appName,
258255
})
259256
} catch (error) {
260257
log.error(

packages/cta-cli/src/options.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ export async function normalizeOptions(
7171
mode = starter.mode
7272
}
7373

74-
let addOns = false
7574
let chosenAddOns: Array<AddOn> = []
7675
if (
7776
Array.isArray(cliOptions.addOns) ||
7877
starter?.dependsOn ||
7978
forcedAddOns ||
8079
cliOptions.toolchain
8180
) {
82-
addOns = true
8381
let finalAddOns = Array.from(
8482
new Set([...(starter?.dependsOn || []), ...(forcedAddOns || [])]),
8583
)
@@ -111,20 +109,19 @@ export async function normalizeOptions(
111109

112110
return {
113111
// TODO: This is a bit to fix the default framework
114-
framework: getFrameworkById(cliOptions.framework || 'react-cra')!,
115112
projectName: cliOptions.projectName,
113+
framework: getFrameworkById(cliOptions.framework || 'react-cra')!,
114+
mode,
116115
typescript,
117116
tailwind,
118117
packageManager:
119118
cliOptions.packageManager ||
120119
getPackageManager() ||
121120
DEFAULT_PACKAGE_MANAGER,
122-
mode,
123121
git: !!cliOptions.git,
124-
addOns,
125122
chosenAddOns,
126-
variableValues: {},
127123
starter,
124+
variableValues: {},
128125
}
129126
}
130127
}

packages/cta-cli/src/ui-environment.ts

+52-44
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,60 @@ import { createDefaultEnvironment } from '@tanstack/cta-engine'
1313

1414
import type { Environment } from '@tanstack/cta-engine'
1515

16-
export function createUIEnvironment(): Environment {
16+
export function createUIEnvironment(
17+
appName: string,
18+
silent: boolean,
19+
): Environment {
1720
const defaultEnvironment = createDefaultEnvironment()
1821

19-
return {
22+
let newEnvironment = {
2023
...defaultEnvironment,
21-
intro: (message: string) => {
22-
intro(message)
23-
},
24-
outro: (message: string) => {
25-
outro(message)
26-
},
27-
info: (title?: string, message?: string) => {
28-
console.log('info', title, message)
29-
log.info(
30-
`${title ? chalk.red(title) : ''}${message ? chalk.green(message) : ''}`,
31-
)
32-
},
33-
error: (title?: string, message?: string) => {
34-
console.log('error', title, message)
35-
log.error(`${title ? `${title}: ` : ''}${message}`)
36-
},
37-
warn: (title?: string, message?: string) => {
38-
console.log('warn', title, message)
39-
log.warn(`${title ? `${title}: ` : ''}${message}`)
40-
},
41-
confirm: async (message: string) => {
42-
console.log('confirm', message)
43-
const shouldContinue = await confirm({
44-
message,
45-
})
46-
if (isCancel(shouldContinue)) {
47-
cancel('Operation cancelled.')
48-
process.exit(0)
49-
}
50-
return shouldContinue
51-
},
52-
spinner: () => {
53-
const s = spinner()
54-
return {
55-
start: (message: string) => {
56-
s.start(message)
57-
},
58-
stop: (message: string) => {
59-
s.stop(message)
60-
},
61-
}
62-
},
24+
appName,
6325
}
26+
27+
if (!silent) {
28+
newEnvironment = {
29+
...newEnvironment,
30+
intro: (message: string) => {
31+
intro(message)
32+
},
33+
outro: (message: string) => {
34+
outro(message)
35+
},
36+
info: (title?: string, message?: string) => {
37+
log.info(
38+
`${title ? chalk.red(title) : ''}${message ? chalk.green(message) : ''}`,
39+
)
40+
},
41+
error: (title?: string, message?: string) => {
42+
log.error(`${title ? `${title}: ` : ''}${message}`)
43+
},
44+
warn: (title?: string, message?: string) => {
45+
log.warn(`${title ? `${title}: ` : ''}${message}`)
46+
},
47+
confirm: async (message: string) => {
48+
const shouldContinue = await confirm({
49+
message,
50+
})
51+
if (isCancel(shouldContinue)) {
52+
cancel('Operation cancelled.')
53+
process.exit(0)
54+
}
55+
return shouldContinue
56+
},
57+
spinner: () => {
58+
const s = spinner()
59+
return {
60+
start: (message: string) => {
61+
s.start(message)
62+
},
63+
stop: (message: string) => {
64+
s.stop(message)
65+
},
66+
}
67+
},
68+
}
69+
}
70+
71+
return newEnvironment
6472
}

packages/cta-custom-add-on/src/custom-add-on.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,8 @@ export async function createAppOptionsFromPersisted(
123123

124124
async function runCreateApp(options: Required<Options>) {
125125
const { environment, output } = createMemoryEnvironment()
126-
await createApp(options, {
127-
silent: true,
128-
environment,
126+
await createApp(environment, options, {
129127
cwd: process.cwd(),
130-
name: 'create-tsrouter-app',
131128
})
132129
return output
133130
}

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ async function createOptions(
4646

4747
async function runCreateApp(options: Required<Options>) {
4848
const { environment, output } = createMemoryEnvironment()
49-
await createApp(options, {
50-
silent: true,
51-
environment,
49+
await createApp(environment, options, {
5250
cwd: process.cwd(),
53-
name: 'create-tsrouter-app',
5451
})
5552
return output
5653
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function writeConfigFile(
2020
options: Options,
2121
) {
2222
/* eslint-disable unused-imports/no-unused-vars */
23-
const { addOns, chosenAddOns, framework, ...rest } = options
23+
const { chosenAddOns, framework, ...rest } = options
2424
/* eslint-enable unused-imports/no-unused-vars */
2525
const persistedOptions: PersistedOptions = {
2626
...rest,

packages/cta-engine/src/create-app.ts

+16-35
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,37 @@ async function runCommandsAndInstallDependencies(
6666
environment: Environment,
6767
targetDir: string,
6868
options: Options,
69-
silent: boolean,
7069
) {
71-
const s = silent ? null : environment.spinner()
70+
const s = environment.spinner()
7271

7372
// Setup git
7473
if (options.git) {
75-
s?.start(`Initializing git repository...`)
74+
s.start(`Initializing git repository...`)
7675
await setupGit(environment, targetDir)
77-
s?.stop(`Initialized git repository`)
76+
s.stop(`Initialized git repository`)
7877
}
7978

8079
// Install dependencies
81-
s?.start(`Installing dependencies via ${options.packageManager}...`)
80+
s.start(`Installing dependencies via ${options.packageManager}...`)
8281
await packageManagerInstall(
8382
environment,
8483
resolve(targetDir),
8584
options.packageManager,
8685
)
87-
s?.stop(`Installed dependencies`)
86+
s.stop(`Installed dependencies`)
8887

8988
for (const phase of ['setup', 'add-on', 'example']) {
9089
for (const addOn of options.chosenAddOns.filter(
9190
(addOn) =>
9291
addOn.phase === phase && addOn.command && addOn.command.command,
9392
)) {
94-
s?.start(`Setting up ${addOn.name}...`)
93+
s.start(`Setting up ${addOn.name}...`)
9594
await environment.execute(
9695
addOn.command!.command,
9796
addOn.command!.args || [],
9897
resolve(targetDir),
9998
)
100-
s?.stop(`${addOn.name} setup complete`)
99+
s.stop(`${addOn.name} setup complete`)
101100
}
102101
}
103102

@@ -107,24 +106,19 @@ async function runCommandsAndInstallDependencies(
107106
options.starter.command &&
108107
options.starter.command.command
109108
) {
110-
s?.start(`Setting up starter ${options.starter.name}...`)
109+
s.start(`Setting up starter ${options.starter.name}...`)
111110
await environment.execute(
112111
options.starter.command.command,
113112
options.starter.command.args || [],
114113
resolve(targetDir),
115114
)
116-
s?.stop(`Starter ${options.starter.name} setup complete`)
115+
s.stop(`Starter ${options.starter.name} setup complete`)
117116
}
118117

119-
await installShadcnComponents(environment, targetDir, options, silent)
118+
await installShadcnComponents(environment, targetDir, options)
120119
}
121120

122-
function report(
123-
environment: Environment,
124-
options: Options,
125-
appName: string,
126-
targetDir: string,
127-
) {
121+
function report(environment: Environment, options: Options, targetDir: string) {
128122
const warnings: Array<string> = []
129123
for (const addOn of options.chosenAddOns) {
130124
if (addOn.warning) {
@@ -146,7 +140,7 @@ Errors were encountered during this process:
146140
${environment.getErrors().join('\n')}`
147141
}
148142

149-
environment.outro(`Your ${appName} app is ready in '${basename(targetDir)}'.
143+
environment.outro(`Your ${environment.appName} app is ready in '${basename(targetDir)}'.
150144
151145
Use the following commands to start your app:
152146
% cd ${options.projectName}
@@ -158,36 +152,23 @@ Use the following commands to start your app:
158152
}
159153

160154
export async function createApp(
155+
environment: Environment,
161156
options: Options,
162157
{
163-
silent = false,
164-
environment,
165158
cwd,
166-
appName = 'TanStack',
167159
}: {
168-
silent?: boolean
169-
environment: Environment
170160
cwd?: string
171-
name?: string
172-
appName?: string
173-
},
161+
} = {},
174162
) {
175163
environment.startRun()
176164

177165
const targetDir: string = cwd || resolve(process.cwd(), options.projectName)
178166

179167
await writeFiles(environment, targetDir, options)
180168

181-
await runCommandsAndInstallDependencies(
182-
environment,
183-
targetDir,
184-
options,
185-
silent,
186-
)
169+
await runCommandsAndInstallDependencies(environment, targetDir, options)
187170

188171
environment.finishRun()
189172

190-
if (!silent) {
191-
report(environment, options, appName, targetDir)
192-
}
173+
report(environment, options, targetDir)
193174
}

packages/cta-engine/src/environment.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function createDefaultEnvironment(): Environment {
5151

5252
exists: (path: string) => existsSync(path),
5353

54+
appName: 'TanStack',
5455
intro: () => {},
5556
outro: () => {},
5657
info: () => {},

packages/cta-engine/src/integrations/shadcn.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ export async function installShadcnComponents(
66
environment: Environment,
77
targetDir: string,
88
options: Options,
9-
silent: boolean,
109
) {
11-
const s = silent ? null : environment.spinner()
10+
const s = environment.spinner()
1211

1312
if (options.chosenAddOns.find((a) => a.id === 'shadcn')) {
1413
const shadcnComponents = new Set<string>()
@@ -28,7 +27,7 @@ export async function installShadcnComponents(
2827
}
2928

3029
if (shadcnComponents.size > 0) {
31-
s?.start(
30+
s.start(
3231
`Installing shadcn components (${Array.from(shadcnComponents).join(', ')})...`,
3332
)
3433
await packageManagerExecute(
@@ -38,7 +37,7 @@ export async function installShadcnComponents(
3837
'shadcn@latest',
3938
['add', '--silent', '--yes', ...Array.from(shadcnComponents)],
4039
)
41-
s?.stop(`Installed additional shadcn components`)
40+
s.stop(`Installed additional shadcn components`)
4241
}
4342
}
4443
}

packages/cta-engine/src/types.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,21 @@ export type Framework = FrameworkDefinition &
8585
}
8686

8787
export interface Options {
88-
framework: Framework
8988
projectName: string
89+
90+
framework: Framework
91+
mode: Mode
92+
9093
typescript: boolean
9194
tailwind: boolean
95+
9296
packageManager: PackageManager
93-
mode: Mode
94-
addOns: boolean
95-
chosenAddOns: Array<AddOn>
9697
git: boolean
97-
variableValues: Record<string, string | number | boolean>
98+
99+
chosenAddOns: Array<AddOn>
98100
starter?: AddOn | undefined
101+
102+
variableValues: Record<string, string | number | boolean>
99103
}
100104

101105
type ProjectEnvironment = {
@@ -114,6 +118,8 @@ type FileEnvironment = {
114118
}
115119

116120
type UIEnvironment = {
121+
appName: string
122+
117123
intro: (message: string) => void
118124
outro: (message: string) => void
119125

0 commit comments

Comments
 (0)