sfcode
An Online Competing and Development Environment
|
Recursive version of fs.readdir. Exposes a stream api.
Meant to be one of the recursive versions of fs functions, e.g., like mkdirp.
Table of Contents generated with DocToc
npm install readdirp
var entryStream = readdirp (options)
Reads given root recursively and returns a stream
of entry infos.
Behaves as follows:
passes an [entry info](#entry-info) whenever one is found -
emit('warn')passes a non-fatal
Error` that prevents a file/directory from being processed (i.e., if it is inaccessible to the user)passes a fatal
Errorwhich also ends the stream (i.e., when illegal options where passed) -
emit('end')called when all entries were found and no more will be emitted (i.e., we are done) -
emit('close')called when the stream is destroyed via
stream.destroy()(which could be useful if you want to manually abort even on a non fatal error) - at that point the stream is no longer
readable` and no more entries, warning or errors are emitted,
'directories',
'both', or
'all'. Setting to
'all'will also include entries for other types of file descriptors like character devices, unix sockets and named pipes. Defaults to
'files'`.true
, readdirp uses fs.lstat
instead of fs.stat
in order to stat files and includes symlink entries in the stream along with files.Has the following properties:
/User/dev/readdirp
) parentDir : 'test/bed/root_dir1', fullParentDir : '/User/dev/readdirp/test/bed/root_dir1', name : 'root_dir1_subdir1', path : 'test/bed/root_dir1/root_dir1_subdir1', fullPath : '/User/dev/readdirp/test/bed/root_dir1/root_dir1_subdir1', stat : [ ... ]
There are three different ways to specify filters for files and directories respectively.
glob string: a string (e.g., *.js
) which is matched using minimatch, so go there for more information.
Globstars (**
) are not supported since specifying a recursive pattern for an already recursive function doesn't make sense.
Negated globs (as explained in the minimatch documentation) are allowed, e.g., !*.txt
matches everything but text files.
array of glob strings: either need to be all inclusive or all exclusive (negated) patterns otherwise an error is thrown.
‘[ ’*.json', '*.js' ]` includes all JavaScript and Json files.
`[ '!.git', '!node_modules' ]` includes all directories except the '.git' and 'node_modules'.
Directories that do not pass a filter will not be recursed into.
Although the stream api is recommended, readdirp also exposes a callback based api.
readdirp (options, callback1 [, callback2])
If callback2 is given, callback1 functions as the fileProcessed callback, and callback2 as the allProcessed callback.
If only callback1 is given, it functions as the allProcessed callback.
function (err, res) { ... }
function (entryInfo) { ... }
‘on('error’, ..),
on('warn', ..)and
on('end', ..)` handling omitted for brevity
Try more examples by following instructions on how to get going.
The readdirp tests also will give you a good idea on how things work.