2.0.0 • Published 10 years ago
file-emitter v2.0.0
file-emitter
A recursive file emitter
Stability: 3 - StablefileEmitter(folder, opt_options)
Create a new FileEmitter object, extends events.EventEmitter.
{string} folder{?Object} opt_options
opt_options
{boolean} bufferLoad each file into a buffer before emitting, defaults tofalse{number} maxBufferSizeThe max size of a file buffer, defaults to10485760(=10MiB){boolean} incrementalWhentrueeachfileevent has to be acknowledged by callingfe.next(){boolean} followSymLinks..., defaults tofalse{boolean} recursive..., defaults totrue{boolean} autorun..., defaults totrue{Array<string>} excludeglob patterns applied afterreadir{Array<string>} includeglob patterns applied afterstat{Object} minimatchOptionsSeeminimatchREADME, defaults to{matchBase: true}{Function} FileA custom constructor for file objects, has to extendFile
Example
var fileEmitter = require('file-emitter');
var fe = fileEmitter('./lib', {
buffer: true,
incremental: true,
include: ['*.js'],
exclude: ['.git', 'node_modules', 'coverage', 'test']
});
fe.on('file', function(file) {
// {string} file.name
// {fs.Stats} file.stats
// {?Buffer} file.buffer
if (!file.buffer) { // maxBufferSize exceeded
var readableStream = file.createReadStream();
}
// acknowledge that the file has been processed
// see 'incremental' option
fe.next();
});
fe.on('error', function(err) { }); // everything but EMFILE
fe.once('end', function(hadError) { });fe.run()
Manually start scanning the folder & emitting files when autostart=false.
fe.next()
When the emitter runs with incremental = true a call to next() is required after each file event to acknowledge that the file has been processed.
Used to process files asynchronously while preventing allocation of large queues & file buffers.
fileEmitter.list(folder, opt_options, callback)
Convenience method to retrieve a list of files.
opt_options
- all options of
fileEmitter()except forincremental&autorun {function(File, File)} compareA compare function passed toArray.sort()
Example
fileEmitter.list('./', {
compare: function(a, b) {
return a.stats.size - b.stats.size;
}
}, function(err, files) { });Tests
npm test
firefox coverage/lcov-report/index.htmlCoverage
Statements : 94.05% ( 174/185 )
Branches : 89.90% ( 89/99 )
Functions : 100% ( 21/21 )