Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
389 changes: 389 additions & 0 deletions apps/desktop/plans/20260108-2251-static-ports-json.md

Large diffs are not rendered by default.

145 changes: 142 additions & 3 deletions apps/desktop/src/lib/trpc/routers/ports/ports.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { workspaces } from "@superset/local-db";
import { observable } from "@trpc/server/observable";
import { eq } from "drizzle-orm";
import { localDb } from "main/lib/local-db";
import {
hasStaticPortsConfig,
loadStaticPorts,
staticPortsWatcher,
} from "main/lib/static-ports";
import { portManager } from "main/lib/terminal/port-manager";
import type { DetectedPort } from "shared/types";
import type { DetectedPort, StaticPort } from "shared/types";
import { z } from "zod";
import { publicProcedure, router } from "../..";
import { getWorkspacePath } from "../workspaces/utils/worktree";

type PortEvent =
| { type: "add"; port: DetectedPort }
| { type: "remove"; port: DetectedPort };

export const createPortsRouter = () => {
return router({
// Get all currently detected ports
getAll: publicProcedure.query(() => {
return portManager.getAllPorts();
}),

// Subscribe to port changes (add/remove events)
subscribe: publicProcedure.subscription(() => {
return observable<PortEvent>((emit) => {
const onAdd = (port: DetectedPort) => {
Expand All @@ -34,5 +42,136 @@ export const createPortsRouter = () => {
};
});
}),

hasStaticConfig: publicProcedure
.input(z.object({ workspaceId: z.string() }))
.query(({ input }): { hasStatic: boolean } => {
const workspace = localDb
.select()
.from(workspaces)
.where(eq(workspaces.id, input.workspaceId))
.get();

if (!workspace) {
return { hasStatic: false };
}

const workspacePath = getWorkspacePath(workspace);
if (!workspacePath) {
return { hasStatic: false };
}

return { hasStatic: hasStaticPortsConfig(workspacePath) };
}),

getStatic: publicProcedure
.input(z.object({ workspaceId: z.string() }))
.query(
({ input }): { ports: StaticPort[] | null; error: string | null } => {
const workspace = localDb
.select()
.from(workspaces)
.where(eq(workspaces.id, input.workspaceId))
.get();

if (!workspace) {
return { ports: null, error: "Workspace not found" };
}

const workspacePath = getWorkspacePath(workspace);
if (!workspacePath) {
return { ports: null, error: "Workspace path not found" };
}

const result = loadStaticPorts(workspacePath);

if (!result.exists) {
return { ports: null, error: null };
}

if (result.error) {
return { ports: null, error: result.error };
}

const portsWithWorkspace: StaticPort[] =
result.ports?.map((p) => ({
...p,
workspaceId: input.workspaceId,
})) ?? [];

return { ports: portsWithWorkspace, error: null };
},
),

getAllStatic: publicProcedure.query(
(): {
ports: StaticPort[];
errors: Array<{ workspaceId: string; error: string }>;
} => {
const allWorkspaces = localDb.select().from(workspaces).all();
const allPorts: StaticPort[] = [];
const errors: Array<{ workspaceId: string; error: string }> = [];

for (const workspace of allWorkspaces) {
const workspacePath = getWorkspacePath(workspace);
if (!workspacePath) continue;

const result = loadStaticPorts(workspacePath);

if (!result.exists) continue;

if (result.error) {
errors.push({ workspaceId: workspace.id, error: result.error });
continue;
}

if (result.ports) {
const portsWithWorkspace = result.ports.map((p) => ({
...p,
workspaceId: workspace.id,
}));
allPorts.push(...portsWithWorkspace);
}
}

return { ports: allPorts, errors };
},
),

subscribeStatic: publicProcedure
.input(z.object({ workspaceId: z.string() }))
.subscription(({ input }) => {
return observable<{ type: "change" }>((emit) => {
const workspace = localDb
.select()
.from(workspaces)
.where(eq(workspaces.id, input.workspaceId))
.get();

if (!workspace) {
return () => {};
}

const workspacePath = getWorkspacePath(workspace);
if (!workspacePath) {
return () => {};
}

staticPortsWatcher.watch(input.workspaceId, workspacePath);

const onChange = (changedWorkspaceId: string) => {
if (changedWorkspaceId === input.workspaceId) {
emit.next({ type: "change" });
}
};

staticPortsWatcher.on("change", onChange);

return () => {
staticPortsWatcher.off("change", onChange);
staticPortsWatcher.unwatch(input.workspaceId);
};
});
}),
});
};
2 changes: 2 additions & 0 deletions apps/desktop/src/main/lib/static-ports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { hasStaticPortsConfig, loadStaticPorts } from "./loader";
export { staticPortsWatcher } from "./watcher";
Loading
Loading