|
1 | 1 | import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; |
2 | 2 | import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
3 | | -import { exec } from "node:child_process"; |
4 | 3 | import { join } from "node:path"; |
5 | 4 | import { homedir } from "node:os"; |
| 5 | +import { openUrl } from "./openUrl.js"; |
6 | 6 |
|
7 | 7 | const CREDENTIALS_DIR = join(homedir(), ".supermemory-opencode"); |
8 | 8 | const CREDENTIALS_FILE = join(CREDENTIALS_DIR, "credentials.json"); |
@@ -40,21 +40,6 @@ export function clearCredentials(): boolean { |
40 | 40 | return true; |
41 | 41 | } |
42 | 42 |
|
43 | | -function openBrowser(url: string): void { |
44 | | - const platform = process.platform; |
45 | | - |
46 | | - const commands: Record<string, string> = { |
47 | | - darwin: `open "${url}"`, |
48 | | - win32: `start "" "${url}"`, |
49 | | - linux: `xdg-open "${url}"`, |
50 | | - }; |
51 | | - |
52 | | - const cmd = commands[platform] ?? `xdg-open "${url}"`; |
53 | | - exec(cmd, (err) => { |
54 | | - if (err) console.error("Failed to open browser:", err.message); |
55 | | - }); |
56 | | -} |
57 | | - |
58 | 43 | export interface AuthResult { |
59 | 44 | success: boolean; |
60 | 45 | apiKey?: string; |
@@ -129,7 +114,13 @@ export function startAuthFlow(timeoutMs = 120000): Promise<AuthResult> { |
129 | 114 |
|
130 | 115 | console.log("Opening browser for authentication..."); |
131 | 116 | console.log(`If it doesn't open, visit: ${authUrl}`); |
132 | | - openBrowser(authUrl); |
| 117 | + openUrl(authUrl).catch((error) => { |
| 118 | + if (!resolved) { |
| 119 | + resolved = true; |
| 120 | + server.close(); |
| 121 | + resolve({ success: false, error: `Failed to open browser: ${error.message}` }); |
| 122 | + } |
| 123 | + }); |
133 | 124 | }); |
134 | 125 |
|
135 | 126 | setTimeout(() => { |
|
0 commit comments