| 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 | const base = |
| 70 | return item.type |
| 71 | }); |
| 72 | |
| 73 | const categoryMap = |
| 74 | |
| 75 | for (const file |
| 76 | const parts = |
| 77 | if (parts.length |
| 78 | const category = |
| 79 | const name = |
| 80 | .replace(/\.json |
| 81 | .replace(/^egg[ |
| 82 | .replace(/[-_] |
| 83 | const rawUrl = |
| 84 | |
| 85 | if (!categoryMap.has |
| 86 | categoryMap.get(category |
| 87 | } |
| 88 | |
| 89 | const categories = |
| 90 | category, |
| 91 | eggs: eggList, |
| 92 | })); |
| 93 | |
| 94 | return { id, |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | return (results.filter |
| 99 | (r) => |
| 100 | ); |
| 101 | }), |
| 102 | |
| 103 | importEggs: adminProcedure |
| 104 | .input( |
| 105 | z.object({ |
| 106 | eggs: z.array |
| 107 | z.object({ |
| 108 | nestName: z.string |
| 109 | rawUrl: z.string |
| 110 | }), |
| 111 | ), |
| 112 | }), |
| 113 | ) |
| 114 | .handler(async ( |
| 115 | const nestCache = |
| 116 | let imported = |
| 117 | const skipped: |
| 118 | |
| 119 | async function getOrCreateNest |
| 120 | if (nestCache.has |
| 121 | |
| 122 | const existing = |
| 123 | where: eq(nests |
| 124 | }); |
| 125 | if (existing) { |
| 126 | nestCache.set(name |
| 127 | return existing.id |
| 128 | } |
| 129 | |
| 130 | const id = |
| 131 | await db.insert |
| 132 | id, |
| 133 | uuid: randomUUID(), |
| 134 | name, |
| 135 | description: null, |
| 136 | author: "onboarding-import", |
| 137 | }); |
| 138 | nestCache.set(name |
| 139 | return id; |
| 140 | } |
| 141 | |
| 142 | for (const item |
| 143 | const fallbackName = |
| 144 | item.rawUrl.split |
| 145 | |
| 146 | let raw: |
| 147 | name: string |
| 148 | description?: string |
| 149 | startup?: string |
| 150 | config?: { |
| 151 | scripts?: { |
| 152 | variables?: Array |
| 153 | name: string |
| 154 | description?: string |
| 155 | env_variable: string |
| 156 | default_value?: string |
| 157 | user_viewable?: boolean |
| 158 | user_editable?: boolean |
| 159 | rules?: string |
| 160 | }>; |
| 161 | docker_images?: Record |
| 162 | }; |
| 163 | |
| 164 | try { |
| 165 | const res = |
| 166 | if (!res.ok |
| 167 | skipped.push({ |
| 168 | continue; |
| 169 | } |
| 170 | raw = (await |
| 171 | if (!raw?.name |
| 172 | skipped.push({ |
| 173 | continue; |
| 174 | } |
| 175 | } catch { |
| 176 | skipped.push({ |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | try { |
| 181 | const nestId = |
| 182 | const eggId = |
| 183 | |
| 184 | await db.insert |
| 185 | id: eggId, |
| 186 | uuid: randomUUID(), |
| 187 | nestId, |
| 188 | name: raw.name |
| 189 | description: raw.description |
| 190 | startup: raw.startup |
| 191 | dockerImages: JSON.stringify |
| 192 | configFiles: raw.config |
| 193 | configStartup: raw.config |
| 194 | configStop: raw.config |
| 195 | configLogs: raw.config |
| 196 | scriptInstall: raw.scripts |
| 197 | scriptContainer: raw.scripts |
| 198 | scriptEntry: raw.scripts |
| 199 | }); |
| 200 | |
| 201 | if (raw.variables |
| 202 | await db.insert |
| 203 | raw.variables.map |
| 204 | id: randomUUID(), |
| 205 | eggId, |
| 206 | name: v.name |
| 207 | description: v.description |
| 208 | envVariable: v.env_variable |
| 209 | defaultValue: v.default_value |
| 210 | userViewable: v.user_viewable |
| 211 | userEditable: v.user_editable |
| 212 | rules: v.rules |
| 213 | })), |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | imported++; |
| 218 | } catch { |
| 219 | skipped.push({ |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return { imported, |
| 224 | }), |
| 225 | |
| 226 | completeSetup: adminProcedure.handler |
| 227 | await db |
| 228 | .insert(settings) |
| 229 | .values({ key: |
| 230 | .onDuplicateKeyUpdate({ set: |
| 231 | }), |
| 232 | }; |