Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent 83c5c9f commit 7ddb55c
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 41 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
4 changes: 1 addition & 3 deletions .travis.yml
@@ -1,6 +1,4 @@
sudo: false
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
33 changes: 33 additions & 0 deletions index.d.ts
@@ -0,0 +1,33 @@
/// <reference types="node"/>

declare const getStdin: {
/**
Get [`stdin`](https://nodejs.org/api/process.html#process_process_stdin) as a `string`.
@returns A promise that is resolved when the `end` event fires on the `stdin` stream, indicating that there is no more data to be read. In a TTY context, an empty `string` is returned.
@example
```
// example.ts
import getStdin = require('get-stdin');
(async () => {
console.log(await getStdin());
//=> 'unicorns'
})
// $ echo unicorns | ts-node example.ts
// unicorns
```
*/
(): Promise<string>;

/**
Get [`stdin`](https://nodejs.org/api/process.html#process_process_stdin) as a `Buffer`.
@returns A promise that is resolved when the `end` event fires on the `stdin` stream, indicating that there is no more data to be read. In a TTY context, an empty `Buffer` is returned.
*/
buffer(): Promise<Buffer>;
};

export = getStdin;
20 changes: 10 additions & 10 deletions index.js
@@ -1,12 +1,12 @@
'use strict';
const stdin = process.stdin;
const {stdin} = process;

module.exports = () => {
let ret = '';
let result = '';

return new Promise(resolve => {
if (stdin.isTTY) {
resolve(ret);
resolve(result);
return;
}

Expand All @@ -16,19 +16,19 @@ module.exports = () => {
let chunk;

while ((chunk = stdin.read())) {
ret += chunk;
result += chunk;
}
});

stdin.on('end', () => {
resolve(ret);
resolve(result);
});
});
};

module.exports.buffer = () => {
const ret = [];
let len = 0;
const result = [];
let length = 0;

return new Promise(resolve => {
if (stdin.isTTY) {
Expand All @@ -40,13 +40,13 @@ module.exports.buffer = () => {
let chunk;

while ((chunk = stdin.read())) {
ret.push(chunk);
len += chunk.length;
result.push(chunk);
length += chunk.length;
}
});

stdin.on('end', () => {
resolve(Buffer.concat(ret, len));
resolve(Buffer.concat(result, length));
});
});
};
5 changes: 5 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
import getStdin = require('.');

expectType<Promise<string>>(getStdin());
expectType<Promise<Buffer>>(getStdin.buffer());
14 changes: 9 additions & 5 deletions package.json
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js"
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"std",
Expand All @@ -29,7 +30,10 @@
"read"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"@types/node": "^11.13.4",
"ava": "^1.4.1",
"delay": "^4.2.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -16,10 +16,10 @@ $ npm install get-stdin
// example.js
const getStdin = require('get-stdin');

getStdin().then(str => {
console.log(str);
(async () => {
console.log(await getStdin());
//=> 'unicorns'
});
})
```

```
Expand All @@ -36,13 +36,13 @@ Both methods returns a promise that is resolved when the `end` event fires on th

Get `stdin` as a `string`.

In a TTY context, a promise that resolves to an empty string is returned.
In a TTY context, a promise that resolves to an empty `string` is returned.

### getStdin.buffer()

Get `stdin` as a `Buffer`.

In a TTY context, a promise that resolves to an empty buffer is returned.
In a TTY context, a promise that resolves to an empty `Buffer` is returned.


## Related
Expand Down
16 changes: 9 additions & 7 deletions test-buffer.js
@@ -1,20 +1,22 @@
import test from 'ava';
import m from '.';
import {serial as test} from 'ava';
import delay from 'delay';
import getStdin from '.';

test.serial('get stdin', async t => {
t.plan(2);
test('get stdin', async t => {
process.stdin.isTTY = false;

const promise = m.buffer();
const promise = getStdin.buffer();
process.stdin.push(Buffer.from('uni'));
process.stdin.push(Buffer.from('corns'));
await delay(1);
process.stdin.emit('end');

const data = await promise;
t.true(data.equals(Buffer.from('unicorns')));
t.is(data.toString(), 'unicorns');
});

test.serial('get empty buffer when no stdin', async t => {
test('get empty buffer when no stdin', async t => {
process.stdin.isTTY = true;
t.true((await m.buffer()).equals(Buffer.from('')));
t.true((await getStdin.buffer()).equals(Buffer.from('')));
});
4 changes: 2 additions & 2 deletions test-real.js
@@ -1,6 +1,6 @@
'use strict';
const m = require('.');
const getStdin = require('.');

m().then(data => {
getStdin().then(data => {
process.exit(data ? 0 : 1); // eslint-disable-line unicorn/no-process-exit
});
17 changes: 10 additions & 7 deletions test.js
@@ -1,17 +1,20 @@
import test from 'ava';
import m from '.';
import {serial as test} from 'ava';
import delay from 'delay';
import getStdin from '.';

test.serial('get stdin', async t => {
t.plan(1);
test('get stdin', async t => {
process.stdin.isTTY = false;
const promise = m();
const promise = getStdin();

process.stdin.push('uni');
process.stdin.push('corns');
await delay(1);
process.stdin.emit('end');

t.is((await promise).trim(), 'unicorns');
});

test.serial('get empty string when no stdin', async t => {
test('get empty string when no stdin', async t => {
process.stdin.isTTY = true;
t.is(await m(), '');
t.is(await getStdin(), '');
});

0 comments on commit 7ddb55c

Please sign in to comment.