Skip to content

skenqbx/file-emitter

Repository files navigation

file-emitter

NPM version downloads node version Build Status

A recursive file emitter

Stability: 3 - Stable

fileEmitter(folder, opt_options)

Create a new FileEmitter object, extends events.EventEmitter.

  • {string} folder
  • {?Object} opt_options

opt_options

  • {boolean} buffer Load each file into a buffer before emitting, defaults to false
  • {number} maxBufferSize The max size of a file buffer, defaults to 10485760 (=10MiB)
  • {boolean} incremental When true each file event has to be acknowledged by calling fe.next()
  • {boolean} followSymLinks ..., defaults to false
  • {boolean} recursive ..., defaults to true
  • {boolean} autorun ..., defaults to true
  • {Array<string>} exclude glob patterns applied after readir
  • {Array<string>} include glob patterns applied after stat
  • {Object} minimatchOptions See minimatch README, defaults to {matchBase: true}
  • {Function} File A custom constructor for file objects, has to extend File

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 for incremental & autorun
  • {function(File, File)} compare A compare function passed to Array.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.html

Coverage

Statements   : 94.05% ( 174/185 )
Branches     : 89.90% ( 89/99 )
Functions    : 100%   ( 21/21 )

About

A recursive file emitter

Resources

License

Stars

Watchers

Forks

Packages

No packages published