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 nodevar read = ;var fnames = processargv; ;
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 = ;var fnames = processargv; //=> ['readme.txt'] ;
To support older versions of Node.js without Promises, you can use callbacks:
;
You can also iterate through res.files
.
If files
is a blank array (or null), data will be read from stdin. The
resulting data will have a similar schema.
;
read.stdin()
read.stdin(fn)
Read data from standard input. This will not throw errors.
read; read;
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) — istrue
if the file is read from stdinfiles
(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 dataerror
(Error) — the first error encountered, if applicablestdin
(Boolean) — istrue
if the file is read from stdinname
(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