Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
var util = require('util'),
2
minimatch = require('minimatch'),
3
Glob = require('glob').Glob,
4
EventEmitter = require('events').EventEmitter;
5
6
module.exports = fileset;
7
8
function fileset(include, exclude, options, cb) {
9
if (typeof exclude === 'function') cb = exclude, exclude = '';
10
else if (typeof options === 'function') cb = options, options = {};
11
12
var includes = (typeof include === 'string') ? include.split(' ') : include;
13
var excludes = (typeof exclude === 'string') ? exclude.split(' ') : exclude;
14
15
var em = new EventEmitter,
16
remaining = includes.length,
17
results = [];
18
19
if(!includes.length) return cb(new Error('Must provide an include pattern'));
20
21
em.includes = includes.map(function(pattern) {
22
return new fileset.Fileset(pattern, options)
23
.on('error', cb ? cb : em.emit.bind(em, 'error'))
24
.on('match', em.emit.bind(em, 'match'))
25
.on('match', em.emit.bind(em, 'include'))
26
.on('end', next.bind({}, pattern))
27
});
28
29
function next(pattern, matches) {
30
results = results.concat(matches);
31
32
if(!(--remaining)) {
33
results = results.filter(function(file) {
34
return !excludes.filter(function(glob) {
35
var match = minimatch(file, glob, { matchBase: true });
36
if(match) em.emit('exclude', file);
37
return match;
38
}).length;
39
});
40
41
if(cb) cb(null, results);
42
em.emit('end', results);
43
}
44
}
45
46
return em;
47
}
48
49
fileset.Fileset = function Fileset(pattern, options, cb) {
50
51
if (typeof options === 'function') cb = options, options = {};
52
if (!options) options = {};
53
54
Glob.call(this, pattern, options);
55
56
if(typeof cb === 'function') {
57
this.on('error', cb);
58
this.on('end', function(matches) { cb(null, matches); });
59
}
60
};
61
62
util.inherits(fileset.Fileset, Glob);
63
64
65
66