Open History to see the last commit
1import type {
2 AddOn,
3 AttributedFile,
4 DependencyAttribution,
5 FileProvenance,
6 Framework,
7 Integration,
8 IntegrationWithSource,
9 LineAttribution,
10 Starter,
11} from './types.js'
12
13export interface AttributionInput {
14 framework: Framework
15 chosenAddOns: Array<AddOn>
16 starter?: Starter
17 files: Record<string, string>
18}
19
20export interface AttributionOutput {
21 attributedFiles: Record<string, AttributedFile>
22 dependencies: Array<DependencyAttribution>
23}
24
25type Source = { sourceId: string; sourceName: string }
26
27// A pattern to search for in file content, with its source add-on
28interface Injection {
29 matches: (line: string) => boolean
30 appliesTo: (filePath: string) => boolean
31 source: Source
32}
33
34function normalizePath(path: string): string {
35 let p = path.startsWith('./') ? path.slice(2) : path
36 p = p.replace(/\.ejs$/, '').replace(/_dot_/g, '.')
37 const match = p.match(/^(.+\/)?__([^_]+)__(.+)$/)
38 return match ? (match[1] || '') + match[3] : p
39}
40
41async function getFileProvenance(
42 filePath: string,
43 framework: Framework,
44 addOns: Array<AddOn>,
45 starter?: Starter,
46): Promise<FileProvenance | null> {
47 const target = filePath.startsWith('./') ? filePath.slice(2) : filePath
48
49 if (starter) {
50 const files = await starter.getFiles()
51 if (files.some((f: string) => normalizePath(f) === target)) {
52 return {
53 source: 'starter',
54 sourceId: starter.id,
55 sourceName: starter.name,
56 }
57 }
58 }
59
60 // Order add-ons by type then phase (matches writeFiles order), check in reverse
61 const typeOrder = ['add-on', 'example', 'toolchain', 'deployment']
62 const phaseOrder = ['setup', 'add-on', 'example']
63 const ordered = typeOrder.flatMap((type) =>
64 phaseOrder.flatMap((phase) =>
65 addOns.filter((a) => a.phase === phase && a.type === type),
66 ),
67 )
68
69 for (let i = ordered.length - 1; i >= 0; i--) {
70 const files = await ordered[i].getFiles()
71 if (files.some((f: string) => normalizePath(f) === target)) {
72 return {
73 source: 'add-on',
74 sourceId: ordered[i].id,
75 sourceName: ordered[i].name,
76 }
77 }
78 }
79
80 const frameworkFiles = await framework.getFiles()
81 if (frameworkFiles.some((f: string) => normalizePath(f) === target)) {
82 return {
83 source: 'framework',
84 sourceId: framework.id,
85 sourceName: framework.name,
86 }
87 }
88
89 return null
90}
91
92// Build injection patterns from integrations (for source files)
93function integrationInjections(int: IntegrationWithSource): Array<Injection> {
94 const source = { sourceId: int._sourceId, sourceName: int._sourceName }
95 const injections: Array<Injection> = []
96
97 const appliesTo = (path: string) => {
98 if (int.type === 'vite-plugin') return path.includes('vite.config')
99 if (
100 int.type === 'provider' ||
101 int.type === 'root-provider' ||
102 int.type === 'devtools'
103 ) {
104 return path.includes('__root') || path.includes('root.tsx')
105 }
106 return false
107 }
108
109 if (int.import) {
110 const prefix = int.import.split(' from ')[0]
111 injections.push({
112 matches: (line) => line.includes(prefix),
113 appliesTo,
114 source,
115 })
116 }
117
118 const code = int.code || int.jsName
119 if (code) {
120 injections.push({
121 matches: (line) => line.includes(code),
122 appliesTo,
123 source,
124 })
125 }
126
127 return injections
128}
129
130// Build injection pattern from a dependency (for package.json)
131function dependencyInjection(dep: DependencyAttribution): Injection {
132 return {
133 matches: (line) => line.includes(`"${dep.name}"`),
134 appliesTo: (path) => path.endsWith('package.json'),
135 source: { sourceId: dep.sourceId, sourceName: dep.sourceName },
136 }
137}
138
139export async function computeAttribution(
140 input: AttributionInput,
141): Promise<AttributionOutput> {
142 const { framework, chosenAddOns, starter, files } = input
143
144 // Collect integrations tagged with source
145 const integrations: Array<IntegrationWithSource> = chosenAddOns.flatMap(
146 (addOn) =>
147 (addOn.integrations || []).map((int: Integration) => ({
148 ...int,
149 _sourceId: addOn.id,
150 _sourceName: addOn.name,
151 })),
152 )
153
154 // Collect dependencies from add-ons (from packageAdditions or packageTemplate)
155 const dependencies: Array<DependencyAttribution> = chosenAddOns.flatMap(
156 (addOn) => {
157 const result: Array<DependencyAttribution> = []
158 const source = { sourceId: addOn.id, sourceName: addOn.name }
159
160 const addDeps = (
161 deps: Record<string, unknown> | undefined,
162 type: 'dependency' | 'devDependency',
163 ) => {
164 if (!deps) return
165 for (const [name, version] of Object.entries(deps)) {
166 if (typeof version === 'string') {
167 result.push({ name, version, type, ...source })
168 }
169 }
170 }
171
172 // From static package.json
173 addDeps(addOn.packageAdditions?.dependencies, 'dependency')
174 addDeps(addOn.packageAdditions?.devDependencies, 'devDependency')
175
176 // From package.json.ejs template (strip EJS tags and parse)
177 if (addOn.packageTemplate) {
178 try {
179 const tmpl = JSON.parse(
180 addOn.packageTemplate.replace(/"[^"]*<%[^%]*%>[^"]*"/g, '""'),
181 )
182 addDeps(tmpl.dependencies, 'dependency')
183 addDeps(tmpl.devDependencies, 'devDependency')
184 } catch {}
185 }
186
187 return result
188 },
189 )
190
191 // Build unified injection patterns from both integrations and dependencies
192 const injections: Array<Injection> = [
193 ...integrations.flatMap(integrationInjections),
194 ...dependencies.map(dependencyInjection),
195 ]
196
197 const attributedFiles: Record<string, AttributedFile> = {}
198
199 for (const [filePath, content] of Object.entries(files)) {
200 const provenance = await getFileProvenance(
201 filePath,
202 framework,
203 chosenAddOns,
204 starter,
205 )
206 if (!provenance) continue
207
208 const lines = content.split('\n')
209 const relevant = injections.filter((inj) => inj.appliesTo(filePath))
210
211 // Find injected lines
212 const injectedLines = new Map<number, Source>()
213 for (const inj of relevant) {
214 lines.forEach((line, i) => {
215 if (inj.matches(line) && !injectedLines.has(i + 1)) {
216 injectedLines.set(i + 1, inj.source)
217 }
218 })
219 }
220
221 attributedFiles[filePath] = {
222 content,
223 provenance,
224 lineAttributions: lines.map((_, i): LineAttribution => {
225 const lineNum = i + 1
226 const inj = injectedLines.get(lineNum)
227 return inj
228 ? {
229 line: lineNum,
230 sourceId: inj.sourceId,
231 sourceName: inj.sourceName,
232 type: 'injected',
233 }
234 : {
235 line: lineNum,
236 sourceId: provenance.sourceId,
237 sourceName: provenance.sourceName,
238 type: 'original',
239 }
240 }),
241 }
242 }
243
244 return { attributedFiles, dependencies }
245}