Skip to content

Commit 98ce9fe

Browse files
committed
fix: moving to a Vite SPA approach for the UI
1 parent f55b432 commit 98ce9fe

36 files changed

+373
-683
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ my-app
77
**/dist
88
.vinxi
99
**/lib-dist
10+
**/api-server-dist
1011
.vscode
1112

1213
.nx/cache

packages/cta-cli/src/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ export function cli({
109109
launchUI({
110110
mode: 'add',
111111
addOns: parsedAddOns,
112-
forcedMode,
112+
projectPath: process.cwd(),
113+
forcedRouterMode: forcedMode,
113114
forcedAddOns,
114115
})
115116
} else {
@@ -292,7 +293,7 @@ export function cli({
292293
launchUI({
293294
mode: 'setup',
294295
options: createSerializedOptions(finalOptions || defaultOptions),
295-
forcedMode,
296+
forcedRouterMode: forcedMode,
296297
forcedAddOns,
297298
})
298299
return

packages/cta-ui/app.config.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/cta-ui/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>TanStack CTA</title>
7+
</head>
8+
<body class="dark bg-black text-white dark">
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>

packages/cta-ui/src/engine-handling/add-to-app-wrapper.ts renamed to packages/cta-ui/lib/engine-handling/add-to-app-wrapper.ts

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ import {
88
recursivelyGatherFiles,
99
} from '@tanstack/cta-engine'
1010

11+
import { cleanUpFileArray, cleanUpFiles } from './file-helpers.js'
12+
import { getProjectPath } from './server-environment.js'
1113
import { createAppWrapper } from './create-app-wrapper.js'
1214

13-
import { getProjectPath } from '@/engine-handling/server-environment.js'
14-
import {
15-
cleanUpFileArray,
16-
cleanUpFiles,
17-
} from '@/engine-handling/file-helpers.js'
15+
import type { Response } from 'express'
1816

1917
export async function addToAppWrapper(
2018
addOns: Array<string>,
2119
opts: {
2220
dryRun?: boolean
23-
stream?: boolean
21+
response?: Response
2422
},
2523
) {
2624
const projectPath = getProjectPath()
2725

2826
const persistedOptions = JSON.parse(
29-
readFileSync(resolve(projectPath, '.cta.json')),
27+
readFileSync(resolve(projectPath, '.cta.json')).toString(),
3028
)
3129

30+
persistedOptions.targetDir = projectPath
31+
3232
const newAddons: Array<string> = []
3333
for (const addOn of addOns) {
3434
if (!persistedOptions.existingAddOns.includes(addOn)) {
@@ -64,42 +64,38 @@ export async function addToAppWrapper(
6464

6565
const { environment, output } = await createEnvironment()
6666

67-
if (opts.stream) {
68-
return new ReadableStream({
69-
start(controller) {
70-
environment.startStep = ({ id, type, message }) => {
71-
controller.enqueue(
72-
new TextEncoder().encode(
73-
JSON.stringify({
74-
msgType: 'start',
75-
id,
76-
type,
77-
message,
78-
}) + '\n',
79-
),
80-
)
81-
}
82-
environment.finishStep = (id, message) => {
83-
controller.enqueue(
84-
new TextEncoder().encode(
85-
JSON.stringify({
86-
msgType: 'finish',
87-
id,
88-
message,
89-
}) + '\n',
90-
),
91-
)
92-
}
67+
if (opts.response) {
68+
opts.response.writeHead(200, {
69+
'Content-Type': 'text/plain',
70+
'Transfer-Encoding': 'chunked',
71+
})
72+
73+
environment.startStep = ({ id, type, message }) => {
74+
opts.response!.write(
75+
JSON.stringify({
76+
msgType: 'start',
77+
id,
78+
type,
79+
message,
80+
}) + '\n',
81+
)
82+
}
83+
environment.finishStep = (id, message) => {
84+
opts.response!.write(
85+
JSON.stringify({
86+
msgType: 'finish',
87+
id,
88+
message,
89+
}) + '\n',
90+
)
91+
}
9392

94-
environment.startRun()
95-
addToApp(environment, newAddons, projectPath, {
96-
forced: true,
97-
}).then(() => {
98-
environment.finishRun()
99-
controller.close()
100-
})
101-
},
93+
environment.startRun()
94+
await addToApp(environment, newAddons, projectPath, {
95+
forced: true,
10296
})
97+
environment.finishRun()
98+
opts.response.end()
10399
} else {
104100
environment.startRun()
105101
await addToApp(environment, newAddons, projectPath, {

packages/cta-ui/src/engine-handling/create-app-wrapper.ts renamed to packages/cta-ui/lib/engine-handling/create-app-wrapper.ts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ import {
99
loadStarter,
1010
} from '@tanstack/cta-engine'
1111

12-
import { registerFrameworks } from './framework-registration'
12+
import { registerFrameworks } from './framework-registration.js'
1313

14-
import { cleanUpFileArray, cleanUpFiles } from './file-helpers'
15-
import { getApplicationMode, getProjectPath } from './server-environment'
14+
import { cleanUpFileArray, cleanUpFiles } from './file-helpers.js'
15+
import { getApplicationMode, getProjectPath } from './server-environment.js'
1616

1717
import type { Options, SerializedOptions, Starter } from '@tanstack/cta-engine'
1818

19+
import type { Response } from 'express'
20+
1921
export async function createAppWrapper(
2022
projectOptions: SerializedOptions,
21-
opts: { dryRun?: boolean; stream?: boolean },
23+
opts: { dryRun?: boolean; response?: Response },
2224
) {
2325
registerFrameworks()
2426

2527
const framework = getFrameworkById(projectOptions.framework)!
2628

2729
let starter: Starter | undefined
28-
const addOns: Array<string> = [...projectOptions.chosenAddOns]
30+
const addOns: Array<string> = [...(projectOptions.chosenAddOns || [])]
2931
if (projectOptions.starter) {
3032
starter = await loadStarter(projectOptions.starter)
31-
for (const addOn of starter.dependsOn ?? []) {
33+
for (const addOn of starter?.dependsOn ?? []) {
3234
addOns.push(addOn)
3335
}
3436
}
@@ -64,38 +66,34 @@ export async function createAppWrapper(
6466

6567
const { environment, output } = createEnvironment()
6668

67-
if (opts.stream) {
68-
return new ReadableStream({
69-
start(controller) {
70-
environment.startStep = ({ id, type, message }) => {
71-
controller.enqueue(
72-
new TextEncoder().encode(
73-
JSON.stringify({
74-
msgType: 'start',
75-
id,
76-
type,
77-
message,
78-
}) + '\n',
79-
),
80-
)
81-
}
82-
environment.finishStep = (id, message) => {
83-
controller.enqueue(
84-
new TextEncoder().encode(
85-
JSON.stringify({
86-
msgType: 'finish',
87-
id,
88-
message,
89-
}) + '\n',
90-
),
91-
)
92-
}
93-
94-
createApp(environment, options).then(() => {
95-
controller.close()
96-
})
97-
},
69+
if (opts.response) {
70+
opts.response.writeHead(200, {
71+
'Content-Type': 'text/plain',
72+
'Transfer-Encoding': 'chunked',
9873
})
74+
75+
environment.startStep = ({ id, type, message }) => {
76+
opts.response!.write(
77+
JSON.stringify({
78+
msgType: 'start',
79+
id,
80+
type,
81+
message,
82+
}) + '\n',
83+
)
84+
}
85+
environment.finishStep = (id, message) => {
86+
opts.response!.write(
87+
JSON.stringify({
88+
msgType: 'finish',
89+
id,
90+
message,
91+
}) + '\n',
92+
)
93+
}
94+
95+
await createApp(environment, options)
96+
opts.response.end()
9997
} else {
10098
await createApp(environment, options)
10199

packages/cta-ui/src/engine-handling/generate-initial-payload.ts renamed to packages/cta-ui/lib/engine-handling/generate-initial-payload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from './server-environment.js'
2222

2323
import type { SerializedOptions } from '@tanstack/cta-engine'
24-
import type { Registry } from '@/types.js'
24+
import type { Registry } from '../types.js'
2525

2626
function absolutizeUrl(originalUrl: string, relativeUrl: string) {
2727
if (relativeUrl.startsWith('http') || relativeUrl.startsWith('https')) {
@@ -58,7 +58,7 @@ export async function generateInitialPayload() {
5858
} as SerializedOptions
5959
} else {
6060
const persistedOptions = JSON.parse(
61-
readFileSync(resolve(projectPath, '.cta.json')),
61+
readFileSync(resolve(projectPath, '.cta.json')).toString(),
6262
)
6363
return createSerializedOptionsFromPersisted(persistedOptions)
6464
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Mode, SerializedOptions } from '@tanstack/cta-engine'
2+
3+
export type ServerEnvironment = {
4+
projectPath: string
5+
mode: 'add' | 'setup'
6+
options?: SerializedOptions
7+
addOns?: Array<string>
8+
forcedRouterMode?: Mode
9+
forcedAddOns?: Array<string>
10+
registry?: string
11+
}
12+
13+
const serverEnvironment: ServerEnvironment = {
14+
projectPath: '',
15+
mode: 'add',
16+
options: {} as SerializedOptions,
17+
addOns: [],
18+
forcedRouterMode: undefined,
19+
forcedAddOns: undefined,
20+
registry: undefined,
21+
}
22+
23+
export function setServerEnvironment(options: Partial<ServerEnvironment>) {
24+
Object.assign(serverEnvironment, options)
25+
}
26+
27+
export const getProjectPath = () => serverEnvironment.projectPath
28+
29+
export const getApplicationMode = () => serverEnvironment.mode
30+
31+
export const getProjectOptions = () => serverEnvironment.options!
32+
33+
export const getForcedRouterMode = () => serverEnvironment.forcedRouterMode
34+
35+
export const getForcedAddOns = () => serverEnvironment.forcedAddOns || []
36+
37+
export const getRegistry = () => serverEnvironment.registry

0 commit comments

Comments
 (0)