(command: string, args?: ReadonlyArray<string>, options?: cp.CommonOptions, onDisposed?: () => unknown)
| 515 | } |
| 516 | |
| 517 | export async function spawnAsync(command: string, args?: ReadonlyArray<string>, options?: cp.CommonOptions, onDisposed?: () => unknown): Promise<cp.SpawnSyncReturns<string>> { |
| 518 | return new Promise((resolve) => { |
| 519 | |
| 520 | const result: cp.SpawnSyncReturns<string> = { |
| 521 | error: undefined, |
| 522 | pid: -1, |
| 523 | output: [], |
| 524 | stdout: '', |
| 525 | stderr: '', |
| 526 | status: null, |
| 527 | signal: null |
| 528 | }; |
| 529 | |
| 530 | try { |
| 531 | const childProcess = spawn(command, args, options, onDisposed); |
| 532 | if (childProcess.pid !== undefined) { |
| 533 | result.pid = childProcess.pid; |
| 534 | } |
| 535 | childProcess.stdout?.on('data', (chunk: Buffer) => { |
| 536 | result.stdout += chunk.toString(); |
| 537 | }); |
| 538 | childProcess.stderr?.on('data', (chunk: Buffer) => { |
| 539 | result.stderr += chunk.toString(); |
| 540 | }); |
| 541 | childProcess.on('error', (err: Error) => { |
| 542 | result.error = err; |
| 543 | }); |
| 544 | childProcess.on('exit', (code, signal) => { |
| 545 | result.status = code; |
| 546 | result.signal = signal; |
| 547 | resolve(result); |
| 548 | }); |
| 549 | } catch (e) { |
| 550 | result.error = (e instanceof Error) ? e : undefined; |
| 551 | resolve(result); |
| 552 | } |
| 553 | }); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Check if an R package is available or not |
no test coverage detected