Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 16, 2021
1 parent a326009 commit 8f70f58
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
12 changes: 4 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="node"/>

declare const getStdin: {
/**
Get [`stdin`](https://nodejs.org/api/process.html#process_process_stdin) as a `string`.
Expand All @@ -9,12 +7,10 @@ declare const getStdin: {
@example
```
// example.ts
import getStdin = require('get-stdin');
import getStdin from 'get-stdin';
(async () => {
console.log(await getStdin());
//=> 'unicorns'
})
console.log(await getStdin());
//=> 'unicorns'
// $ echo unicorns | ts-node example.ts
// unicorns
Expand All @@ -30,4 +26,4 @@ declare const getStdin: {
buffer(): Promise<Buffer>;
};

export = getStdin;
export default getStdin;
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const {stdin} = process;

module.exports = async () => {
export default async function getStdin() {
let result = '';

if (stdin.isTTY) {
Expand All @@ -15,9 +14,9 @@ module.exports = async () => {
}

return result;
};
}

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

Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import getStdin = require('.');
import getStdin from './index.js';

expectType<Promise<string>>(getStdin());
expectType<Promise<Buffer>>(getStdin.buffer());
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js && tsd"
Expand All @@ -31,10 +33,10 @@
"read"
],
"devDependencies": {
"@types/node": "^13.13.5",
"ava": "^2.4.0",
"delay": "^4.2.0",
"tsd": "^0.11.0",
"xo": "^0.24.0"
"@types/node": "^14.14.41",
"ava": "^3.15.0",
"delay": "^5.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ $ npm install get-stdin

```js
// example.js
const getStdin = require('get-stdin');
import getStdin from 'get-stdin';

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

```
Expand Down
10 changes: 5 additions & 5 deletions test-buffer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {serial as test} from 'ava';
import test from 'ava';
import delay from 'delay';
import getStdin from '.';
import getStdin from './index.js';

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

const promise = getStdin.buffer();
process.stdin.push(Buffer.from('uni'));
process.stdin.push(Buffer.from('corns'));
process.stdin.push(Buffer.from('corns')); // eslint-disable-line unicorn/no-array-push-push
await delay(1);
process.stdin.emit('end');

Expand All @@ -16,7 +16,7 @@ test('get stdin', async t => {
t.is(data.toString(), 'unicorns');
});

test('get empty buffer when no stdin', async t => {
test.serial('get empty buffer when no stdin', async t => {
process.stdin.isTTY = true;
t.true((await getStdin.buffer()).equals(Buffer.from('')));
});
3 changes: 1 addition & 2 deletions test-real.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const getStdin = require('.');
import getStdin from './index.js';

(async () => {
const stdin = await getStdin();
Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {serial as test} from 'ava';
import test from 'ava';
import delay from 'delay';
import getStdin from '.';
import getStdin from './index.js';

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

process.stdin.push('uni');
process.stdin.push('corns');
process.stdin.push('corns'); // eslint-disable-line unicorn/no-array-push-push
await delay(1);
process.stdin.emit('end');

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

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

0 comments on commit 8f70f58

Please sign in to comment.