Skip to content

Commit 94595ef

Browse files
committed
chore: starting move to API endpoints
1 parent 225ff38 commit 94595ef

File tree

6 files changed

+62
-50
lines changed

6 files changed

+62
-50
lines changed

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,24 @@ export async function addToApp(
7070
)
7171
return
7272
}
73-
7473
if (!silent) {
7574
environment.intro(`Adding ${addOns.join(', ')} to the project...`)
7675
}
77-
76+
console.log('>>hasPendingGitChanges')
7877
if (await hasPendingGitChanges()) {
7978
environment.error(
8079
'You have pending git changes.',
8180
'Please commit or stash them before adding add-ons.',
8281
)
8382
return
8483
}
85-
84+
console.log('>>hasPendingGitChanges')
8685
const newOptions = await createOptions(persistedOptions, addOns)
87-
8886
const output = await runCreateApp(newOptions)
89-
9087
const overwrittenFiles: Array<string> = []
9188
const changedFiles: Array<string> = []
9289
const contentMap = new Map<string, string>()
90+
console.log('>>hasPendingGitChanges')
9391
for (const file of Object.keys(output.files)) {
9492
const relativeFile = file.replace(process.cwd(), '')
9593
if (existsSync(file)) {
@@ -108,7 +106,6 @@ export async function addToApp(
108106
contentMap.set(relativeFile, output.files[file])
109107
}
110108
}
111-
112109
if (overwrittenFiles.length > 0 && !silent) {
113110
environment.warn(
114111
'The following will be overwritten:',
@@ -119,7 +116,6 @@ export async function addToApp(
119116
process.exit(0)
120117
}
121118
}
122-
123119
for (const file of [...changedFiles, ...overwrittenFiles]) {
124120
const targetFile = `.${file}`
125121
const fName = basename(file)
@@ -129,7 +125,6 @@ export async function addToApp(
129125
(await readFile(resolve(fName), 'utf-8')).toString(),
130126
)
131127
const newJson = JSON.parse(contents)
132-
133128
currentJson.scripts = newJson.scripts
134129
currentJson.dependencies = sortObject({
135130
...currentJson.dependencies,
@@ -139,14 +134,12 @@ export async function addToApp(
139134
...currentJson.devDependencies,
140135
...newJson.devDependencies,
141136
})
142-
143137
await writeFile(targetFile, JSON.stringify(currentJson, null, 2))
144138
} else if (fName !== CONFIG_FILE) {
145139
await mkdir(resolve(dirname(targetFile)), { recursive: true })
146140
await writeFile(resolve(targetFile), contents)
147141
}
148142
}
149-
150143
// Handle commands
151144
const originalOutput = await runCreateApp(
152145
await createOptions(persistedOptions, []),
@@ -162,7 +155,6 @@ export async function addToApp(
162155
}
163156
const realEnvironment = createDefaultEnvironment()
164157
writeConfigFile(realEnvironment, process.cwd(), newOptions)
165-
166158
const s = silent ? null : environment.spinner()
167159
s?.start(`Installing dependencies via ${newOptions.packageManager}...`)
168160
await realEnvironment.execute(
@@ -171,7 +163,6 @@ export async function addToApp(
171163
resolve(process.cwd()),
172164
)
173165
s?.stop(`Installed dependencies`)
174-
175166
if (!silent) {
176167
environment.outro('Add-ons added successfully!')
177168
}

packages/cta-ui/src/components/cta-sidebar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@/components/ui/sidebar'
88

99
import { SelectedAddOns } from '@/components/sidebar-items/add-ons'
10+
import RunAddOns from '@/components/sidebar-items/run-add-ons'
1011

1112
export function AppSidebar() {
1213
return (
@@ -18,7 +19,9 @@ export function AppSidebar() {
1819
</SidebarGroup>
1920
<SidebarGroup />
2021
</SidebarContent>
21-
<SidebarFooter />
22+
<SidebarFooter>
23+
<RunAddOns />
24+
</SidebarFooter>
2225
</Sidebar>
2326
)
2427
}

packages/cta-ui/src/components/sidebar-items/run-add-ons.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button } from '@/components/ui/button'
2-
// import { closeApp, executeAddToApp } from '@/lib/server-fns'
2+
import { closeApp } from '@/lib/add-to-app-server-fn'
33
import { selectedAddOns } from '@/store/project'
44

55
export default function RunAddOns() {
@@ -8,11 +8,15 @@ export default function RunAddOns() {
88
<Button
99
variant="outline"
1010
onClick={async () => {
11-
// await executeAddToApp({
12-
// data: {
13-
// addOns: selectedAddOns.state.map((addOn) => addOn.id),
14-
// },
15-
// })
11+
await fetch('/api/add-add-ons', {
12+
method: 'POST',
13+
body: JSON.stringify({
14+
addOns: selectedAddOns.state.map((addOn) => addOn.id),
15+
}),
16+
headers: {
17+
'Content-Type': 'application/json',
18+
},
19+
})
1620
// await closeApp()
1721
// window.close()
1822
}}

packages/cta-ui/src/lib/server-fns.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { register as registerReactCra } from '@tanstack/cta-framework-react-cra'
66
import { register as registerSolid } from '@tanstack/cta-framework-solid'
77

88
import {
9-
// addToApp,
109
createApp,
1110
createAppOptionsFromPersisted,
12-
createDefaultEnvironment,
1311
createMemoryEnvironment,
1412
getAllAddOns,
1513
getFrameworkById,
@@ -19,13 +17,16 @@ import {
1917
import type { Mode, PersistedOptions } from '@tanstack/cta-engine'
2018

2119
let registered = false
22-
function register() {
20+
21+
export const register = createServerFn({
22+
method: 'POST',
23+
}).handler(() => {
2324
if (!registered) {
2425
registerReactCra()
2526
registerSolid()
2627
registered = true
2728
}
28-
}
29+
})
2930

3031
function cleanUpFiles(files: Record<string, string>, targetDir?: string) {
3132
return Object.keys(files).reduce<Record<string, string>>((acc, file) => {
@@ -43,7 +44,9 @@ function cleanUpFiles(files: Record<string, string>, targetDir?: string) {
4344
export const getLocalFiles = createServerFn({
4445
method: 'GET',
4546
}).handler(async () => {
46-
register()
47+
if (!registered) {
48+
await register()
49+
}
4750
return cleanUpFiles(
4851
await recursivelyGatherFiles(process.env.CTA_PROJECT_PATH!),
4952
)
@@ -56,7 +59,9 @@ export const getAddons = createServerFn({
5659
return data as { platform: string; mode: Mode }
5760
})
5861
.handler(async ({ data: { platform, mode } }) => {
59-
register()
62+
if (!registered) {
63+
await register()
64+
}
6065
const framework = await getFrameworkById(platform)
6166
return getAllAddOns(framework!, mode).map((addOn) => ({
6267
id: addOn.id,
@@ -69,7 +74,9 @@ export const getAddons = createServerFn({
6974
export const getAddonInfo = createServerFn({
7075
method: 'GET',
7176
}).handler(async () => {
72-
register()
77+
if (!registered) {
78+
await register()
79+
}
7380
const addOnInfo = readFileSync(
7481
resolve(process.env.CTA_PROJECT_PATH!, 'add-on.json'),
7582
)
@@ -79,7 +86,9 @@ export const getAddonInfo = createServerFn({
7986
export const getOriginalOptions = createServerFn({
8087
method: 'GET',
8188
}).handler(async () => {
82-
register()
89+
if (!registered) {
90+
await register()
91+
}
8392
const addOnInfo = readFileSync(
8493
resolve(process.env.CTA_PROJECT_PATH!, '.cta.json'),
8594
)
@@ -98,7 +107,9 @@ export const runCreateApp = createServerFn({
98107
}: {
99108
data: { options: PersistedOptions }
100109
}) => {
101-
register()
110+
if (!registered) {
111+
await register()
112+
}
102113

103114
try {
104115
const targetDir = process.env.CTA_PROJECT_PATH!
@@ -133,23 +144,3 @@ export const runCreateApp = createServerFn({
133144
}
134145
},
135146
)
136-
137-
export const executeAddToApp = createServerFn({
138-
method: 'POST',
139-
})
140-
.validator((data: unknown) => {
141-
return data as { addOns: Array<string> }
142-
})
143-
.handler(async ({ data: { addOns } }) => {
144-
register()
145-
const environment = createDefaultEnvironment()
146-
// await addToApp(addOns, { silent: true }, environment)
147-
return true
148-
})
149-
150-
export const closeApp = createServerFn({
151-
method: 'POST',
152-
}).handler(async () => {
153-
process.exit(0)
154-
return true
155-
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { json } from '@tanstack/react-start'
2+
import { createAPIFileRoute } from '@tanstack/react-start/api'
3+
4+
import { addToApp, createDefaultEnvironment } from '@tanstack/cta-engine'
5+
6+
import { register as registerReactCra } from '@tanstack/cta-framework-react-cra'
7+
import { register as registerSolid } from '@tanstack/cta-framework-solid'
8+
9+
registerReactCra()
10+
registerSolid()
11+
12+
export const APIRoute = createAPIFileRoute('/api/add-add-ons')({
13+
POST: async ({ request }) => {
14+
const { addOns } = await request.json()
15+
console.log(process.env.CTA_PROJECT_PATH)
16+
process.chdir(process.env.CTA_PROJECT_PATH!)
17+
console.log(process.cwd())
18+
const environment = createDefaultEnvironment()
19+
environment.error = console.error
20+
environment.warn = console.warn
21+
environment.info = console.log
22+
await addToApp(addOns, { silent: true }, environment)
23+
return json({ message: 'Hello "/api/add-add-ons"!' })
24+
},
25+
})

packages/cta-ui/src/routes/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import {
1717
projectOptions,
1818
} from '@/store/project'
1919

20-
// import type { AddOn } from '@tanstack/cta-engine'
21-
2220
export const Route = createFileRoute('/')({
2321
component: App,
2422
})

0 commit comments

Comments
 (0)