-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (75 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var glob = require("glob");
var _ = require("lodash");
var minimatch = require("minimatch");
var path = require('path');
/**
* @parent bit-docs-glob-finder/modules
* @module {function} bit-docs-glob-finder/index
*
* @signature `find.files(siteConfig)`
*
* @param {Object} siteConfig Options that configure the behavior of the files
* that will be processed.
*
* @option {String|bit-docs-glob-finder/types/globObject} glob The glob option
* either specifies a [minimatch](https://github.com/isaacs/minimatch) pattern
* like:
*
* find.files({glob: "*.js"})
*
* Or a [bit-docs-glob-finder/types/globObject] that specifies the a
* [minimatch](https://github.com/isaacs/minimatch) pattern and other
* siteConfig like:
*
* find.files({
* glob: {
* pattern: "*.js",
* cwd: __dirname
* }
* })
*
* @return {bit-docs/types/FileEventEmitter} An event emitter that emits events
* for matched files.
*/
module.exports = function(siteConfig){
var pattern;
var globOptions;
if(typeof siteConfig.glob === "string"){
var pattern = siteConfig.glob;
globOptions = {};
} else {
pattern = siteConfig.glob.pattern;
globOptions = _.extend({}, siteConfig.glob);
delete globOptions.pattern;
}
if(!globOptions.cwd && siteConfig.cwd) {
globOptions.cwd = siteConfig.cwd;
}
globOptions.nodir = true;
var glb = new glob.Glob(pattern, globOptions);
var ignore = siteConfig.glob.ignore;
if(typeof ignore === "string") {
ignore = [ignore];
}
if(ignore) {
// weave in ignore behavior
var oldOn = glb.on;
glb.on = function(event, listener) {
if(event === "match") {
var handler = function(filepath){
var dir = path.dirname(filepath);
for(var i = 0; i < ignore.length; i++) {
if( minimatch(filepath, ignore[i]) ) {
return;
}
}
listener.apply(this, arguments);
};
return oldOn.call(this, event, handler);
} else {
return oldOn.apply(this, arguments);
}
};
}
return glb;
};