|
| 1 | +import { defineCommand } from "../../core/define-command.ts"; |
| 2 | +import { resolveConfig } from "../../config/index.ts"; |
| 3 | +import { createDbClient } from "@bunny.net/api"; |
| 4 | +import { resolveDbId } from "./resolve-db.ts"; |
| 5 | +import { spinner } from "../../core/ui.ts"; |
| 6 | +import { logger } from "../../core/logger.ts"; |
| 7 | +import { UserError } from "../../core/errors.ts"; |
| 8 | +import { readEnvValue } from "../../utils/env-file.ts"; |
| 9 | +import { ARG_DATABASE_ID, ENV_DATABASE_URL, ENV_DATABASE_AUTH_TOKEN } from "./constants.ts"; |
| 10 | +import { clientOptions } from "../../core/client-options.ts"; |
| 11 | + |
| 12 | +const COMMAND = `studio [${ARG_DATABASE_ID}]`; |
| 13 | +const DESCRIPTION = "Open a visual database explorer in your browser."; |
| 14 | + |
| 15 | +const ARG_PORT = "port"; |
| 16 | +const ARG_URL = "url"; |
| 17 | +const ARG_TOKEN = "token"; |
| 18 | +const ARG_NO_OPEN = "no-open"; |
| 19 | +const ARG_DEV = "dev"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Resolve database credentials — same pattern as shell.ts. |
| 23 | + */ |
| 24 | +async function resolveCredentials( |
| 25 | + urlArg: string | undefined, |
| 26 | + tokenArg: string | undefined, |
| 27 | + databaseIdArg: string | undefined, |
| 28 | + profile: string, |
| 29 | + apiKeyOverride?: string, |
| 30 | + verbose = false, |
| 31 | +): Promise<{ url: string; token: string; databaseId: string | undefined }> { |
| 32 | + let url = urlArg ?? readEnvValue(ENV_DATABASE_URL)?.value; |
| 33 | + let token = tokenArg ?? readEnvValue(ENV_DATABASE_AUTH_TOKEN)?.value; |
| 34 | + |
| 35 | + if (url && token) return { url, token, databaseId: databaseIdArg }; |
| 36 | + |
| 37 | + const config = resolveConfig(profile, apiKeyOverride); |
| 38 | + const apiClient = createDbClient(clientOptions(config, verbose)); |
| 39 | + |
| 40 | + const { id: databaseId } = await resolveDbId(apiClient, databaseIdArg); |
| 41 | + |
| 42 | + const spin = spinner("Connecting..."); |
| 43 | + spin.start(); |
| 44 | + |
| 45 | + const fetches: Promise<any>[] = []; |
| 46 | + |
| 47 | + if (!url) { |
| 48 | + fetches.push( |
| 49 | + apiClient.GET("/v2/databases/{db_id}", { |
| 50 | + params: { path: { db_id: databaseId } }, |
| 51 | + }), |
| 52 | + ); |
| 53 | + } else { |
| 54 | + fetches.push(Promise.resolve(null)); |
| 55 | + } |
| 56 | + |
| 57 | + if (!token) { |
| 58 | + spin.text = "Generating token..."; |
| 59 | + fetches.push( |
| 60 | + apiClient.PUT("/v2/databases/{db_id}/auth/generate", { |
| 61 | + params: { path: { db_id: databaseId } }, |
| 62 | + body: { authorization: "full-access", expires_at: null }, |
| 63 | + }), |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const [dbResult, tokenResult] = await Promise.all(fetches); |
| 68 | + |
| 69 | + spin.stop(); |
| 70 | + |
| 71 | + if (!url && dbResult) url = dbResult.data?.db?.url; |
| 72 | + if (!token && tokenResult) token = tokenResult.data?.token; |
| 73 | + |
| 74 | + if (!url || !token) { |
| 75 | + throw new UserError("Could not resolve database URL or generate token."); |
| 76 | + } |
| 77 | + |
| 78 | + return { url, token, databaseId }; |
| 79 | +} |
| 80 | + |
| 81 | +export const dbStudioCommand = defineCommand<{ |
| 82 | + [ARG_DATABASE_ID]?: string; |
| 83 | + [ARG_PORT]?: number; |
| 84 | + [ARG_URL]?: string; |
| 85 | + [ARG_TOKEN]?: string; |
| 86 | + [ARG_NO_OPEN]?: boolean; |
| 87 | + [ARG_DEV]?: boolean; |
| 88 | +}>({ |
| 89 | + command: COMMAND, |
| 90 | + describe: DESCRIPTION, |
| 91 | + examples: [ |
| 92 | + ["$0 db studio", "Open studio (auto-detect from .env)"], |
| 93 | + ["$0 db studio --port 3000", "Use a custom port"], |
| 94 | + ["$0 db studio db_abc123", "Open studio for a specific database"], |
| 95 | + ], |
| 96 | + |
| 97 | + builder: (yargs) => |
| 98 | + yargs |
| 99 | + .positional(ARG_DATABASE_ID, { |
| 100 | + type: "string", |
| 101 | + describe: |
| 102 | + "Database ID (db_<ulid>). Auto-detected from BUNNY_DATABASE_URL in .env if omitted.", |
| 103 | + }) |
| 104 | + .option(ARG_PORT, { |
| 105 | + type: "number", |
| 106 | + default: 4488, |
| 107 | + describe: "Port for the studio server", |
| 108 | + }) |
| 109 | + .option(ARG_URL, { |
| 110 | + type: "string", |
| 111 | + describe: "Database URL (skips API lookup)", |
| 112 | + }) |
| 113 | + .option(ARG_TOKEN, { |
| 114 | + type: "string", |
| 115 | + describe: "Auth token (skips token generation)", |
| 116 | + }) |
| 117 | + .option(ARG_NO_OPEN, { |
| 118 | + type: "boolean", |
| 119 | + default: false, |
| 120 | + describe: "Don't automatically open the browser", |
| 121 | + }) |
| 122 | + .option(ARG_DEV, { |
| 123 | + type: "boolean", |
| 124 | + default: false, |
| 125 | + hidden: true, |
| 126 | + }), |
| 127 | + |
| 128 | + handler: async ({ |
| 129 | + [ARG_DATABASE_ID]: databaseIdArg, |
| 130 | + [ARG_PORT]: port, |
| 131 | + [ARG_URL]: urlArg, |
| 132 | + [ARG_TOKEN]: tokenArg, |
| 133 | + [ARG_NO_OPEN]: noOpen, |
| 134 | + [ARG_DEV]: dev, |
| 135 | + profile, |
| 136 | + verbose, |
| 137 | + apiKey, |
| 138 | + }) => { |
| 139 | + const { createClient } = await import("@libsql/client/web"); |
| 140 | + const { startStudio } = await import("@bunny.net/database-studio"); |
| 141 | + |
| 142 | + const { url, token } = await resolveCredentials( |
| 143 | + urlArg, |
| 144 | + tokenArg, |
| 145 | + databaseIdArg, |
| 146 | + profile, |
| 147 | + apiKey, |
| 148 | + verbose, |
| 149 | + ); |
| 150 | + |
| 151 | + const client = createClient({ url, authToken: token }); |
| 152 | + |
| 153 | + logger.log(""); |
| 154 | + await startStudio({ |
| 155 | + client, |
| 156 | + port: port ?? 4488, |
| 157 | + open: !noOpen, |
| 158 | + dev, |
| 159 | + logger: { |
| 160 | + log: (msg: string) => logger.log(msg), |
| 161 | + error: (msg: string) => logger.error(msg), |
| 162 | + }, |
| 163 | + }); |
| 164 | + }, |
| 165 | +}); |
0 commit comments