0.0.3 • Published 12 years ago

dirstream v0.0.3

Weekly downloads
12
License
-
Repository
github
Last release
12 years ago

DirStream

Simple stream wrapper for fs.readdir(). Takes a path and streams the contents of the target directory.

Installation

Install with NPM:

npm install dirstream

Usage

To get a stream of file names from a target directory just do:

var path  = 'path/to/folder/to/scan';
var files = require('dirstream').createStream(path);
files.on('data', function (file) {
  console.log(file);
});
files.on('end', function () {
  console.log('All files logged! Fish: ><(( v ) ">');
});

To get a stream of only the files that match a given pattern do:

var path  = 'path/to/folder/to/scan';
var pattern = /\.jpg$/;
var files = require('dirstream').createStream(path, pattern);
files.on('data', function (jpg) {
  console.log(file);
});
files.on('count', function (count) {
  console.log('Hang on for a stream of ' + count + ' files!');
});

Events

count

DirStream will emit a count event after the results from fs.readdir() has been counted. Files excluded by filter will not be included in this count.

data

The data event is emitted once for every file in the target directory.

end

The end event will be emitted once all files have been emitted.

error

If DirStream encounters an error, the error event will be emitted.

Pause/Resume

DirStream has full support for pausing and resuming. If for example you need to load and parse the files emitted but only want to do process a few files at a time. You could implement an internal counter and pause the stream using the .pause() function on the stream once you're processing x number of files. Then call .resume() once a file has completed parsing. Or something.

License

See LICENSE.