|
| 1 | +import { exec } from "node:child_process"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import { promisify } from "node:util"; |
| 4 | +import { app, ipcMain } from "electron"; |
| 5 | +import Store from "electron-store"; |
| 6 | +import type { |
| 7 | + DetectedApplication, |
| 8 | + ExternalAppsPreferences, |
| 9 | + ExternalAppType, |
| 10 | +} from "../../shared/types"; |
| 11 | + |
| 12 | +const execAsync = promisify(exec); |
| 13 | + |
| 14 | +// Dynamic import for file-icon ESM module |
| 15 | +let fileIcon: typeof import("file-icon") | null = null; |
| 16 | +async function getFileIcon() { |
| 17 | + if (!fileIcon) { |
| 18 | + fileIcon = await import("file-icon"); |
| 19 | + } |
| 20 | + return fileIcon; |
| 21 | +} |
| 22 | + |
| 23 | +// Cache detected apps in memory (cache the promise to prevent concurrent detections) |
| 24 | +let cachedApps: DetectedApplication[] | null = null; |
| 25 | +let detectionPromise: Promise<DetectedApplication[]> | null = null; |
| 26 | + |
| 27 | +const APP_PATHS: Record<string, string> = { |
| 28 | + vscode: "/Applications/Visual Studio Code.app", |
| 29 | + cursor: "/Applications/Cursor.app", |
| 30 | + sublime: "/Applications/Sublime Text.app", |
| 31 | + webstorm: "/Applications/WebStorm.app", |
| 32 | + intellij: "/Applications/IntelliJ IDEA.app", |
| 33 | + zed: "/Applications/Zed.app", |
| 34 | + pycharm: "/Applications/PyCharm.app", |
| 35 | + iterm: "/Applications/iTerm.app", |
| 36 | + warp: "/Applications/Warp.app", |
| 37 | + terminal: "/System/Applications/Utilities/Terminal.app", |
| 38 | + alacritty: "/Applications/Alacritty.app", |
| 39 | + kitty: "/Applications/kitty.app", |
| 40 | + ghostty: "/Applications/Ghostty.app", |
| 41 | + finder: "/System/Library/CoreServices/Finder.app", |
| 42 | +}; |
| 43 | + |
| 44 | +const DISPLAY_NAMES: Record<string, string> = { |
| 45 | + vscode: "VS Code", |
| 46 | + cursor: "Cursor", |
| 47 | + sublime: "Sublime Text", |
| 48 | + webstorm: "WebStorm", |
| 49 | + intellij: "IntelliJ IDEA", |
| 50 | + zed: "Zed", |
| 51 | + pycharm: "PyCharm", |
| 52 | + iterm: "iTerm", |
| 53 | + warp: "Warp", |
| 54 | + terminal: "Terminal", |
| 55 | + alacritty: "Alacritty", |
| 56 | + kitty: "Kitty", |
| 57 | + ghostty: "Ghostty", |
| 58 | + finder: "Finder", |
| 59 | +}; |
| 60 | + |
| 61 | +function getStorePath(): string { |
| 62 | + const userDataPath = app.getPath("userData"); |
| 63 | + if (userDataPath.includes("@posthog")) { |
| 64 | + const path = require("node:path"); |
| 65 | + return path.join(path.dirname(userDataPath), "Array"); |
| 66 | + } |
| 67 | + return userDataPath; |
| 68 | +} |
| 69 | + |
| 70 | +interface ExternalAppsSchema { |
| 71 | + externalAppsPrefs: ExternalAppsPreferences; |
| 72 | +} |
| 73 | + |
| 74 | +export const externalAppsStore = new Store<ExternalAppsSchema>({ |
| 75 | + name: "external-apps", |
| 76 | + cwd: getStorePath(), |
| 77 | + defaults: { |
| 78 | + externalAppsPrefs: {}, |
| 79 | + }, |
| 80 | +}); |
| 81 | + |
| 82 | +async function extractIcon(appPath: string): Promise<string | undefined> { |
| 83 | + try { |
| 84 | + const fileIconModule = await getFileIcon(); |
| 85 | + const uint8Array = await fileIconModule.fileIconToBuffer(appPath, { |
| 86 | + size: 64, |
| 87 | + }); |
| 88 | + const buffer = Buffer.from(uint8Array); |
| 89 | + const base64 = buffer.toString("base64"); |
| 90 | + return `data:image/png;base64,${base64}`; |
| 91 | + } catch (_error) { |
| 92 | + return undefined; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function generateCommand(appPath: string): string { |
| 97 | + return `open -a "${appPath}"`; |
| 98 | +} |
| 99 | + |
| 100 | +function getDisplayName(id: string): string { |
| 101 | + return DISPLAY_NAMES[id] || id; |
| 102 | +} |
| 103 | + |
| 104 | +async function checkApplication( |
| 105 | + id: string, |
| 106 | + appPath: string, |
| 107 | + type: ExternalAppType, |
| 108 | +): Promise<DetectedApplication | null> { |
| 109 | + try { |
| 110 | + await fs.access(appPath); |
| 111 | + |
| 112 | + const icon = await extractIcon(appPath); |
| 113 | + const command = generateCommand(appPath); |
| 114 | + const name = getDisplayName(id); |
| 115 | + |
| 116 | + return { |
| 117 | + id, |
| 118 | + name, |
| 119 | + type, |
| 120 | + path: appPath, |
| 121 | + command, |
| 122 | + icon, |
| 123 | + }; |
| 124 | + } catch { |
| 125 | + return null; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +async function detectExternalApps(): Promise<DetectedApplication[]> { |
| 130 | + const apps: DetectedApplication[] = []; |
| 131 | + |
| 132 | + for (const [id, appPath] of Object.entries(APP_PATHS)) { |
| 133 | + const detected = await checkApplication(id, appPath, "editor"); |
| 134 | + if (detected) { |
| 135 | + apps.push(detected); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return apps; |
| 140 | +} |
| 141 | + |
| 142 | +export async function getOrRefreshApps(): Promise<DetectedApplication[]> { |
| 143 | + if (cachedApps) { |
| 144 | + return cachedApps; |
| 145 | + } |
| 146 | + |
| 147 | + if (detectionPromise) { |
| 148 | + return detectionPromise; |
| 149 | + } |
| 150 | + |
| 151 | + detectionPromise = detectExternalApps().then((apps) => { |
| 152 | + cachedApps = apps; |
| 153 | + detectionPromise = null; |
| 154 | + return apps; |
| 155 | + }); |
| 156 | + |
| 157 | + return detectionPromise; |
| 158 | +} |
| 159 | + |
| 160 | +export function registerExternalAppsIpc(): void { |
| 161 | + ipcMain.handle( |
| 162 | + "external-apps:get-detected-apps", |
| 163 | + async (): Promise<DetectedApplication[]> => { |
| 164 | + return await getOrRefreshApps(); |
| 165 | + }, |
| 166 | + ); |
| 167 | + |
| 168 | + ipcMain.handle( |
| 169 | + "external-apps:open-in-app", |
| 170 | + async ( |
| 171 | + _event, |
| 172 | + appId: string, |
| 173 | + targetPath: string, |
| 174 | + ): Promise<{ success: boolean; error?: string }> => { |
| 175 | + try { |
| 176 | + const apps = await getOrRefreshApps(); |
| 177 | + const appToOpen = apps.find((a) => a.id === appId); |
| 178 | + |
| 179 | + if (!appToOpen) { |
| 180 | + return { success: false, error: "Application not found" }; |
| 181 | + } |
| 182 | + |
| 183 | + let command: string; |
| 184 | + if (appToOpen.command.includes("open -a")) { |
| 185 | + command = `${appToOpen.command} "${targetPath}"`; |
| 186 | + } else { |
| 187 | + command = `${appToOpen.command} "${targetPath}"`; |
| 188 | + } |
| 189 | + |
| 190 | + await execAsync(command); |
| 191 | + return { success: true }; |
| 192 | + } catch (error) { |
| 193 | + return { |
| 194 | + success: false, |
| 195 | + error: error instanceof Error ? error.message : "Unknown error", |
| 196 | + }; |
| 197 | + } |
| 198 | + }, |
| 199 | + ); |
| 200 | + |
| 201 | + ipcMain.handle( |
| 202 | + "external-apps:set-last-used", |
| 203 | + async (_event, appId: string): Promise<void> => { |
| 204 | + const prefs = externalAppsStore.get("externalAppsPrefs"); |
| 205 | + externalAppsStore.set("externalAppsPrefs", { |
| 206 | + ...prefs, |
| 207 | + lastUsedApp: appId, |
| 208 | + }); |
| 209 | + }, |
| 210 | + ); |
| 211 | + |
| 212 | + ipcMain.handle( |
| 213 | + "external-apps:get-last-used", |
| 214 | + async (): Promise<{ |
| 215 | + lastUsedApp?: string; |
| 216 | + }> => { |
| 217 | + const prefs = externalAppsStore.get("externalAppsPrefs"); |
| 218 | + return { |
| 219 | + lastUsedApp: prefs.lastUsedApp, |
| 220 | + }; |
| 221 | + }, |
| 222 | + ); |
| 223 | + |
| 224 | + ipcMain.handle( |
| 225 | + "external-apps:copy-path", |
| 226 | + async (_event, targetPath: string): Promise<void> => { |
| 227 | + const { clipboard } = await import("electron"); |
| 228 | + clipboard.writeText(targetPath); |
| 229 | + }, |
| 230 | + ); |
| 231 | +} |
0 commit comments