Comparing version
63
index.js
'use strict'; | ||
var stdin = process.stdin; | ||
module.exports = function (cb) { | ||
var stdin = process.stdin; | ||
module.exports = function () { | ||
var ret = ''; | ||
if (stdin.isTTY) { | ||
setImmediate(cb, ''); | ||
return; | ||
} | ||
return new Promise(function (resolve) { | ||
if (stdin.isTTY) { | ||
resolve(ret); | ||
return; | ||
} | ||
stdin.setEncoding('utf8'); | ||
stdin.setEncoding('utf8'); | ||
stdin.on('readable', function () { | ||
var chunk; | ||
stdin.on('readable', function () { | ||
var chunk; | ||
while (chunk = stdin.read()) { | ||
ret += chunk; | ||
} | ||
}); | ||
while ((chunk = stdin.read())) { | ||
ret += chunk; | ||
} | ||
}); | ||
stdin.on('end', function () { | ||
cb(ret); | ||
stdin.on('end', function () { | ||
resolve(ret); | ||
}); | ||
}); | ||
}; | ||
module.exports.buffer = function (cb) { | ||
var stdin = process.stdin; | ||
module.exports.buffer = function () { | ||
var ret = []; | ||
var len = 0; | ||
if (stdin.isTTY) { | ||
setImmediate(cb, new Buffer('')); | ||
return; | ||
} | ||
return new Promise(function (resolve) { | ||
if (stdin.isTTY) { | ||
resolve(new Buffer('')); | ||
return; | ||
} | ||
stdin.on('readable', function () { | ||
var chunk; | ||
stdin.on('readable', function () { | ||
var chunk; | ||
while (chunk = stdin.read()) { | ||
ret.push(chunk); | ||
len += chunk.length; | ||
} | ||
}); | ||
while ((chunk = stdin.read())) { | ||
ret.push(chunk); | ||
len += chunk.length; | ||
} | ||
}); | ||
stdin.on('end', function () { | ||
cb(Buffer.concat(ret, len)); | ||
stdin.on('end', function () { | ||
resolve(Buffer.concat(ret, len)); | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "get-stdin", | ||
"version": "4.0.1", | ||
"description": "Easier stdin", | ||
"version": "5.0.0", | ||
"description": "Get stdin as a string or buffer", | ||
"license": "MIT", | ||
@@ -10,9 +10,9 @@ "repository": "sindresorhus/get-stdin", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "http://sindresorhus.com" | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=0.12.0" | ||
}, | ||
"scripts": { | ||
"test": "node test.js && node test-buffer.js && echo unicorns | node test-real.js" | ||
"test": "xo && ava test.js test-buffer.js && echo unicorns | ava test-real.js" | ||
}, | ||
@@ -30,8 +30,9 @@ "files": [ | ||
"process", | ||
"stream" | ||
"read" | ||
], | ||
"devDependencies": { | ||
"ava": "0.0.4", | ||
"buffer-equal": "0.0.1" | ||
"ava": "*", | ||
"buffer-equals": "^1.0.3", | ||
"xo": "*" | ||
} | ||
} |
# get-stdin [](https://travis-ci.org/sindresorhus/get-stdin) | ||
> Easier stdin | ||
> Get stdin as a string or buffer | ||
@@ -8,3 +8,3 @@ | ||
```sh | ||
``` | ||
$ npm install --save get-stdin | ||
@@ -18,11 +18,11 @@ ``` | ||
// example.js | ||
var stdin = require('get-stdin'); | ||
const stdin = require('get-stdin'); | ||
stdin(function (data) { | ||
console.log(data); | ||
//=> unicorns | ||
stdin.then(x => { | ||
console.log(x); | ||
//=> 'unicorns' | ||
}); | ||
``` | ||
```sh | ||
``` | ||
$ echo unicorns | node example.js | ||
@@ -35,13 +35,17 @@ unicorns | ||
### stdin(callback) | ||
### stdin() | ||
Get `stdin` as a string. | ||
### stdin.buffer(callback) | ||
Returns a promise. | ||
### stdin.buffer() | ||
Get `stdin` as a buffer. | ||
Returns a promise. | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
3196
64.74%4
33.33%41
7.89%49
8.89%3
50%