child-process-data
A helper to capture data streams from child processes
Simple usage
Child processes output messages through their stdout and stderr. They get interleaved with messages from other child processes and messages from the parent process. child-process-data is a Node.js module that helps disentangle and recover these messages.
;; const proc1 = ; // Spawns a child processconst proc2 = ; // Spawns another child process ;;
Options
As a second argument, childProcessData
can take a literal object with the following keys:
silent
: iftrue
,childProcessData
will capture but not print the child process logging messages.
Accessing individual messages
To access the output of a child process that exited without errors, you use the then
channel of the promise returned by childProcessData
.
Messages output on stdout are contained in array property outMessages
. Those on stderr are contained in array property errMessages
. They all are contained in order in array property allMessages
.
Using file normal-exit.js:
processstdout;processstdout;processstderr;processstdout;processstderr;
We can recover all outputs:
;
Accessing all messages
As a conveniency, you can recover messages of a child process that exited without errors as a whole. Methods out
, err
and all
return a concatenation respectively of array properties outMessages
, errMessages
and allMessages
.
;
Accessing uncaught error messages
If the child process exits with a non-zero error code, then you recover the output messages using the catch
channel of the promise returned by childProcessData
. You can access all messages through the result
property of the returned error object (it is the object that would have been returned via the then
channel of the promise, had it not failed).
Using file error-exit.js:
;
Dealing with long lived processes
childProcessData
is tweaked for TDD with Gulp
. It keeps track of Starting and Finished messages and resolves even if the process keeps running. Therefore even after resolving, it keeps buffering the Gulp
TDD messages.
But if you use childProcessData
on another long running processes, to obtain the same behavior, you need to pass {dontBlock: true}
option or a {startDelay: numberInMs}
option. Then, after 1 ms or startDelay
ms, logs will be accessible.
interceptMessage
and resolveMessage
helpers
interceptMessage(proc, message, options)
helps capture messages. It returns a promise that resolves right away or after {startDelay}
. Thereafter resolveMessage(message)
returns a function returning a promise that resolves on seeing message
either on stdout or stderr.
A possible usage is when defining gulp tasks that launch servers. You can take advantage of their logs to start tasks that rely on their being ready without the need of maintaining some sockets open.
Test helpers
childProcessData
's primary intent was to help test Gulp
processes by catching their outputs, especially while developing Yeoman
generator generator-wupjs.
As such, some tests are pretty recurring, and two helpers around childProcessData
are provided to help build them fast: makeSingleTest
and
makeIOTest
.
makeSingleTest
;; ;
makeIOTest
; ;
Special extensions
The results
argument of the checkResults
function you provide has helper extensions to help you do some common testing.
test(msg => fn(msg))
: Returnstrue
if passes for all messages.test(arrayOfFns)
: Returnstrue
if passes for all function and all messages.testUpTo(msg => fn(msg), msg0, {included})
: Returnstrue
if passes for all messages up tomsg0
,included
or not.testUpTo(arrayOfFns, msg0, {included})
: Returnstrue
if passes for all functions and all messages up tomsg0
,included
or not.forget()
: In long processes whereresults
is continuously updated, forget all previous messages.forgetUpTo(msg0, {included})
: In long processes whereresults
is continuously updated, forget all previous messages up tomsg0
,included
or not.
License
child-process-data is MIT licensed.
© 2016-2018 Jason Lenoble