Code Monkey home page Code Monkey logo

capture-stream's Introduction

capture-stream NPM version Build Status

Capture stream output.

Install

Install with npm:

$ npm install capture-stream --save

Usage

var capture = require('capture-stream');
var restore = capture(process.stdout);

console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore();
console.log(output);
//=> [ [ 'Hello, world!!!\n' ], [ 'foo bar\n' ] ]

Pass true to restore to return a string instead of an array of output.

var capture = require('capture-stream');
var restore = capture(process.stdout);

console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore(true);
console.log(output);
//=> Hello, world!!!
//=> foo bar
//=>

This module has been built to be used in unit tests to easily capture output from process.stdout and process.stderr and test the results.

describe('awesome module', function () {
  function log () {
    console.log.apply(console, arguments);
  }

  it('should write "Hello, world!!!" to stdout', function () {
    var restore = capture(process.stdout);
    log('Hello, world!!!');
    var output = restore();
    assert.equal(output.length, 1);
    assert(output[0][0].indexOf('Hello, world!!!') === 0);
  });
});

API

Capture the output from a stream and store later.

Params

  • stream {Stream}: A stream to capture output from (e.g. process.stdout, process.stderr)
  • returns {Function} restore: function that restores normal output and returns an array of output.

Example

var restore = capture(process.stdout);
console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore();
console.log(output);
//=> [ [ 'Hello, world!!!\n' ], [ 'foo bar\n' ] ]

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with [verb][]:

$ npm install verb && npm run docs

Or, if [verb][] is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Brian Woodward

License

Copyright © 2016 Brian Woodward Released under the MIT license.


This file was generated by verb, v0.9.0, on March 19, 2016.

capture-stream's People

Contributors

aparajita avatar doowb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sreekanth370

capture-stream's Issues

Not working when spawn stdio: 'inherit'?

Should it work?

var spawn = require('cross-spawn')
var capture = require('capture-stream')

var stream = spawn('npm', ['show verb'], {stdio: 'inherit'})
var restore = capture(stream)
console.log(restore()) // => []

Or I'm missing something? I it would be great. I found http://npm.im/superspawn, but it's ugly and too big and "reinventing the wheel". I also have done some capturing in the past with async-exec-cmd, so I can think for something and can do a PR. I'm starting to work on this now, because I need it.

Missing tags

This git repo is missing tags, please add them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.