import { readFile, rm, writeFile, mkdir } from "fs/promises"; import { dirname, join } from "path"; import type { RemoteAppKind, RemoteEndpoint } from "./types"; const DEFAULT_ENDPOINT_FILE = "remote-control.json"; export function remoteEndpointPath(dataDir: string, appKind?: RemoteAppKind): string { return join(dataDir, appKind ? `remote-control.${appKind}.json` : DEFAULT_ENDPOINT_FILE); } export async function readRemoteEndpoint(dataDir: string, appKind?: RemoteAppKind): Promise { const raw = await readFile(remoteEndpointPath(dataDir, appKind), "utf-8"); return JSON.parse(raw) as RemoteEndpoint; } export async function writeRemoteEndpointFiles(dataDir: string, endpoint: RemoteEndpoint): Promise { await writeRemoteEndpoint(remoteEndpointPath(dataDir, endpoint.appKind), endpoint); await writeRemoteEndpoint(remoteEndpointPath(dataDir), endpoint); } export async function removeRemoteEndpointFiles(dataDir: string, endpoint: RemoteEndpoint): Promise { await Promise.all([ removeRemoteEndpointIfOwned(remoteEndpointPath(dataDir, endpoint.appKind), endpoint), removeRemoteEndpointIfOwned(remoteEndpointPath(dataDir), endpoint), ]); } async function writeRemoteEndpoint(filePath: string, endpoint: RemoteEndpoint): Promise { await mkdir(dirname(filePath), { recursive: true }); await writeFile(filePath, JSON.stringify(endpoint, null, 2), { encoding: "utf-8", mode: 0o600 }); } async function removeRemoteEndpointIfOwned(filePath: string, endpoint: RemoteEndpoint): Promise { try { const existing = JSON.parse(await readFile(filePath, "utf-8")) as RemoteEndpoint; if (existing.pid !== endpoint.pid || existing.token !== endpoint.token || existing.port !== endpoint.port) return; await rm(filePath, { force: true }); } catch { await rm(filePath, { force: true }); } }