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