Read File
Reads the entire contents of a file.
Installation
$ npm install utils-fs-read-file
Usage
var readFile = ;
readFile( path, [ options,] clbk )
Reads the entire contents of a file.
;{if errorconsole;elseconsole;}
The function accepts the same options as fs.readFile()
.
readFile.sync( path[, options] )
Synchronously reads the contents of an entire file.
var out = readFile;if out instanceof Errorthrow out;console;
The function accepts the same options as fs.readFileSync()
.
Notes
-
The difference between this module and
fs.readFileSync()
is thatfs.readFileSync()
will throw if anerror
is encountered (e.g., if given a non-existentpath
) and this module will return anerror
. Hence, the following anti-patternvar fs = ;var file = '/path/to/file.js';// Check for existence to prevent an error being thrown...if fsfile = fs;can be replaced by an approach which addresses existence via
error
handling.var readFile = ;var file = '/path/to/file.js';// Explicitly handle the error...file = readFile;if file instanceof Error// You choose what to do...throw file;
Examples
var readFile = ;// Sync:var file = readFile;// returns <string>console;// returns falsefile = readFile;// returns <error>console;// returns true// Async:;;{if errorif errorcode === 'ENOENT'console;elsethrow error;elseconsole;}
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2015. Athan Reines.