forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.ts
More file actions
373 lines (330 loc) · 10.9 KB
/
compile.ts
File metadata and controls
373 lines (330 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import {
createApp,
createMemoryEnvironment,
finalizeAddOns,
populateAddOnOptionsDefaults,
computeAttribution,
type AddOn,
type Starter,
type LineAttribution as CtaLineAttribution,
type AttributedFile as CtaAttributedFile,
} from '@tanstack/create'
type AddOnType = 'add-on' | 'example' | 'starter' | 'toolchain' | 'deployment'
type AddOnPhase = 'setup' | 'add-on'
export interface AddOnCompiled {
id: string
name: string
description: string
type: AddOnType
phase: AddOnPhase
modes: Array<string>
files: Record<string, string>
deletedFiles: Array<string>
dependsOn?: Array<string>
options?: Record<string, unknown>
link?: string
tailwind: boolean
requiresTailwind?: boolean
warning?: string
priority?: number
author?: string
version?: string
license?: string
category?: string
packageAdditions?: {
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
scripts?: Record<string, string>
}
}
export interface StarterCompiled {
id: string
name: string
description: string
type: AddOnType
phase: AddOnPhase
modes: Array<string>
files: Record<string, string>
deletedFiles: Array<string>
framework: string
mode: string
typescript: boolean
tailwind: boolean
banner?: string
dependsOn?: Array<string>
}
import { getFramework, DEFAULT_MODE, DEFAULT_REQUIRED_ADDONS, type FrameworkId } from './config'
export interface ProjectDefinition {
name: string
framework?: FrameworkId
tailwind?: boolean
features: Array<string>
featureOptions: Record<string, Record<string, unknown>>
selectedExample?: string
customIntegrations?: Array<AddOnCompiled>
customTemplate?: StarterCompiled | null
}
export interface CompileRequest {
definition: ProjectDefinition
format?: 'full' | 'summary'
}
export interface CompileResponse {
files: Record<string, string>
packages: {
dependencies: Record<string, string>
devDependencies: Record<string, string>
scripts: Record<string, string>
}
envVars: Array<{
name: string
description: string
required?: boolean
example?: string
}>
commands: Array<{
command: string
args?: Array<string>
}>
warnings: Array<string>
}
export interface CompileHandlerOptions {
format?: 'full' | 'summary'
}
async function resolveAddOns(
featureIds: Array<string>,
customAddOns: Array<AddOnCompiled>,
frameworkId: FrameworkId = 'react-cra',
): Promise<Array<AddOn>> {
const framework = getFramework(frameworkId)
const allFrameworkAddOns = framework.getAddOns()
const customIds = new Set(customAddOns.map((a: AddOnCompiled) => a.id))
const frameworkFeatureIds = featureIds.filter((id) => !customIds.has(id))
const resolvedFramework = await finalizeAddOns(
framework,
DEFAULT_MODE,
[...DEFAULT_REQUIRED_ADDONS, ...frameworkFeatureIds],
)
const customAsAddOns = customAddOns.map((compiled: AddOnCompiled) => ({
...compiled,
getFiles: () => Promise.resolve(Object.keys(compiled.files)),
getFileContents: (path: string) => Promise.resolve(compiled.files[path] ?? ''),
getDeletedFiles: () => Promise.resolve(compiled.deletedFiles || []),
})) as unknown as Array<AddOn>
for (const custom of customAsAddOns) {
if (custom.dependsOn) {
for (const depId of custom.dependsOn) {
if (!resolvedFramework.some((a: AddOn) => a.id === depId)) {
const depAddOn = allFrameworkAddOns.find((a: AddOn) => a.id === depId)
if (depAddOn && !resolvedFramework.some((a: AddOn) => a.id === depId)) {
resolvedFramework.push(depAddOn)
}
}
}
}
}
return [...resolvedFramework, ...customAsAddOns]
}
function extractEnvVars(
addOns: Array<AddOn>,
): Array<{ name: string; description: string; required?: boolean; example?: string }> {
const envVars: Array<{ name: string; description: string; required?: boolean; example?: string }> = []
const seen = new Set<string>()
for (const addOn of addOns) {
const files = addOn.files || {}
for (const [path, content] of Object.entries(files) as Array<[string, string]>) {
if (path.endsWith('.env') || path.endsWith('.env.example') || path.endsWith('.env.local.example')) {
const lines = content.split('\n')
for (const line of lines) {
const match = line.match(/^([A-Z_][A-Z0-9_]*)=(.*)$/)
if (match && !seen.has(match[1])) {
seen.add(match[1])
envVars.push({
name: match[1],
description: `Environment variable from ${addOn.name}`,
example: match[2] || undefined,
})
}
}
}
}
}
return envVars
}
function extractWarnings(addOns: Array<AddOn>): Array<string> {
return addOns
.filter((a) => a.warning)
.map((a) => `${a.name}: ${a.warning}`)
}
function _convertToStarter(template: StarterCompiled): Starter {
return {
...template,
getFiles: () => Promise.resolve(Object.keys(template.files)),
getFileContents: (path: string) => Promise.resolve(template.files[path] ?? ''),
getDeletedFiles: () => Promise.resolve(template.deletedFiles || []),
} as unknown as Starter
}
function mergeOptionsWithDefaults(
chosenAddOns: Array<AddOn>,
userOptions: Record<string, Record<string, unknown>>,
): Record<string, Record<string, unknown>> {
const defaults = populateAddOnOptionsDefaults(chosenAddOns)
const merged: Record<string, Record<string, unknown>> = { ...defaults }
for (const [addonId, options] of Object.entries(userOptions)) {
merged[addonId] = { ...merged[addonId], ...options }
}
return merged
}
export async function compileHandler(
definition: ProjectDefinition,
options: CompileHandlerOptions = {},
): Promise<CompileResponse> {
const frameworkId = definition.framework ?? 'react-cra'
const framework = getFramework(frameworkId)
// Merge selectedExample into features (CTA treats examples as add-ons)
const allFeatures = definition.selectedExample
? [...definition.features, definition.selectedExample]
: definition.features
// Custom integrations disabled until stable launch
const chosenAddOns = await resolveAddOns(allFeatures, [], frameworkId)
const { environment, output } = createMemoryEnvironment(
`/project/${definition.name}`,
)
// Custom starters disabled until stable launch
await createApp(environment, {
projectName: definition.name,
targetDir: `/project/${definition.name}`,
framework,
mode: DEFAULT_MODE,
typescript: true,
tailwind: definition.tailwind ?? true,
packageManager: 'pnpm',
git: false,
install: false,
chosenAddOns,
addOnOptions: mergeOptionsWithDefaults(chosenAddOns, definition.featureOptions),
})
const packageJson = output.files['package.json']
? JSON.parse(output.files['package.json'])
: { dependencies: {}, devDependencies: {}, scripts: {} }
return {
files: options.format === 'summary' ? {} : output.files,
packages: {
dependencies: packageJson.dependencies || {},
devDependencies: packageJson.devDependencies || {},
scripts: packageJson.scripts || {},
},
envVars: extractEnvVars(chosenAddOns),
commands: output.commands,
warnings: extractWarnings(chosenAddOns),
}
}
// Line attribution interface (used by the UI)
export interface LineAttribution {
lineNumber: number
featureId: string
featureName: string
type?: 'original' | 'injected'
}
export interface AttributedFile {
path: string
content: string
attributions: Array<LineAttribution>
}
export interface AttributedCompileOutput extends CompileResponse {
attributedFiles: Record<string, AttributedFile>
dependencies?: Array<{
name: string
version: string
type: 'dependency' | 'devDependency'
sourceId: string
sourceName: string
}>
}
export async function compileWithAttributionHandler(
definition: ProjectDefinition,
): Promise<AttributedCompileOutput> {
const frameworkId = definition.framework ?? 'react-cra'
const framework = getFramework(frameworkId)
// Merge selectedExample into features (CTA treats examples as add-ons)
const allFeatures = definition.selectedExample
? [...definition.features, definition.selectedExample]
: definition.features
// Custom integrations disabled until stable launch
const chosenAddOns = await resolveAddOns(allFeatures, [], frameworkId)
const { environment, output } = createMemoryEnvironment(
`/project/${definition.name}`,
)
// Custom starters disabled until stable launch
await createApp(environment, {
projectName: definition.name,
targetDir: `/project/${definition.name}`,
framework,
mode: DEFAULT_MODE,
typescript: true,
tailwind: definition.tailwind ?? true,
packageManager: 'pnpm',
git: false,
install: false,
chosenAddOns,
addOnOptions: mergeOptionsWithDefaults(chosenAddOns, definition.featureOptions),
})
const packageJson = output.files['package.json']
? JSON.parse(output.files['package.json'])
: { dependencies: {}, devDependencies: {}, scripts: {} }
// Compute attribution using the new cta-engine attribution system
const attribution = await computeAttribution({
framework,
chosenAddOns,
starter: undefined,
files: output.files,
})
// Convert cta-engine attribution format to our UI format
// Baseline sources (framework + required addons) get mapped to 'base'
const baselineSourceIds = new Set([frameworkId, ...DEFAULT_REQUIRED_ADDONS])
const attributedFiles: Record<string, AttributedFile> = {}
for (const [filePath, ctaFile] of Object.entries(attribution.attributedFiles)) {
const file = ctaFile as CtaAttributedFile
attributedFiles[filePath] = {
path: filePath,
content: file.content,
attributions: file.lineAttributions.map((attr: CtaLineAttribution) => {
const isBaseline = baselineSourceIds.has(attr.sourceId)
return {
lineNumber: attr.line,
featureId: isBaseline ? 'base' : attr.sourceId,
featureName: isBaseline ? 'Base Template' : attr.sourceName,
type: attr.type,
}
}),
}
}
// For files that weren't in the attribution (e.g., generated files), use base attribution
for (const [filePath, content] of Object.entries(output.files)) {
if (!attributedFiles[filePath]) {
const lines = (content as string).split('\n')
attributedFiles[filePath] = {
path: filePath,
content: content as string,
attributions: lines.map((_, i) => ({
lineNumber: i + 1,
featureId: 'base',
featureName: 'Base Template',
type: 'original' as const,
})),
}
}
}
return {
files: output.files,
packages: {
dependencies: packageJson.dependencies || {},
devDependencies: packageJson.devDependencies || {},
scripts: packageJson.scripts || {},
},
envVars: extractEnvVars(chosenAddOns),
commands: output.commands,
warnings: extractWarnings(chosenAddOns),
attributedFiles,
dependencies: attribution.dependencies,
}
}