package/package.json000644 000765 000024 0000002045 12617210435013017 0ustar00000000 000000 { "name": "capture-stream", "description": "Capture stream output.", "version": "0.1.2", "homepage": "https://github.com/doowb/capture-stream", "author": "Brian Woodward (https://github.com/doowb)", "repository": "doowb/capture-stream", "bugs": { "url": "https://github.com/doowb/capture-stream/issues" }, "license": "MIT", "files": [ "index.js" ], "main": "index.js", "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha" }, "devDependencies": { "mocha": "^2.3.3" }, "verb": { "related": { "list": [ "composer-runtimes", "composer-errors" ] } }, "keywords": [ "capture", "process", "stderr", "stdout", "stream", "test", "testing", "tests", "unit" ], "contributors": [ { "name": "Brian Woodward", "email": "brian.woodward@gmail.com", "url": "https://github.com/doowb" }, { "name": "Aparajita Fishman", "email": "", "url": "https://github.com/aparajita" } ] } package/README.md000644 000765 000024 0000005516 12617210423012013 0ustar00000000 000000 # capture-stream [![NPM version](https://badge.fury.io/js/capture-stream.svg)](http://badge.fury.io/js/capture-stream) [![Build Status](https://travis-ci.org/doowb/capture-stream.svg)](https://travis-ci.org/doowb/capture-stream) > Capture stream output. Install with [npm](https://www.npmjs.com/) ```sh $ npm i capture-stream --save ``` ## Usage ```js 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. ```js 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. ```js 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 ### [captureStream](index.js#L27) 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** ```js 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 * [composer-errors](https://www.npmjs.com/package/composer-errors): Listen for and output Composer errors. | [homepage](https://github.com/doowb/composer-errors) * [composer-runtimes](https://www.npmjs.com/package/composer-runtimes): Write composer task start and end times to a stream. | [homepage](https://github.com/doowb/composer-runtimes) ## Running tests Install dev dependencies: ```sh $ npm i -d && npm test ``` ## Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/capture-stream/issues/new). ## Author **Brian Woodward** + [github/doowb](https://github.com/doowb) + [twitter/doowb](http://twitter.com/doowb) ## License Copyright © 2015 Brian Woodward Released under the MIT license. *** _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 28, 2015._package/LICENSE000644 000765 000024 0000002073 12600516376011544 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2015, Brian Woodward. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 000765 000024 0000001716 12617210305012176 0ustar00000000 000000 /*! * capture-stream * * Copyright (c) 2015, Brian Woodward. * Licensed under the MIT License. */ 'use strict'; /** * Capture the output from a stream and store later. * * ```js * 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' ] ] * ``` * @param {Stream} `stream` A stream to capture output from (e.g. `process.stdout`, `process.stderr`) * @return {Function} `restore` function that restores normal output and returns an array of output. * @api public */ module.exports = function captureStream(stream) { var output = []; var write = stream.write; stream.write = function() { output.push([].slice.call(arguments)); }; return function restore(wantString) { stream.write = write; return wantString ? output.join('') : output; }; };