Open History to see the last commit
1import { loadRemoteAddOn } from './custom-add-ons/add-on.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 // Higher priority first
22 })
23}
24
25// Turn the list of chosen add-on IDs into a final list of add-ons by resolving dependencies
26export async function finalizeAddOns(
27 framework: Framework,
28 mode: string,
29 chosenAddOnIDs: Array<string>,
30 bundlerId?: string,
31): Promise<Array<AddOn>> {
32 const bundler = resolveBundler(framework, bundlerId).id
33 const finalAddOnIDs = new Set(chosenAddOnIDs)
34
35 const addOns = framework
36 .getAddOns()
37 .filter((addOn) => addOn.modes.includes(mode))
38
39 for (const addOnID of finalAddOnIDs) {
40 let addOn: AddOn | undefined
41 const localAddOn =
42 addOns.find((a) => a.id === addOnID) ??
43 addOns.find((a) => a.id.toLowerCase() === addOnID.toLowerCase())
44 if (localAddOn) {
45 addOn = loadAddOn(localAddOn)
46 if (localAddOn.id !== addOnID) {
47 // Replace the mistyped ID with the canonical one
48 finalAddOnIDs.delete(addOnID)
49 finalAddOnIDs.add(localAddOn.id)
50 }
51 } else if (addOnID.startsWith('http')) {
52 addOn = await loadRemoteAddOn(addOnID)
53 addOns.push(addOn)
54 } else {
55 const suggestion = findClosestAddOn(addOnID, addOns)
56 throw new Error(
57 `Add-on ${addOnID} not found${suggestion ? `. Did you mean "${suggestion}"?` : ''}`,
58 )
59 }
60
61 assertAddOnSupportedByBundler(addOn, bundler)
62
63 for (const dependsOn of addOn.dependsOn || []) {
64 const dep = addOns.find((a) => a.id === dependsOn)
65 if (!dep) {
66 throw new Error(`Dependency ${dependsOn} not found`)
67 }
68 assertAddOnSupportedByBundler(dep, bundler)
69 finalAddOnIDs.add(dep.id)
70 }
71 }
72
73 const finalAddOns = [...finalAddOnIDs].map(
74 (id) => addOns.find((a) => a.id === id)!,
75 )
76
77 return finalAddOns
78}
79
80function loadAddOn(addOn: AddOn): AddOn {
81 return addOn
82}
83
84function findClosestAddOn(
85 input: string,
86 addOns: Array<AddOn>,
87): string | undefined {
88 const inputLower = input.toLowerCase()
89 let bestMatch: string | undefined
90 let bestDistance = Infinity
91
92 for (const addOn of addOns) {
93 const d = levenshtein(inputLower, addOn.id.toLowerCase())
94 if (d < bestDistance) {
95 bestDistance = d
96 bestMatch = addOn.id
97 }
98 }
99
100 // Only suggest if the distance is reasonable (less than half the input length)
101 if (bestMatch && bestDistance <= Math.max(Math.floor(input.length / 2), 2)) {
102 return bestMatch
103 }
104 return undefined
105}
106
107function levenshtein(a: string, b: string): number {
108 const m = a.length
109 const n = b.length
110 let prev = Array.from({ length: n + 1 }, (_, j) => j)
111
112 for (let i = 1; i <= m; i++) {
113 const curr = [i]
114 for (let j = 1; j <= n; j++) {
115 curr[j] =
116 a[i - 1] === b[j - 1]
117 ? prev[j - 1]
118 : 1 + Math.min(prev[j], curr[j - 1], prev[j - 1])
119 }
120 prev = curr
121 }
122
123 return prev[n]
124}
125
126export function populateAddOnOptionsDefaults(
127 chosenAddOns: Array<AddOn>,
128): Record<string, Record<string, any>> {
129 const addOnOptions: Record<string, Record<string, any>> = {}
130
131 for (const addOn of chosenAddOns) {
132 if (addOn.options) {
133 const defaults: Record<string, any> = {}
134 for (const [optionKey, optionDef] of Object.entries(addOn.options)) {
135 defaults[optionKey] = optionDef.default
136 }
137 addOnOptions[addOn.id] = defaults
138 }
139 }
140
141 return addOnOptions
142}