( file: string, args: string[], timeoutMs: number, env: NodeJS.ProcessEnv = process.env )
| 277 | } |
| 278 | |
| 279 | function execFileWithTimeout( |
| 280 | file: string, |
| 281 | args: string[], |
| 282 | timeoutMs: number, |
| 283 | env: NodeJS.ProcessEnv = process.env |
| 284 | ): Promise<{ stdout: string; error: Error | null }> { |
| 285 | return new Promise((resolve) => { |
| 286 | const child = spawn(file, args, { |
| 287 | env, |
| 288 | stdio: [`ignore`, `pipe`, `pipe`], |
| 289 | }) |
| 290 | let stdout = `` |
| 291 | let stderr = `` |
| 292 | const timeout = setTimeout(() => { |
| 293 | child.kill() |
| 294 | resolve({ |
| 295 | stdout, |
| 296 | error: new Error(`Timed out while checking Electric CLI version.`), |
| 297 | }) |
| 298 | }, timeoutMs) |
| 299 | child.stdout?.setEncoding(`utf8`) |
| 300 | child.stderr?.setEncoding(`utf8`) |
| 301 | child.stdout?.on(`data`, (chunk: string) => { |
| 302 | stdout += chunk |
| 303 | }) |
| 304 | child.stderr?.on(`data`, (chunk: string) => { |
| 305 | stderr += chunk |
| 306 | }) |
| 307 | child.on(`error`, (error) => { |
| 308 | clearTimeout(timeout) |
| 309 | resolve({ stdout, error }) |
| 310 | }) |
| 311 | child.on(`close`, (code) => { |
| 312 | clearTimeout(timeout) |
| 313 | if (code === 0) { |
| 314 | resolve({ stdout, error: null }) |
| 315 | return |
| 316 | } |
| 317 | resolve({ |
| 318 | stdout, |
| 319 | error: new Error( |
| 320 | stderr.trim() || `Electric CLI exited with code ${code}.` |
| 321 | ), |
| 322 | }) |
| 323 | }) |
| 324 | }) |
| 325 | } |
no test coverage detected