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 = addOns.find((a) => a.id === addOnID)
29 if (localAddOn) {
30 addOn = loadAddOn(localAddOn)
31 } else if (addOnID.startsWith('http')) {
32 addOn = await loadRemoteAddOn(addOnID)
33 addOns.push(addOn)
34 } else {
35 throw new Error(`Add-on ${addOnID} not found`)
36 }
37
38 for (const dependsOn of addOn.dependsOn || []) {
39 const dep = addOns.find((a) => a.id === dependsOn)
40 if (!dep) {
41 throw new Error(`Dependency ${dependsOn} not found`)
42 }
43 finalAddOnIDs.add(dep.id)
44 }
45 }
46
47 const finalAddOns = [...finalAddOnIDs].map(
48 (id) => addOns.find((a) => a.id === id)!,
49 )
50
51 return finalAddOns
52}
53
54function loadAddOn(addOn: AddOn): AddOn {
55 return addOn
56}
57
58export function populateAddOnOptionsDefaults(
59 chosenAddOns: Array<AddOn>,
60): Record<string, Record<string, any>> {
61 const addOnOptions: Record<string, Record<string, any>> = {}
62
63 for (const addOn of chosenAddOns) {
64 if (addOn.options) {
65 const defaults: Record<string, any> = {}
66 for (const [optionKey, optionDef] of Object.entries(addOn.options)) {
67 defaults[optionKey] = optionDef.default
68 }
69 addOnOptions[addOn.id] = defaults
70 }
71 }
72
73 return addOnOptions
74}