Open History to see the last commit
1import { AddOnCompiledSchema } from './types.js'
2import {
3 assertAddOnSupportedByBundler,
4 isAddOnSupportedByBundler,
5 resolveBundler,
6} from './build-tools.js'
7
8import type { AddOn, Framework } from './types.js'
9
10export function getAllAddOns(framework: Framework, mode: string, bundlerId?: string): Array<AddOn> {
11 const bundler = resolveBundler(framework, bundlerId).id
12 return framework
13 .getAddOns()
14 .filter(
15 (addOn) =>
16 addOn.modes.includes(mode) && isAddOnSupportedByBundler(addOn, bundler),
17 )
18 .sort((a, b) => {
19 const aPriority = a.priority ?? 0
20 const bPriority = b.priority ?? 0
21 return bPriority - aPriority
22 })
23}
24
25export async function finalizeAddOns(
26 framework: Framework,
27 mode: string,
28 chosenAddOnIDs: Array<string>,
29 bundlerId?: string,
30): Promise<Array<AddOn>> {
31 const bundler = resolveBundler(framework, bundlerId).id
32 const finalAddOnIDs = new Set(chosenAddOnIDs)
33 const addOns = framework
34 .getAddOns()
35 .filter((addOn) => addOn.modes.includes(mode))
36
37 for (const addOnID of finalAddOnIDs) {
38 let addOn: AddOn | undefined
39 const localAddOn =
40 addOns.find((a) => a.id === addOnID) ??
41 addOns.find((a) => a.id.toLowerCase() === addOnID.toLowerCase())
42
43 if (localAddOn) {
44 addOn = localAddOn
45 if (localAddOn.id !== addOnID) {
46 finalAddOnIDs.delete(addOnID)
47 finalAddOnIDs.add(localAddOn.id)
48 }
49 } else if (addOnID.startsWith('http')) {
50 addOn = await loadRemoteAddOn(addOnID)
51 addOns.push(addOn)
52 } else {
53 const suggestion = findClosestAddOn(addOnID, addOns)
54 throw new Error(
55 `Add-on ${addOnID} not found${suggestion ? `. Did you mean "${suggestion}"?` : ''}`,
56 )
57 }
58
59 assertAddOnSupportedByBundler(addOn, bundler)
60
61 for (const dependsOn of addOn.dependsOn || []) {
62 const dep = addOns.find((a) => a.id === dependsOn)
63 if (!dep) {
64 throw new Error(`Dependency ${dependsOn} not found`)
65 }
66 assertAddOnSupportedByBundler(dep, bundler)
67 finalAddOnIDs.add(dep.id)
68 }
69 }
70
71 return [...finalAddOnIDs].map((id) => addOns.find((a) => a.id === id)!)
72}
73
74export function populateAddOnOptionsDefaults(
75 chosenAddOns: Array<AddOn>,
76): Record<string, Record<string, unknown>> {
77 const addOnOptions: Record<string, Record<string, unknown>> = {}
78
79 for (const addOn of chosenAddOns) {
80 if (addOn.options) {
81 const defaults: Record<string, unknown> = {}
82 for (const [optionKey, optionDef] of Object.entries(addOn.options)) {
83 defaults[optionKey] = optionDef.default
84 }
85 addOnOptions[addOn.id] = defaults
86 }
87 }
88
89 return addOnOptions
90}
91
92export async function loadRemoteAddOn(url: string): Promise<AddOn> {
93 const response = await fetch(url)
94 const jsonContent = await response.json()
95 const checked = AddOnCompiledSchema.safeParse(jsonContent)
96
97 if (!checked.success) {
98 throw new Error(`Invalid add-on: ${url}`)
99 }
100
101 const addOn = {
102 ...checked.data,
103 id: url,
104 }
105
106 return {
107 ...addOn,
108 getFiles: () => Promise.resolve(Object.keys(addOn.files)),
109 getFileContents: (path: string) => Promise.resolve(addOn.files[path]),
110 getDeletedFiles: () => Promise.resolve(addOn.deletedFiles),
111 }
112}
113
114function findClosestAddOn(
115 input: string,
116 addOns: Array<AddOn>,
117): string | undefined {
118 const inputLower = input.toLowerCase()
119 let bestMatch: string | undefined
120 let bestDistance = Infinity
121
122 for (const addOn of addOns) {
123 const distance = levenshtein(inputLower, addOn.id.toLowerCase())
124 if (distance < bestDistance) {
125 bestDistance = distance
126 bestMatch = addOn.id
127 }
128 }
129
130 if (bestMatch && bestDistance <= Math.max(Math.floor(input.length / 2), 2)) {
131 return bestMatch
132 }
133
134 return undefined
135}
136
137function levenshtein(a: string, b: string): number {
138 const m = a.length
139 const n = b.length
140 let prev = Array.from({ length: n + 1 }, (_, j) => j)
141
142 for (let i = 1; i <= m; i++) {
143 const curr = [i]
144 for (let j = 1; j <= n; j++) {
145 curr[j] =
146 a[i - 1] === b[j - 1]
147 ? prev[j - 1]
148 : 1 + Math.min(prev[j], curr[j - 1], prev[j - 1])
149 }
150 prev = curr
151 }
152
153 return prev[n]
154}