npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

read-input

v0.3.1

Published

Easily read from stdin or files.

Downloads

203,400

Readme

read-input

Write CLI utilities that take a stream of data either stdin from one of more files. To be able to handle this:

$ yourutil file.txt
$ yourutil file1.txt file2.txt
$ yourutil < file.txt
$ cat file.txt | yourutil

Just do this:

#!/usr/bin/env node
var read = require('read-input');
var fnames = process.argv.slice(2);

read(fnames, function (err, res) {
  // `err` will be given if at least one of the files fail.
  if (err) {
    console.error(err.message);
    process.exit(8);
  }

  res.data /* => "..." */
});

read()

read(files, function(err, res) { ... })

Reads from files. If no files are given, read from stdin. The result res is a result object. If any of the files can't be read, err will be an error object.

var read = require('read-input');
var fnames = process.argv.slice(2); //=> ['readme.txt']

read(fnames).then(function (res) {
  res.data       // '...'
  res.error      // undefined or Error()
  res.stdin      // true or false
  res.files      // [...]
  res.successes  // [...]
  res.failures   // [...]
}).catch(function (err) {
  // stdin error
});

To support older versions of Node.js without Promises, you can use callbacks:

read(fname, function (err, res) {
});

You can also iterate through res.files.

read(fnames).then(function(res) {
  res.files.forEach(function (f) {
    f.data    // ...
    f.error   // undefined or Error(...)
    f.stdin   // true or false
    f.name    // 'readme.txt'
  }
});

If files is a blank array (or null), data will be read from stdin. The resulting data will have a similar schema.

read([]).then(fucntion (res) {
  ...
});

read.stdin()

read.stdin(fn)

Read data from standard input. This will not throw errors.

read.stdin().then(function (data) {
  console.log(data); // string
});

read.stdin(function (err, data) {
  ...
});

res

The results value is an object passed to the callback of read().

  • data (String) a concatenation of all data in all the files.
  • error (Error) The first error in all files. undefined if successful.
  • stdin (Boolean) is true if the file is read from stdin
  • files (Array) A list of files.
  • failures (Array) A list of files that failed.
  • successes (Array) A list of files that succeeded.

The files, failures and successes are lists of files. Each of the items in these lists has a similar list of values:

  • data (String) File data
  • error (Error) the first error encountered, if applicable
  • stdin (Boolean) is true if the file is read from stdin
  • name (String) File name

There's also error.result which refers to the result object.

See read() for an example.

Thanks

read-input © 2014+, Rico Sta. Cruz. Released under the MIT License. Authored and maintained by Rico Sta. Cruz with help from contributors.

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz