1 var util = require('util'),
\r
2 minimatch = require('minimatch'),
\r
3 Glob = require('glob').Glob,
\r
4 EventEmitter = require('events').EventEmitter;
\r
6 module.exports = fileset;
\r
8 function fileset(include, exclude, options, cb) {
\r
9 if (typeof exclude === 'function') cb = exclude, exclude = '';
\r
10 else if (typeof options === 'function') cb = options, options = {};
\r
12 var includes = (typeof include === 'string') ? include.split(' ') : include;
\r
13 var excludes = (typeof exclude === 'string') ? exclude.split(' ') : exclude;
\r
15 var em = new EventEmitter,
\r
16 remaining = includes.length,
\r
19 if(!includes.length) return cb(new Error('Must provide an include pattern'));
\r
21 em.includes = includes.map(function(pattern) {
\r
22 return new fileset.Fileset(pattern, options)
\r
23 .on('error', cb ? cb : em.emit.bind(em, 'error'))
\r
24 .on('match', em.emit.bind(em, 'match'))
\r
25 .on('match', em.emit.bind(em, 'include'))
\r
26 .on('end', next.bind({}, pattern))
\r
29 function next(pattern, matches) {
\r
30 results = results.concat(matches);
\r
32 if(!(--remaining)) {
\r
33 results = results.filter(function(file) {
\r
34 return !excludes.filter(function(glob) {
\r
35 var match = minimatch(file, glob, { matchBase: true });
\r
36 if(match) em.emit('exclude', file);
\r
41 if(cb) cb(null, results);
\r
42 em.emit('end', results);
\r
49 fileset.Fileset = function Fileset(pattern, options, cb) {
\r
51 if (typeof options === 'function') cb = options, options = {};
\r
52 if (!options) options = {};
\r
54 Glob.call(this, pattern, options);
\r
56 if(typeof cb === 'function') {
\r
57 this.on('error', cb);
\r
58 this.on('end', function(matches) { cb(null, matches); });
\r
62 util.inherits(fileset.Fileset, Glob);
\r