(port: number = DEFAULT_PORT)
| 384 | } |
| 385 | |
| 386 | export async function connectToDaemon(port: number = DEFAULT_PORT): Promise<DaemonConnection> { |
| 387 | // Only set up adb forwarding for mobile ports — standalone macOS apps listen |
| 388 | // directly on localhost and adb forward would shadow them. |
| 389 | if (port !== STANDALONE_PORT) { |
| 390 | await tryAdbForward(port); |
| 391 | } |
| 392 | return new Promise((resolve, reject) => { |
| 393 | const socket = net.createConnection({ port, host: '127.0.0.1' }); |
| 394 | |
| 395 | const timer = setTimeout(() => { |
| 396 | socket.destroy(); |
| 397 | reject(new Error(`Timeout connecting to Valdi daemon on port ${port}. Is the hot-reloader running?`)); |
| 398 | }, 5_000); |
| 399 | |
| 400 | socket.once('connect', () => { |
| 401 | clearTimeout(timer); |
| 402 | resolve(new DaemonConnection(socket)); |
| 403 | }); |
| 404 | |
| 405 | socket.once('error', (err: NodeJS.ErrnoException) => { |
| 406 | clearTimeout(timer); |
| 407 | if (err.code === 'ECONNREFUSED') { |
| 408 | reject(new CliError( |
| 409 | `Valdi daemon not running on port ${port}.\n` + |
| 410 | `Start the hot-reloader (valdi hotreload) or pass --port to use a different port.`, |
| 411 | )); |
| 412 | } else { |
| 413 | reject(err); |
| 414 | } |
| 415 | }); |
| 416 | }); |
| 417 | } |
| 418 | |
| 419 | // ─── Device / context selection helpers ────────────────────────────────────── |
| 420 |
no test coverage detected