pipe.js
This file
Path
lib/pipe.js
42 lines · 1.2 KiB · JavaScript
Last changed by
Add `pipeStdout()`-related methods (#531)ehmickybefore adoption06287c8
History of this fileTake it
Add `pipeStdout()`-related methods (#531)ehmickybefore adoption06287c8
lines 1–42why, one step back1import {createWriteStream} from 'node:fs';2import {ChildProcess} from 'node:child_process';3import {isWritableStream} from 'is-stream';45const isExecaChildProcess = target => target instanceof ChildProcess && typeof target.then === 'function';67const pipeToTarget = (spawned, streamName, target) => {8 if (typeof target === 'string') {9 spawned[streamName].pipe(createWriteStream(target));10 return spawned;11 }1213 if (isWritableStream(target)) {14 spawned[streamName].pipe(target);15 return spawned;16 }1718 if (!isExecaChildProcess(target)) {19 throw new TypeError('The second argument must be a string, a stream or an Execa child process.');20 }2122 if (!isWritableStream(target.stdin)) {23 throw new TypeError('The target child process\'s stdin must be available.');24 }2526 spawned[streamName].pipe(target.stdin);27 return target;28};2930export const addPipeMethods = spawned => {31 if (spawned.stdout !== null) {32 spawned.pipeStdout = pipeToTarget.bind(undefined, spawned, 'stdout');33 }3435 if (spawned.stderr !== null) {36 spawned.pipeStderr = pipeToTarget.bind(undefined, spawned, 'stderr');37 }3839 if (spawned.all !== undefined) {40 spawned.pipeAll = pipeToTarget.bind(undefined, spawned, 'all');41 }42};