| 210 | * Waits for a pattern in the output |
| 211 | */ |
| 212 | export function waitForPattern( |
| 213 | proc: Subprocess, |
| 214 | pattern: RegExp | string, |
| 215 | timeout: number = 5000, |
| 216 | ): Promise<string> { |
| 217 | return new Promise((resolve, reject) => { |
| 218 | let output = ""; |
| 219 | const timer = setTimeout(() => { |
| 220 | proc.kill(); |
| 221 | reject(new Error(`Timeout waiting for pattern: ${pattern}`)); |
| 222 | }, timeout); |
| 223 | |
| 224 | const checkOutput = (data: string) => { |
| 225 | output += data; |
| 226 | const match = |
| 227 | typeof pattern === "string" |
| 228 | ? output.includes(pattern) |
| 229 | : pattern.test(output); |
| 230 | |
| 231 | if (match) { |
| 232 | clearTimeout(timer); |
| 233 | resolve(output); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | proc.stdout?.on("data", checkOutput); |
| 238 | proc.stderr?.on("data", checkOutput); |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Simulates user input in interactive mode |