0.7.0 • Published 6 years ago

child-process-data v0.7.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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.

import childProcessData from 'child-process-data';
import {spawn} from 'child_process';

const proc1 = spawn('echo', ['Lorem ipsum']); // Spawns a child process
const proc2 = spawn('echo', ['dolor sit amet']); // Spawns another child process

childProcessData(proc1).then(res => { // Recovers output from proc1
  res.all(); // Returns 'Lorem ipsum\n'
});
childProcessData(proc2).then(res => { // Recovers output from proc2
  res.all(); // Returns 'dolor sit amet\n'
});

Options

As a second argument, childProcessData can take a literal object with the following keys:

  • silent: if true, 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:

process.stdout.write('lorem\n');
process.stdout.write('ipsum\n');
process.stderr.write('dolor\n');
process.stdout.write('sit\n');
process.stderr.write('amet\n');

We can recover all outputs:

childProcessData(spawn('node', ['./test/examples/normal-exit.js'])).then(res => {
  res.outMessages[0] === 'lorem\n'; // true
  res.outMessages[1] === 'ipsum\n'; // true
  res.outMessages[2] === 'sit\n'; // true
  res.errMessages[0] === 'dolor\n'; // true
  res.errMessages[1] === 'amet\n'; // true
  res.allMessages[0] === 'lorem\n'; // true
  res.allMessages[1] === 'ipsum\n'; // true
  res.allMessages[2] === 'dolor\n'; // true
  res.allMessages[3] === 'sit\n'; // true
  res.allMessages[4] === 'amet\n'; // true
});

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.

childProcessData(spawn('node', ['./test/examples/normal-exit.js'])).then(res => {
  res.out() === 'lorem\nipsum\nsit\n'; // true
  res.err() === 'dolor\namet\n'; // true
  res.all() === 'lorem\nipsum\ndolor\nsit\namet\n'; // true
});

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:

childProcessData(spawn('node', ['./test/examples/error-exit.js'])).catch(err => {
  let res = err.result;

  res.outMessages[0] === 'lorem\n'; // true
  res.outMessages[1] === 'ipsum\n'; // true
  res.outMessages[2] === 'sit\n'; // true
  res.errMessages[0] === 'dolor\n'; // true
  res.errMessages[1]).match(/Error:.*amet/); // Non empty array
  res.allMessages[0] === 'lorem\n'; // true
  res.allMessages[1] === 'ipsum\n'; // true
  res.allMessages[2] === 'dolor\n'; // true
  res.allMessages[3] === 'sit\n'; // true
  res.allMessages[4]).match(/Error:.*amet/); // Non empty array

  res.out() === 'lorem\nipsum\nsit\n'; // true     
  res.err() === 'dolor\n' + res.errMessages[1]; // true
  res.all() === 'lorem\nipsum\ndolor\nsit\n' + res.allMessages[4]; // true
});

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

import {makeSingleTest} from 'child-process-data';
import {expect} from 'chai';

describe('One test suite', function () {
  it('One test', function () {
    const test = makeSingleTest({
      childProcess: ['echo', ['Hello world']],

      checkResults (results) {
        expect(results.out()).to.equal('Hello world\n');
      },
    });

    return test();
  });
});

makeIOTest

import {makeIOTest} from 'child-process-data';

describe('One test suite', function () {
  it('One test', function () {
    const test = makeSingleTest({
      childProcessFile: 'build/src/do-this.js',

      messages: [
        {io: ['Did you do this?', 'Yes\n']},
        {io: ['And that?', 'Not yet\n']},
      ]
    });

    return test();
  });
});

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)): Returns true if passes for all messages.
  • test(arrayOfFns): Returns true if passes for all function and all messages.
  • testUpTo(msg => fn(msg), msg0, {included}): Returns true if passes for all messages up to msg0, included or not.
  • testUpTo(arrayOfFns, msg0, {included}): Returns true if passes for all functions and all messages up to msg0, included or not.
  • forget(): In long processes where results is continuously updated, forget all previous messages.
  • forgetUpTo(msg0, {included}): In long processes where results is continuously updated, forget all previous messages up to msg0, included or not.

License

child-process-data is MIT licensed.

© 2016-2018 Jason Lenoble

0.7.0

6 years ago

0.6.6

6 years ago

0.6.5

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.7

6 years ago

0.5.6

6 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

8 years ago