| 1 | import { randomUUID } from "crypto"; |
| 2 | import { count, eq |
| 3 | import { z } |
| 4 | import { db } |
| 5 | import { eggVariables, eggs |
| 6 | import { adminProcedure, protectedProcedure |
| 7 | |
| 8 | async function isSetupDone() |
| 9 | const row = |
| 10 | where: eq(settings |
| 11 | }); |
| 12 | return row?.value |
| 13 | } |
| 14 | |
| 15 | export const onboardingRouter = |
| 16 | isSetupRequired: publicProcedure.handler |
| 17 | return !(await |
| 18 | }), |
| 19 | |
| 20 | promoteFirstAdmin: protectedProcedure.handler |
| 21 | if (await isSetupDone |
| 22 | throw new Error( |
| 23 | } |
| 24 | const [{ adminCount |
| 25 | .select({ adminCount: |
| 26 | .from(user) |
| 27 | .where(eq(user |
| 28 | if (adminCount > |
| 29 | throw new Error( |
| 30 | } |
| 31 | await db |
| 32 | .update(user) |
| 33 | .set({ role: |
| 34 | .where(eq(user |
| 35 | }), |
| 36 | |
| 37 | listEggRepositories: publicProcedure.handler |
| 38 | type EggEntry |
| 39 | type RepoResult |
| 40 | id: string |
| 41 | label: string |
| 42 | categories: { |
| 43 | }; |
| 44 | |
| 45 | const repos = |
| 46 | { id: "game-eggs", |
| 47 | { |
| 48 | id: "application-eggs", |
| 49 | label: "Application Eggs", |
| 50 | owner: "pterodactyl", |
| 51 | repo: "application-eggs", |
| 52 | }, |
| 53 | { id: "generic-eggs", |
| 54 | ]; |
| 55 | |
| 56 | const results = |
| 57 | repos.map(async |
| 58 | const res = |
| 59 | `https://api.github.com/repos/${owner}/${ |
| 60 | { |
| 61 | headers: { Accept: " |
| 62 | signal: AbortSignal.timeout |
| 63 | }, |
| 64 | ); |
| 65 | if (!res.ok |
| 66 | |
| 67 | const data = |
| 68 | const jsonFiles = |
| 69 | (item) => |
| 70 | item.type === |
| 71 | item.path.endsWith |
| 72 | !item.path.includes |
| 73 | !item.path.toLowerCase |
| 74 | ); |
| 75 | |
| 76 | const categoryMap = |
| 77 | |
| 78 | for (const file |
| 79 | const parts = |
| 80 | if (parts.length |
| 81 | const category = |
| 82 | const name = |
| 83 | .replace(/\.json |
| 84 | .replace(/^egg[ |
| 85 | .replace(/[-_] |
| 86 | const rawUrl = |
| 87 | |
| 88 | if (!categoryMap.has |
| 89 | categoryMap.get(category |
| 90 | } |
| 91 | |
| 92 | const categories = |
| 93 | category, |
| 94 | eggs: eggList, |
| 95 | })); |
| 96 | |
| 97 | return { id, |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | return (results.filter |
| 102 | (r) => |
| 103 | ); |
| 104 | }), |
| 105 | |
| 106 | importEggs: adminProcedure |
| 107 | .input( |
| 108 | z.object({ |
| 109 | eggs: z.array |
| 110 | z.object({ |
| 111 | nestName: z.string |
| 112 | rawUrl: z.string |
| 113 | }), |
| 114 | ), |
| 115 | }), |
| 116 | ) |
| 117 | .handler(async ( |
| 118 | const nestCache = |
| 119 | let imported = |
| 120 | |
| 121 | async function getOrCreateNest |
| 122 | if (nestCache.has |
| 123 | |
| 124 | const existing = |
| 125 | where: eq(nests |
| 126 | }); |
| 127 | if (existing) { |
| 128 | nestCache.set(name |
| 129 | return existing.id |
| 130 | } |
| 131 | |
| 132 | const id = |
| 133 | await db.insert |
| 134 | id, |
| 135 | uuid: randomUUID(), |
| 136 | name, |
| 137 | description: null, |
| 138 | author: "onboarding-import", |
| 139 | }); |
| 140 | nestCache.set(name |
| 141 | return id; |
| 142 | } |
| 143 | |
| 144 | for (const item |
| 145 | try { |
| 146 | const res = |
| 147 | if (!res.ok |
| 148 | |
| 149 | const raw = |
| 150 | name: string; |
| 151 | description?: string |
| 152 | startup?: string |
| 153 | config?: { |
| 154 | scripts?: { |
| 155 | variables?: Array |
| 156 | name: string |
| 157 | description?: string |
| 158 | env_variable: string |
| 159 | default_value?: string |
| 160 | user_viewable?: boolean |
| 161 | user_editable?: boolean |
| 162 | rules?: string |
| 163 | }>; |
| 164 | docker_images?: Record |
| 165 | }; |
| 166 | |
| 167 | const nestId = |
| 168 | const eggId = |
| 169 | |
| 170 | await db.insert |
| 171 | id: eggId, |
| 172 | uuid: randomUUID(), |
| 173 | nestId, |
| 174 | name: raw.name |
| 175 | description: raw.description |
| 176 | startup: raw.startup |
| 177 | dockerImages: JSON.stringify |
| 178 | configFiles: raw.config |
| 179 | configStartup: raw.config |
| 180 | configStop: raw.config |
| 181 | configLogs: raw.config |
| 182 | scriptInstall: raw.scripts |
| 183 | scriptContainer: raw.scripts |
| 184 | scriptEntry: raw.scripts |
| 185 | }); |
| 186 | |
| 187 | if (raw.variables |
| 188 | await db.insert |
| 189 | raw.variables.map |
| 190 | id: randomUUID(), |
| 191 | eggId, |
| 192 | name: v.name |
| 193 | description: v.description |
| 194 | envVariable: v.env_variable |
| 195 | defaultValue: v.default_value |
| 196 | userViewable: v.user_viewable |
| 197 | userEditable: v.user_editable |
| 198 | rules: v.rules |
| 199 | })), |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | imported++; |
| 204 | } catch { |
| 205 | // skip failed imports silently |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return { imported |
| 210 | }), |
| 211 | |
| 212 | completeSetup: adminProcedure.handler |
| 213 | await db |
| 214 | .insert(settings) |
| 215 | .values({ key: |
| 216 | .onDuplicateKeyUpdate({ set: |
| 217 | }), |
| 218 | }; |