版博士V2.0程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

43 рядки
1.2 KiB

  1. import {createWriteStream} from 'node:fs';
  2. import {ChildProcess} from 'node:child_process';
  3. import {isWritableStream} from 'is-stream';
  4. const isExecaChildProcess = target => target instanceof ChildProcess && typeof target.then === 'function';
  5. const pipeToTarget = (spawned, streamName, target) => {
  6. if (typeof target === 'string') {
  7. spawned[streamName].pipe(createWriteStream(target));
  8. return spawned;
  9. }
  10. if (isWritableStream(target)) {
  11. spawned[streamName].pipe(target);
  12. return spawned;
  13. }
  14. if (!isExecaChildProcess(target)) {
  15. throw new TypeError('The second argument must be a string, a stream or an Execa child process.');
  16. }
  17. if (!isWritableStream(target.stdin)) {
  18. throw new TypeError('The target child process\'s stdin must be available.');
  19. }
  20. spawned[streamName].pipe(target.stdin);
  21. return target;
  22. };
  23. export const addPipeMethods = spawned => {
  24. if (spawned.stdout !== null) {
  25. spawned.pipeStdout = pipeToTarget.bind(undefined, spawned, 'stdout');
  26. }
  27. if (spawned.stderr !== null) {
  28. spawned.pipeStderr = pipeToTarget.bind(undefined, spawned, 'stderr');
  29. }
  30. if (spawned.all !== undefined) {
  31. spawned.pipeAll = pipeToTarget.bind(undefined, spawned, 'all');
  32. }
  33. };