You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
687 B

  1. import * as childProcess from 'child_process';
  2. export const spawn = (
  3. cwd: string,
  4. command: string,
  5. args: string[],
  6. parentProcess = process,
  7. ) => new Promise<string>((resolve, reject) => {
  8. let stdout = '';
  9. let stderr = '';
  10. const theChildProcess = childProcess.spawn(command, args, {
  11. cwd,
  12. });
  13. theChildProcess.stdout.on('data', (data) => {
  14. parentProcess.stdout.write(data);
  15. stdout += data;
  16. });
  17. theChildProcess.stderr.on('data', (data) => {
  18. parentProcess.stderr.write(data);
  19. stderr += data;
  20. });
  21. theChildProcess.on('close', (code) => {
  22. if (code !== 0) {
  23. reject(new Error(stderr));
  24. return;
  25. }
  26. resolve(stdout);
  27. })
  28. })