@prantlf/get-stdin-with-tty

Get stdin as a string or buffer from pipe or TTY

Usage no npm install needed!

<script type="module">
  import prantlfGetStdinWithTty from 'https://cdn.skypack.dev/@prantlf/get-stdin-with-tty';
</script>

README

@prantlf/get-stdin-with-tty

NPM version Build Status

Get stdin as a string or buffer

This fork of the original project includes support for reading stdin from TTY, not only via pipes.

Install

$ npm install @prantlf/get-stdin-with-tty

Usage

// example.js
const getStdin = require('@prantlf/get-stdin-with-tty');

(async () => {
    console.log(await getStdin({ tty: true }));
    //=> 'unicorns'
})();
$ echo unicorns | node example.js
unicorns

API

Both methods 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.

getStdin([options])

Get stdin as a string.

In a TTY context, a promise that resolves to an empty string is returned, unless options.tty or getStdin.tty is true.

getStdin.buffer()

Get stdin as a Buffer.

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

getStdin.tty = true/false

Set global TTY handling. When true, accepts input from TTY until a new line beginning with Ctrl-d or Ctrl-z (ASCII 04 and 26) is entered. (default = true)

When enabled for the example above:

$ node example.js
foobar
barfoo
^d
// =>
foobar
barfoo

Related

License

MIT © Sindre Sorhus © Moos © Ferdinand Prantl