Skip to content

Commit 04567eb

Browse files
committed
chore: more small cleanups
1 parent 9d6b072 commit 04567eb

File tree

4 files changed

+105
-64
lines changed

4 files changed

+105
-64
lines changed

packages/cta-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export {
1010
getPackageManager,
1111
getPackageManagerInstallCommand,
1212
getPackageManagerExecuteCommand,
13+
getPackageManagerScriptCommand,
1314
packageManagerInstall,
1415
packageManagerExecute,
1516
} from './package-manager.js'

packages/cta-core/src/package-manager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ export function getPackageManager(): PackageManager | undefined {
2424
return packageManager
2525
}
2626

27+
export function getPackageManagerScriptCommand(
28+
packagerManager: PackageManager,
29+
args: Array<string> = [],
30+
) {
31+
switch (packagerManager) {
32+
case 'yarn':
33+
return { command: 'yarn', args: ['run', ...args] }
34+
case 'pnpm':
35+
return { command: 'pnpm', args: ['run', ...args] }
36+
case 'bun':
37+
return { command: 'bunx', args: ['--bun', 'run', ...args] }
38+
case 'deno':
39+
return { command: 'deno', args: ['task', ...args] }
40+
default:
41+
return { command: 'npm', args: ['run', ...args] }
42+
}
43+
}
44+
2745
export function getPackageManagerExecuteCommand(
2846
packagerManager: PackageManager,
2947
pkg: string,

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

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { basename, resolve } from 'node:path'
22

33
import {
4+
formatCommand,
45
getBinaryFile,
5-
packageManagerExecute,
6+
getPackageManagerScriptCommand,
67
packageManagerInstall,
78
writeConfigFile,
89
} from '@tanstack/cta-core'
910

1011
import { createPackageJSON } from './package-json.js'
1112
import { createTemplateFile } from './template-file.js'
13+
import { installShadcnComponents } from './integrations/shadcn.js'
1214

1315
import type {
1416
Environment,
@@ -91,11 +93,8 @@ export async function createApp(
9193

9294
const s = silent ? null : environment.spinner()
9395

94-
// Setup the add-ons
95-
const isAddOnEnabled = (id: string) =>
96-
options.chosenAddOns.find((a) => a.id === id)
96+
// Install all the dependencies
9797

98-
// Copy all the asset files from the addons
9998
for (const phase of ['setup', 'add-on', 'example']) {
10099
for (const addOn of options.chosenAddOns.filter(
101100
(addOn) =>
@@ -142,81 +141,54 @@ export async function createApp(
142141
)
143142
s?.stop(`Installed dependencies`)
144143

145-
// Run all the commands
146-
if (isAddOnEnabled('shadcn')) {
147-
const shadcnComponents = new Set<string>()
148-
for (const addOn of options.chosenAddOns) {
149-
if (addOn.shadcnComponents) {
150-
for (const component of addOn.shadcnComponents) {
151-
shadcnComponents.add(component)
152-
}
153-
}
154-
}
155-
if (options.starter) {
156-
if (options.starter.shadcnComponents) {
157-
for (const component of options.starter.shadcnComponents) {
158-
shadcnComponents.add(component)
159-
}
160-
}
161-
}
162-
163-
if (shadcnComponents.size > 0) {
164-
s?.start(
165-
`Installing shadcn components (${Array.from(shadcnComponents).join(', ')})...`,
166-
)
167-
await packageManagerExecute(
168-
environment,
169-
resolve(targetDir),
170-
options.packageManager,
171-
'shadcn@latest',
172-
[
173-
'add',
174-
'--force',
175-
'--silent',
176-
'--yes',
177-
...Array.from(shadcnComponents),
178-
],
179-
)
180-
s?.stop(`Installed additional shadcn components`)
181-
}
182-
}
144+
await installShadcnComponents(environment, targetDir, options, silent)
183145

184146
environment.finishRun()
185147

186148
if (!silent) {
187-
// Check for warnings
188-
const warnings: Array<string> = []
189-
for (const addOn of options.chosenAddOns) {
190-
if (addOn.warning) {
191-
warnings.push(addOn.warning)
192-
}
193-
}
149+
report(environment, options, appName, targetDir)
150+
}
151+
}
194152

195-
if (warnings.length > 0) {
196-
environment.warn('Warnings', warnings.join('\n'))
153+
function report(
154+
environment: Environment,
155+
options: Options,
156+
appName: string,
157+
targetDir: string,
158+
) {
159+
const warnings: Array<string> = []
160+
for (const addOn of options.chosenAddOns) {
161+
if (addOn.warning) {
162+
warnings.push(addOn.warning)
197163
}
164+
}
165+
166+
if (warnings.length > 0) {
167+
environment.warn('Warnings', warnings.join('\n'))
168+
}
198169

199-
// Format errors
200-
let errorStatement = ''
201-
if (environment.getErrors().length) {
202-
errorStatement = `
170+
// Format errors
171+
let errorStatement = ''
172+
if (environment.getErrors().length) {
173+
errorStatement = `
203174
204175
Errors were encountered during this process:
205176
206177
${environment.getErrors().join('\n')}`
207-
}
178+
}
208179

209-
let startCommand = `${options.packageManager} ${isAddOnEnabled('start') ? 'dev' : 'start'}`
210-
if (options.packageManager === 'deno') {
211-
startCommand = `deno ${isAddOnEnabled('start') ? 'task dev' : 'start'}`
212-
}
180+
const start = !!options.chosenAddOns.find((a) => a.id === 'start')
181+
const { command, args } = getPackageManagerScriptCommand(
182+
options.packageManager,
183+
start ? ['dev'] : ['start'],
184+
)
185+
const startCommand = formatCommand(command, args)
213186

214-
environment.outro(`Your ${appName} app is ready in '${basename(targetDir)}'.
187+
environment.outro(`Your ${appName} app is ready in '${basename(targetDir)}'.
215188
216189
Use the following commands to start your app:
217190
% cd ${options.projectName}
218191
% ${startCommand}
219192
220-
Please check the README.md for more information on testing, styling, adding routes, etc.${errorStatement}`)
221-
}
193+
Please check the README.md for more information on testing, styling, adding routes, etc.${errorStatement}`)
222194
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { resolve } from 'node:path'
2+
import { packageManagerExecute } from '@tanstack/cta-core'
3+
import type { Environment, Options } from '@tanstack/cta-core'
4+
5+
export async function installShadcnComponents(
6+
environment: Environment,
7+
targetDir: string,
8+
options: Options,
9+
silent: boolean,
10+
) {
11+
const s = silent ? null : environment.spinner()
12+
13+
if (options.chosenAddOns.find((a) => a.id === 'shadcn')) {
14+
const shadcnComponents = new Set<string>()
15+
for (const addOn of options.chosenAddOns) {
16+
if (addOn.shadcnComponents) {
17+
for (const component of addOn.shadcnComponents) {
18+
shadcnComponents.add(component)
19+
}
20+
}
21+
}
22+
if (options.starter) {
23+
if (options.starter.shadcnComponents) {
24+
for (const component of options.starter.shadcnComponents) {
25+
shadcnComponents.add(component)
26+
}
27+
}
28+
}
29+
30+
if (shadcnComponents.size > 0) {
31+
s?.start(
32+
`Installing shadcn components (${Array.from(shadcnComponents).join(', ')})...`,
33+
)
34+
await packageManagerExecute(
35+
environment,
36+
resolve(targetDir),
37+
options.packageManager,
38+
'shadcn@latest',
39+
[
40+
'add',
41+
'--force',
42+
'--silent',
43+
'--yes',
44+
...Array.from(shadcnComponents),
45+
],
46+
)
47+
s?.stop(`Installed additional shadcn components`)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)