/// import type { PluginHandlerContext, PluginServerContext } from "@getpaseo/plugin/server"; import { stat } from "node:fs/promises"; import { basename } from "node:path"; import { detectPickers, listDirectory, pickFolder, resolvePicker, resolveStartDir, } from "./server/pick-folder"; import { detectRpc, listDirRpc, openFolderRpc, pickAndOpenRpc, pickRpc, } from "./shared/picker"; import { defaultPickerSettings, pickerSettings } from "./shared/settings"; async function openWorkspaceFolder( paseo: PluginHandlerContext["paseo"], folderPath: string, picker: string, ) { const info = await stat(folderPath).catch(() => null); if (!info?.isDirectory()) { return { status: "error" as const, message: `Not a folder on this host: ${folderPath}` }; } try { const workspace = await paseo.workspaces.open({ cwd: folderPath }); return { status: "opened" as const, path: folderPath, name: workspace.name ?? basename(folderPath), workspaceId: workspace.id, projectId: workspace.projectId, picker, }; } catch (error) { return { status: "error" as const, message: `Paseo could not open ${folderPath}: ${error instanceof Error ? error.message : String(error)}`, }; } } export default function contribute(server: PluginServerContext) { const settings = server.registerSettings(pickerSettings); let pickInFlight = false; async function readSettings() { const state = await settings.read(); return state.status === "ready" ? state.values : defaultPickerSettings; } async function pick(initialDir?: string) { if (pickInFlight) { return { status: "error" as const, message: "A folder picker is already open on this host." }; } pickInFlight = true; try { return await pickFolder(await readSettings(), initialDir); } finally { pickInFlight = false; } } server.handle(detectRpc, async () => { const current = await readSettings(); const detected = detectPickers(); const resolved = resolvePicker(current, detected); return { ...detected, effectivePicker: typeof resolved === "string" ? resolved : null, defaultStartDir: resolveStartDir(current.startDir), }; }); server.handle(pickRpc, async ({ initialDir }) => { const result = await pick(initialDir); if (result.status !== "picked") return result; const info = await stat(result.path).catch(() => null); if (!info?.isDirectory()) { return { status: "error" as const, message: `Not a folder on this host: ${result.path}` }; } return result; }); server.handle(pickAndOpenRpc, async ({ initialDir }, { paseo }) => { const result = await pick(initialDir); if (result.status === "cancelled" || result.status === "error") return result; return openWorkspaceFolder(paseo, result.path, result.picker); }); server.handle(listDirRpc, async ({ path, showHidden }) => { const current = await readSettings(); return listDirectory(path ?? resolveStartDir(current.startDir), showHidden ?? false); }); server.handle(openFolderRpc, async ({ path }, { paseo }) => { return openWorkspaceFolder(paseo, path, "browser"); }); return () => {}; }