Skip to content

Commit 7ddb55c

Browse files
BendingBendersindresorhus
authored andcommitted
Require Node.js 8, add TypeScript definition (#27)
1 parent 83c5c9f commit 7ddb55c

10 files changed

Lines changed: 85 additions & 41 deletions

File tree

.gitattributes

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
sudo: false
21
language: node_js
32
node_js:
3+
- '10'
44
- '8'
5-
- '6'
6-
- '4'

index.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="node"/>
2+
3+
declare const getStdin: {
4+
/**
5+
Get [`stdin`](https://nodejs.org/api/process.html#process_process_stdin) as a `string`.
6+
7+
@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.
8+
9+
@example
10+
```
11+
// example.ts
12+
import getStdin = require('get-stdin');
13+
14+
(async () => {
15+
console.log(await getStdin());
16+
//=> 'unicorns'
17+
})
18+
19+
// $ echo unicorns | ts-node example.ts
20+
// unicorns
21+
```
22+
*/
23+
(): Promise<string>;
24+
25+
/**
26+
Get [`stdin`](https://nodejs.org/api/process.html#process_process_stdin) as a `Buffer`.
27+
28+
@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.
29+
*/
30+
buffer(): Promise<Buffer>;
31+
};
32+
33+
export = getStdin;

index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
const stdin = process.stdin;
2+
const {stdin} = process;
33

44
module.exports = () => {
5-
let ret = '';
5+
let result = '';
66

77
return new Promise(resolve => {
88
if (stdin.isTTY) {
9-
resolve(ret);
9+
resolve(result);
1010
return;
1111
}
1212

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

1818
while ((chunk = stdin.read())) {
19-
ret += chunk;
19+
result += chunk;
2020
}
2121
});
2222

2323
stdin.on('end', () => {
24-
resolve(ret);
24+
resolve(result);
2525
});
2626
});
2727
};
2828

2929
module.exports.buffer = () => {
30-
const ret = [];
31-
let len = 0;
30+
const result = [];
31+
let length = 0;
3232

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

4242
while ((chunk = stdin.read())) {
43-
ret.push(chunk);
44-
len += chunk.length;
43+
result.push(chunk);
44+
length += chunk.length;
4545
}
4646
});
4747

4848
stdin.on('end', () => {
49-
resolve(Buffer.concat(ret, len));
49+
resolve(Buffer.concat(result, length));
5050
});
5151
});
5252
};

index.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {expectType} from 'tsd';
2+
import getStdin = require('.');
3+
4+
expectType<Promise<string>>(getStdin());
5+
expectType<Promise<Buffer>>(getStdin.buffer());

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=4"
13+
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js"
16+
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"std",
@@ -29,7 +30,10 @@
2930
"read"
3031
],
3132
"devDependencies": {
32-
"ava": "*",
33-
"xo": "*"
33+
"@types/node": "^11.13.4",
34+
"ava": "^1.4.1",
35+
"delay": "^4.2.0",
36+
"tsd": "^0.7.2",
37+
"xo": "^0.24.0"
3438
}
3539
}

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ $ npm install get-stdin
1616
// example.js
1717
const getStdin = require('get-stdin');
1818

19-
getStdin().then(str => {
20-
console.log(str);
19+
(async () => {
20+
console.log(await getStdin());
2121
//=> 'unicorns'
22-
});
22+
})
2323
```
2424

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

3737
Get `stdin` as a `string`.
3838

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

4141
### getStdin.buffer()
4242

4343
Get `stdin` as a `Buffer`.
4444

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

4747

4848
## Related

test-buffer.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import test from 'ava';
2-
import m from '.';
1+
import {serial as test} from 'ava';
2+
import delay from 'delay';
3+
import getStdin from '.';
34

4-
test.serial('get stdin', async t => {
5-
t.plan(2);
5+
test('get stdin', async t => {
66
process.stdin.isTTY = false;
77

8-
const promise = m.buffer();
8+
const promise = getStdin.buffer();
99
process.stdin.push(Buffer.from('uni'));
1010
process.stdin.push(Buffer.from('corns'));
11+
await delay(1);
1112
process.stdin.emit('end');
13+
1214
const data = await promise;
1315
t.true(data.equals(Buffer.from('unicorns')));
1416
t.is(data.toString(), 'unicorns');
1517
});
1618

17-
test.serial('get empty buffer when no stdin', async t => {
19+
test('get empty buffer when no stdin', async t => {
1820
process.stdin.isTTY = true;
19-
t.true((await m.buffer()).equals(Buffer.from('')));
21+
t.true((await getStdin.buffer()).equals(Buffer.from('')));
2022
});

test-real.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const m = require('.');
2+
const getStdin = require('.');
33

4-
m().then(data => {
4+
getStdin().then(data => {
55
process.exit(data ? 0 : 1); // eslint-disable-line unicorn/no-process-exit
66
});

test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import test from 'ava';
2-
import m from '.';
1+
import {serial as test} from 'ava';
2+
import delay from 'delay';
3+
import getStdin from '.';
34

4-
test.serial('get stdin', async t => {
5-
t.plan(1);
5+
test('get stdin', async t => {
66
process.stdin.isTTY = false;
7-
const promise = m();
7+
const promise = getStdin();
8+
89
process.stdin.push('uni');
910
process.stdin.push('corns');
11+
await delay(1);
1012
process.stdin.emit('end');
13+
1114
t.is((await promise).trim(), 'unicorns');
1215
});
1316

14-
test.serial('get empty string when no stdin', async t => {
17+
test('get empty string when no stdin', async t => {
1518
process.stdin.isTTY = true;
16-
t.is(await m(), '');
19+
t.is(await getStdin(), '');
1720
});

0 commit comments

Comments
 (0)