pax_global_header00006660000000000000000000000064126451651340014521gustar00rootroot0000000000000052 comment=e586fa81f25cf22a0c3faa3f306b7d45a2832932 hook-std-0.2.0/000077500000000000000000000000001264516513400132505ustar00rootroot00000000000000hook-std-0.2.0/.editorconfig000066400000000000000000000003471264516513400157310ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false hook-std-0.2.0/.gitattributes000066400000000000000000000000141264516513400161360ustar00rootroot00000000000000* text=auto hook-std-0.2.0/.gitignore000066400000000000000000000000151264516513400152340ustar00rootroot00000000000000node_modules hook-std-0.2.0/.travis.yml000066400000000000000000000000761264516513400153640ustar00rootroot00000000000000language: node_js node_js: - 'stable' - '0.12' - '0.10' hook-std-0.2.0/index.js000066400000000000000000000013761264516513400147240ustar00rootroot00000000000000'use strict'; function hook(type, opts, cb) { if (typeof opts !== 'object') { cb = opts; opts = {}; } var std = process[type]; var write = std.write; std.write = function (str, enc, cb2) { var cbRet = cb(str, enc); if (opts.silent) { return typeof cbRet === 'boolean' ? cbRet : true; } var ret = Buffer.isBuffer(cbRet) || typeof cbRet === 'string' ? cbRet : str; return write.call(std, ret, enc, cb2); }; return function () { std.write = write; }; } var x = module.exports = function (opts, cb) { var unhookStdout = hook('stdout', opts, cb); var unhookStderr = hook('stderr', opts, cb); return function () { unhookStdout(); unhookStderr(); }; }; x.stdout = hook.bind(null, 'stdout'); x.stderr = hook.bind(null, 'stderr'); hook-std-0.2.0/license000066400000000000000000000021371264516513400146200ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) 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. hook-std-0.2.0/package.json000066400000000000000000000012451264516513400155400ustar00rootroot00000000000000{ "name": "hook-std", "version": "0.2.0", "description": "Hook and modify stdout/stderr", "license": "MIT", "repository": "sindresorhus/hook-std", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "hook", "intercept", "capture", "modify", "change", "test", "assert", "check", "std", "stdio", "stdout", "stderr", "console", "log", "process" ], "devDependencies": { "ava": "*", "xo": "*" } } hook-std-0.2.0/readme.md000066400000000000000000000021571264516513400150340ustar00rootroot00000000000000# hook-std [![Build Status](https://travis-ci.org/sindresorhus/hook-std.svg?branch=master)](https://travis-ci.org/sindresorhus/hook-std) > Hook and modify stdout/stderr ## Install ``` $ npm install --save hook-std ``` ## Usage ```js const assert = require('assert'); const hookStd = require('hook-std'); const unhook = hookStd.stdout(output => { unhook(); assert.strictEqual(output.trim(), 'unicorn'); }); console.log('unicorn'); ``` ## API ### hookStd([options], callback) Hook stdout and stderr. Returns a function that unhooks stdout and stderr. ### hookStd.stdout([options], callback) Hook stdout. Returns a function that unhooks stdout. ### hookStd.stderr([options], callback) Hook stderr. Returns a function that unhooks stderr. #### options ##### silent Type: `boolean` Default: `false` Suppress stdout/stderr output. ##### callback Type: `function` Receives stdout/stderr as the first argument. Return a string to modify it. Optionally, when in silent mode, you may return a `boolean` to influence the return value of `.write(...)`. ## License MIT © [Sindre Sorhus](http://sindresorhus.com) hook-std-0.2.0/test.js000066400000000000000000000055651264516513400146000ustar00rootroot00000000000000import test from 'ava'; import fn from './'; const stdout = process.stdout; const stderr = process.stderr; function restore() { // This craziness is required because these properties only have getters by default. Object.defineProperties(process, { stdout: { configurable: true, writable: true, value: stdout }, stderr: { configurable: true, writable: true, value: stderr } }); } test.beforeEach(restore); test.afterEach(restore); test.serial.cb('hook stdout & stderr', t => { t.plan(2); let i = 0; const unhook = fn({silent: true}, str => { if (str === 'foo' || str === 'bar') { t.pass(); } if (++i === 2) { unhook(); t.end(); } }); process.stdout.write('foo'); process.stderr.write('bar'); }); test.serial.cb('hook stdout', t => { t.plan(1); const unhook = fn.stdout({silent: true}, str => { t.is(str, 'foo'); unhook(); t.end(); }); process.stdout.write('foo'); }); test.serial.cb('hook stderr', t => { t.plan(1); const unhook = fn.stderr({silent: true}, str => { t.is(str, 'foo'); unhook(); t.end(); }); process.stderr.write('foo'); }); function loggingWrite(log, retVal) { return (...items) => { while (items[items.length - 1] === undefined) { items.pop(); } log.push(items); return retVal(); }; } test.serial('passes through the return value of the underlying write call', t => { const log = []; let returnValue = false; process.stdout = { write: loggingWrite(log, () => returnValue) }; fn.stdout(str => str); t.false(process.stdout.write('foo')); returnValue = true; t.true(process.stdout.write('bar')); t.same(log, [['foo'], ['bar']]); }); test.serial('if silent, returns true by default', t => { const log = []; process.stdout = { write: () => t.fail() }; fn.stdout({silent: true}, str => { log.push(str); return str; }); t.true(process.stdout.write('foo')); t.same(log, ['foo']); }); test.serial('if silent, callback can return a boolean', t => { const log = []; let returnValue = true; process.stdout = { write: () => t.fail() }; fn.stdout({silent: true}, str => { log.push(str); return returnValue; }); t.true(process.stdout.write('foo')); returnValue = false; t.false(process.stdout.write('bar')); t.same(log, ['foo', 'bar']); }); test.serial('callback can return a buffer', t => { const log = []; process.stdout = { write: loggingWrite(log, () => true) }; fn.stdout(str => new Buffer(str)); t.true(process.stdout.write('foo')); t.true(process.stdout.write('bar')); t.same(log, [[new Buffer('foo')], [new Buffer('bar')]]); }); test.serial('callback receives encoding type', t => { const log = []; process.stdout = { write: () => t.fail() }; fn.stdout({silent: true}, loggingWrite(log, () => true)); t.true(process.stdout.write('a9fe', 'hex')); t.true(process.stdout.write('a234', 'hex')); t.same(log, [['a9fe', 'hex'], ['a234', 'hex']]); });