Read Directory
Reads the contents of a directory.
Installation
$ npm install utils-fs-read-dir
Usage
var readdir = ;
readdir( path, clbk )
Reads the contents of a directory.
;{if errorconsole;elseconsole;}
readdir.sync( path )
Synchronously reads the contents of a directory.
var out = readdir;if out instanceof Errorthrow out;console;
Notes
-
The difference between this module and
fs.readdirSync()
is thatfs.readdirSync()
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 dir = '/path/to/dir';// Check for existence to prevent an error being thrown...if fsdir = fs;can be replaced by an approach which addresses existence via
error
handling.var readdir = ;var dir = '/path/to/dir';// Explicitly handle the error...dir = readdir;if dir instanceof Error// You choose what to do...throw dir;
Examples
var readdir = ;// Sync:var out = readdir;// returns <array>console;// returns falseout = readdir;// 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.