(piPath: string, cwd: string)
| 51 | private _alive = false; |
| 52 | |
| 53 | async spawn(piPath: string, cwd: string): Promise<void> { |
| 54 | const commandPath = resolveWindowsCommandShim(piPath); |
| 55 | const command = |
| 56 | buildWindowsCommandScriptSpawnCommand(commandPath, ["--mode", "rpc"]) ?? [ |
| 57 | commandPath, |
| 58 | "--mode", |
| 59 | "rpc", |
| 60 | ]; |
| 61 | let proc: ChildProcess; |
| 62 | try { |
| 63 | const [file, ...args] = command; |
| 64 | proc = spawn(file, args, { |
| 65 | cwd, |
| 66 | stdio: ["pipe", "pipe", "pipe"], |
| 67 | }); |
| 68 | } catch (err) { |
| 69 | const error = err instanceof Error ? err : new Error(String(err)); |
| 70 | this.handleProcessEnd(error); |
| 71 | throw error; |
| 72 | } |
| 73 | |
| 74 | this.proc = proc; |
| 75 | proc.once("exit", () => { |
| 76 | this.handleProcessEnd(new Error("Pi process exited unexpectedly")); |
| 77 | }); |
| 78 | |
| 79 | await new Promise<void>((resolve, reject) => { |
| 80 | const cleanup = () => { |
| 81 | proc.off("spawn", onSpawn); |
| 82 | proc.off("error", onError); |
| 83 | }; |
| 84 | const onSpawn = () => { |
| 85 | cleanup(); |
| 86 | this._alive = true; |
| 87 | this.readStream(); |
| 88 | resolve(); |
| 89 | }; |
| 90 | const onError = (err: Error) => { |
| 91 | cleanup(); |
| 92 | this.handleProcessEnd(err); |
| 93 | reject(err); |
| 94 | }; |
| 95 | |
| 96 | proc.once("spawn", onSpawn); |
| 97 | proc.once("error", onError); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | private handleProcessEnd(error: Error): void { |
| 102 | if (!this.proc && this.pendingRequests.size === 0) return; |
no test coverage detected