Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

create-app-wrapper.ts

124 lines3.0 KB

1import { resolve } from 'node:path'
2​
3import {
4 createApp,
5 createDefaultEnvironment,
6 createMemoryEnvironment,
7 finalizeAddOns,
8 getFrameworkById,
9 loadStarter,
10 populateAddOnOptionsDefaults,
11} from '@tanstack/create'
12​
13import { TMP_TARGET_DIR } from '../constants.js'
14​
15import { cleanUpFileArray, cleanUpFiles } from './file-helpers.js'
16import { getApplicationMode, getProjectPath } from './server-environment.js'
17​
18import type {
19 Environment,
20 Options,
21 SerializedOptions,
22 Starter,
23} from '@tanstack/create'
24​
25import type { Response } from 'express'
26​
27export async function createAppWrapper(
28 projectOptions: SerializedOptions,
29 opts: {
30 dryRun?: boolean
31 response?: Response
32 environmentFactory?: () => Environment
33 },
34) {
35 const framework = getFrameworkById(projectOptions.framework)!
36 if (!framework) {
37 throw new Error(`Framework ${projectOptions.framework} not found`)
38 }
39​
40 let starter: Starter | undefined
41 const addOns: Array<string> = [...projectOptions.chosenAddOns]
42 if (projectOptions.starter) {
43 starter = await loadStarter(projectOptions.starter)
44 if (starter)
45 for (const addOn of starter.dependsOn ?? []) {
46 addOns.push(addOn)
47 }
48 }
49 const chosenAddOns = await finalizeAddOns(
50 framework,
51 projectOptions.mode,
52 addOns,
53 )
54​
55 const projectPath = getProjectPath()
56 const targetDir = opts.dryRun
57 ? TMP_TARGET_DIR
58 : getApplicationMode() === 'add'
59 ? projectOptions.targetDir
60 : resolve(projectPath, projectOptions.projectName)
61​
62 const options: Options = {
63 ...projectOptions,
64 targetDir,
65 starter,
66 framework,
67 chosenAddOns,
68 addOnOptions:
69 !projectOptions.addOnOptions ||
70 Object.keys(projectOptions.addOnOptions).length === 0
71 ? populateAddOnOptionsDefaults(chosenAddOns)
72 : projectOptions.addOnOptions,
73 }
74​
75 function createEnvironment() {
76 if (opts.dryRun) {
77 return createMemoryEnvironment(targetDir)
78 }
79 return {
80 environment: opts.environmentFactory?.() ?? createDefaultEnvironment(),
81 output: { files: {}, deletedFiles: [], commands: [] },
82 }
83 }
84​
85 const { environment, output } = createEnvironment()
86​
87 if (opts.response) {
88 opts.response.writeHead(200, {
89 'Content-Type': 'text/plain',
90 'Transfer-Encoding': 'chunked',
91 })
92​
93 environment.startStep = ({ id, type, message }) => {
94 opts.response!.write(
95 JSON.stringify({
96 msgType: 'start',
97 id,
98 type,
99 message,
100 }) + '\n',
101 )
102 }
103 environment.finishStep = (id, message) => {
104 opts.response!.write(
105 JSON.stringify({
106 msgType: 'finish',
107 id,
108 message,
109 }) + '\n',
110 )
111 }
112​
113 await createApp(environment, options)
114 opts.response.end()
115 } else {
116 await createApp(environment, options)
117​
118 // Preserve base64 content for WebContainer, FileViewer will display placeholders
119 output.files = cleanUpFiles(output.files, targetDir, true)
120 output.deletedFiles = cleanUpFileArray(output.deletedFiles, targetDir)
121​
122 return output
123 }