import path from "node:path"; /** 当前已接线的宿主面。gui / tui 先能识别,适配仍回退 web。 */ export const SURFACES = Object.freeze(["web", "desktop", "gui", "tui"]); function norm(value) { return String(value || "").trim().toLowerCase(); } function slash(value) { return String(value || "").replace(/\\/g, "/").toLowerCase(); } function exeName(execPath) { return path.basename(String(execPath || "")).toLowerCase(); } export function pathLooksDesktop(p) { const n = slash(p); if (!n) return false; return ( n.includes("/dsh desktop/") || n.includes("/dsh desktop.app/") || n.includes("dsh-desktop") || n.includes("dsh-plugin-desktop") || /\/dsh desktop\.exe$/i.test(n) || n.endsWith("/host-process-entry.js") || n.endsWith("/desktop-cli.js") ); } export function isDesktopSurface(input = {}) { const env = input.env ?? process.env; const forced = norm(env.DSH_SURFACE); if (forced === "desktop") return true; if (forced && SURFACES.includes(forced)) return false; const execPath = input.execPath ?? process.execPath; const name = exeName(execPath); if (name === "dsh desktop.exe" || name === "dsh desktop") return true; if (pathLooksDesktop(execPath)) return true; const resourcesPath = input.resourcesPath ?? process.resourcesPath; if (pathLooksDesktop(resourcesPath)) return true; const argv = input.argv ?? process.argv; if ((Array.isArray(argv) ? argv : []).some((arg) => pathLooksDesktop(arg))) return true; const parentPort = input.parentPort !== undefined ? input.parentPort : process.parentPort; if (parentPort && (pathLooksDesktop(execPath) || pathLooksDesktop(resourcesPath))) return true; if (env.ELECTRON_RUN_AS_NODE && pathLooksDesktop(execPath)) return true; return false; } function forcedSurface(env) { const forced = norm(env.DSH_SURFACE); return SURFACES.includes(forced) ? forced : ""; } export function detectSurface(input = {}) { const env = input.env ?? process.env; const forced = forcedSurface(env); if (forced) return forced; if (isDesktopSurface(input)) return "desktop"; return "web"; } /** gui / tui 尚未单独适配,先走 web 重启与探测。 */ export function adapterFor(kind) { return kind === "desktop" ? "desktop" : "web"; }