Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / fileset / lib / fileset.js
1 var util = require('util'),\r
2   minimatch = require('minimatch'),\r
3   Glob = require('glob').Glob,\r
4   EventEmitter = require('events').EventEmitter;\r
5 \r
6 module.exports = fileset;\r
7 \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
11 \r
12   var includes = (typeof include === 'string') ? include.split(' ') : include;\r
13   var excludes = (typeof exclude === 'string') ? exclude.split(' ') : exclude;\r
14 \r
15   var em = new EventEmitter,\r
16     remaining = includes.length,\r
17     results = [];\r
18 \r
19   if(!includes.length) return cb(new Error('Must provide an include pattern'));\r
20 \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
27   });\r
28 \r
29   function next(pattern, matches) {\r
30     results = results.concat(matches);\r
31 \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
37           return match;\r
38         }).length;\r
39       });\r
40 \r
41       if(cb) cb(null, results);\r
42       em.emit('end', results);\r
43     }\r
44   }\r
45 \r
46   return em;\r
47 }\r
48 \r
49 fileset.Fileset = function Fileset(pattern, options, cb) {\r
50 \r
51   if (typeof options === 'function') cb = options, options = {};\r
52   if (!options) options = {};\r
53 \r
54   Glob.call(this, pattern, options);\r
55 \r
56   if(typeof cb === 'function') {\r
57     this.on('error', cb);\r
58     this.on('end', function(matches) { cb(null, matches); });\r
59   }\r
60 };\r
61 \r
62 util.inherits(fileset.Fileset, Glob);\r
63 \r
64 \r