diff --git a/.gitattributes b/.gitattributes
index 391f0a4..6313b56 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,2 +1 @@
-* text=auto
-*.js text eol=lf
+* text=auto eol=lf
diff --git a/.travis.yml b/.travis.yml
index 57505cf..f3fa8cd 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,4 @@
-sudo: false
language: node_js
node_js:
+ - '10'
- '8'
- - '6'
- - '4'
diff --git a/index.d.ts b/index.d.ts
new file mode 100644
index 0000000..9bda3c1
--- /dev/null
+++ b/index.d.ts
@@ -0,0 +1,33 @@
+///
+
+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;
+
+ /**
+ 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;
+};
+
+export = getStdin;
diff --git a/index.js b/index.js
index b7483e4..b091359 100644
--- a/index.js
+++ b/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;
}
@@ -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) {
@@ -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));
});
});
};
diff --git a/index.test-d.ts b/index.test-d.ts
new file mode 100644
index 0000000..8fea6ec
--- /dev/null
+++ b/index.test-d.ts
@@ -0,0 +1,5 @@
+import {expectType} from 'tsd';
+import getStdin = require('.');
+
+expectType>(getStdin());
+expectType>(getStdin.buffer());
diff --git a/package.json b/package.json
index 309763b..a55b6d2 100644
--- a/package.json
+++ b/package.json
@@ -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",
@@ -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"
}
}
diff --git a/readme.md b/readme.md
index cc9ec1a..01644c5 100644
--- a/readme.md
+++ b/readme.md
@@ -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'
-});
+})
```
```
@@ -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
diff --git a/test-buffer.js b/test-buffer.js
index a6dfc1a..1e6dc26 100644
--- a/test-buffer.js
+++ b/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('')));
});
diff --git a/test-real.js b/test-real.js
index 895b9e0..74a2cec 100644
--- a/test-real.js
+++ b/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
});
diff --git a/test.js b/test.js
index a3510af..d161e13 100644
--- a/test.js
+++ b/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(), '');
});