(host: HostConnection)
| 348 | } |
| 349 | |
| 350 | async function ensureRuntimeHost(host: HostConnection) { |
| 351 | if (runtimeHostsShuttingDown) { |
| 352 | throw new Error('Pi runtime host is shutting down.') |
| 353 | } |
| 354 | |
| 355 | registerHostShutdownHandlers() |
| 356 | if (host.process && !host.process.killed && host.process.exitCode === null) { |
| 357 | clearHostIdleTimer(host) |
| 358 | return host.process |
| 359 | } |
| 360 | |
| 361 | if (host.startPromise) { |
| 362 | return host.startPromise |
| 363 | } |
| 364 | |
| 365 | clearHostIdleTimer(host) |
| 366 | host.startPromise = (async () => { |
| 367 | const nodeExecutable = await getNodeExecutable() |
| 368 | if (runtimeHostsShuttingDown) { |
| 369 | throw new Error('Pi runtime host is shutting down.') |
| 370 | } |
| 371 | |
| 372 | return await new Promise<ChildProcess>((resolve, reject) => { |
| 373 | const child = spawn(nodeExecutable, [getRuntimeHostPath()], { |
| 374 | cwd: getDesktopWorkingDirectory(), |
| 375 | env: { |
| 376 | ...process.env, |
| 377 | HOWCODE_REPO_ROOT: getDesktopWorkingDirectory(), |
| 378 | HOWCODE_ELECTRON_RESOURCES_PATH: getElectronResourcesPath(), |
| 379 | HOWCODE_BUNDLED_SKILLS_PATH: getBundledSkillsPath(), |
| 380 | }, |
| 381 | stdio: ['ignore', 'pipe', 'pipe', 'ipc'], |
| 382 | }) as ChildProcess |
| 383 | |
| 384 | let settled = false |
| 385 | const settleFailure = (error: Error) => { |
| 386 | if (settled) { |
| 387 | return |
| 388 | } |
| 389 | settled = true |
| 390 | host.startPromise = null |
| 391 | host.process = null |
| 392 | if (host.role === 'thread') { |
| 393 | forgetHost(host) |
| 394 | } |
| 395 | reject(error) |
| 396 | } |
| 397 | |
| 398 | child.once('spawn', () => { |
| 399 | if (runtimeHostsShuttingDown) { |
| 400 | terminateHostProcess(child) |
| 401 | settleFailure(new Error('Pi runtime host is shutting down.')) |
| 402 | return |
| 403 | } |
| 404 | |
| 405 | settled = true |
| 406 | host.process = child |
| 407 | host.startPromise = null |
no test coverage detected