|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { getConfiguration } from "../../../context"; |
| 4 | +import { outputChannel, render, toastError } from "../../../utils"; |
| 5 | +import { createDrizzleStudioPanel } from "../panel"; |
| 6 | +import { startStudio } from "../server"; |
| 7 | + |
| 8 | +export const OpenStudioCommand = "drizzle.studio:open"; |
| 9 | + |
| 10 | +export async function OpenStudio(...args: any[]) { |
| 11 | + const OutputKey = `[${OpenStudioCommand}]`; |
| 12 | + const configPath = args[0]; |
| 13 | + const envFile = args[1]; |
| 14 | + |
| 15 | + if (!configPath || typeof configPath !== "string") { |
| 16 | + toastError(`${OutputKey} Expected config path to be a string`); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + await startStudio(configPath, envFile); |
| 22 | + const panel = createDrizzleStudioPanel(); |
| 23 | + |
| 24 | + const studioUrl = |
| 25 | + getConfiguration<string>("studio.url") || "https://local.drizzle.studio"; |
| 26 | + |
| 27 | + outputChannel.appendLine(`${OutputKey} using Studio URL: ${studioUrl}`); |
| 28 | + |
| 29 | + panel.webview.html = render(` |
| 30 | + <iframe |
| 31 | + src="${studioUrl}" |
| 32 | + width="100%" |
| 33 | + height="100%" |
| 34 | + frameborder="0" |
| 35 | + style="border: none;" |
| 36 | + sandbox="allow-scripts allow-same-origin allow-downloads" |
| 37 | + />`); |
| 38 | + |
| 39 | + panel.reveal(); |
| 40 | + } catch (error) { |
| 41 | + toastError( |
| 42 | + `${OutputKey} Failed to start Drizzle Studio: ${ |
| 43 | + error instanceof Error ? error.message : String(error) |
| 44 | + }`, |
| 45 | + ); |
| 46 | + return; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export const SelectEnvAndOpenStudioCommand = |
| 51 | + "drizzle.studio:select_env_and_open"; |
| 52 | + |
| 53 | +/* Local state */ |
| 54 | +let $lastSelectedEnv: string | undefined; |
| 55 | + |
| 56 | +export async function SelectEnvAndOpenStudio(configPath: string) { |
| 57 | + // Find .env files in the workspace |
| 58 | + const envFiles = await vscode.workspace.findFiles( |
| 59 | + "**/.env*", |
| 60 | + "**/node_modules/**", |
| 61 | + ); |
| 62 | + |
| 63 | + // Create quick pick items |
| 64 | + const items = envFiles.map((file) => ({ |
| 65 | + label: vscode.workspace.asRelativePath(file), |
| 66 | + path: file.fsPath, |
| 67 | + })); |
| 68 | + |
| 69 | + // Add option to not use an env file |
| 70 | + items.unshift({ label: "None", path: "Don't use an env file" }); |
| 71 | + |
| 72 | + // Move last selected item to top if it exists |
| 73 | + if ($lastSelectedEnv) { |
| 74 | + const lastSelectedIndex = items.findIndex( |
| 75 | + (item) => item.path === $lastSelectedEnv, |
| 76 | + ); |
| 77 | + if (lastSelectedIndex > -1) { |
| 78 | + const [lastItem] = items.splice(lastSelectedIndex, 1); |
| 79 | + items.unshift(lastItem); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Show quick pick |
| 84 | + const selected = await vscode.window.showQuickPick(items, { |
| 85 | + placeHolder: "Select an environment file", |
| 86 | + }); |
| 87 | + |
| 88 | + if (selected) { |
| 89 | + // Save the selection to workspace configuration (except for "None") |
| 90 | + if (selected.label !== "None") { |
| 91 | + $lastSelectedEnv = selected.path; |
| 92 | + } |
| 93 | + |
| 94 | + // Call open studio command with the selected env file |
| 95 | + await vscode.commands.executeCommand( |
| 96 | + OpenStudioCommand, |
| 97 | + configPath, |
| 98 | + selected.label === "None" ? undefined : selected.path, |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments