diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f9fe6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.DS_Store +.nyc_output diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..836440b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,14 @@ +Copyright (c) 2016, Contributors + +Permission to use, copy, modify, and/or distribute this software +for any purpose with or without fee is hereby granted, provided +that the above copyright notice and this permission notice +appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE +LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d073c1d --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# set-blocking + +[![Build Status](https://travis-ci.org/yargs/set-blocking.svg)](https://travis-ci.org/yargs/set-blocking) +[![NPM version](https://img.shields.io/npm/v/set-blocking.svg)](https://www.npmjs.com/package/set-blocking) +[![Coverage Status](https://coveralls.io/repos/yargs/set-blocking/badge.svg?branch=)](https://coveralls.io/r/yargs/set-blocking?branch=master) +[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version) + +set blocking `stdio` and `stderr` ensuring that terminal output does not truncate + +```js +const setBlocking = require('set-blocking') +setBlocking(true) +console.log(someLargeStringToOutput) +``` + +## License + +ISC diff --git a/index.js b/index.js new file mode 100644 index 0000000..1973abd --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ +module.exports = function (blocking) { + [process.stdout, process.stderr].forEach(function(stream){ + if (stream._handle && typeof stream._handle.setBlocking === 'function') { + stream._handle.setBlocking(blocking) + } + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..6e13e77 --- /dev/null +++ b/package.json @@ -0,0 +1,37 @@ +{ + "name": "set-blocking", + "version": "1.0.0", + "description": "set blocking stdio and stderr ensuring that terminal output does not truncate", + "main": "index.js", + "scripts": { + "test": "nyc mocha ./test/*.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/yargs/set-blocking.git" + }, + "keywords": [ + "flush", + "terminal", + "blocking", + "stdio", + "stderr" + ], + "author": "Ben Coe ", + "license": "ISC", + "bugs": { + "url": "https://github.com/yargs/set-blocking/issues" + }, + "homepage": "https://github.com/yargs/set-blocking#readme", + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^2.4.5", + "nyc": "^6.4.4", + "standard": "^7.0.1", + "standard-version": "^2.2.1" + }, + "files": [ + "index.js", + "LICENSE.txt" + ] +} diff --git a/test/fixtures/yargs-497-stderr.js b/test/fixtures/yargs-497-stderr.js new file mode 100755 index 0000000..9b498af --- /dev/null +++ b/test/fixtures/yargs-497-stderr.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +// see: https://github.com/yargs/yargs/issues/497 +var buffer = '' +for (var i = 0; i < 3000; i++) { + buffer += 'line ' + i + '\n' +} + +var setBlocking = require('../../') +setBlocking(true) + +console.error(buffer) +process.exit(1) + +console.log('should not execute') diff --git a/test/fixtures/yargs-497-stdout.js b/test/fixtures/yargs-497-stdout.js new file mode 100755 index 0000000..fdc792a --- /dev/null +++ b/test/fixtures/yargs-497-stdout.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +// see: https://github.com/yargs/yargs/issues/497 +var buffer = '' +for (var i = 0; i < 3000; i++) { + buffer += 'line ' + i + '\n' +} + +var setBlocking = require('../../') +setBlocking(true) + +console.log(buffer) +process.exit(0) + +console.log('should not execute') diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..1f91356 --- /dev/null +++ b/test/test.js @@ -0,0 +1,24 @@ +/* global describe, should */ + +var exec = require('child_process').exec +var setBlocking = require('../') + +require('chai').should() + +describe('setBlocking', function () { + // see: https://github.com/yargs/yargs/issues/497 + it('does not truncate text printed to stdout when process.exit() is called', function (done) { + exec('./test/fixtures/yargs-497-stdout.js', function (err, stdout, stderr) { + if (err) return done(err) + stdout.should.match(/line 2999/) + return done() + }) + }) + + it('does not truncate text printed to stderr when process.exit() is called', function (done) { + exec('./test/fixtures/yargs-497-stderr.js', function (err, stdout, stderr) { + stderr.should.match(/line 2999/) + return done() + }) + }) +})