Skip to content

Support arrays of glob objects on sites #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 55 additions & 39 deletions lib/find/files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var glob = require("glob"),
var Glob = require("glob").Glob,
Q = require("q"),
_ = require("lodash"),
minimatch = require("minimatch");
minimatch = require("minimatch"),
util = require("util"),
EventEmitter = require("events");

// Resolve API difference in Node 0.10
if ("EventEmitter" in EventEmitter) { EventEmitter = EventEmitter.EventEmitter }

/**
* @function documentjs.find.files
Expand Down Expand Up @@ -32,46 +38,56 @@ var glob = require("glob"),
* @return {documentjs.process.types.FileEventEmitter} An event emitter that
* emits events for matched files.
*/
module.exports = function(options){
var pattern;
var globOptions;
module.exports = function( options ) {
var
emitter = new EventEmitter(),
remaining;

if(typeof options.glob === "string"){
var pattern = options.glob;
globOptions = {};
} else {
pattern = options.glob.pattern;
globOptions = _.extend({}, options.glob);
delete globOptions.pattern;
}

var glb = new glob.Glob(pattern, globOptions);
var ignore = options.glob.ignore;
options = options.glob;

if(typeof ignore === "string") {
ignore = [ignore];
if (!util.isArray(options)) {
options = [options];
}
if(ignore) {
// weave in ignore behavior
var oldOn = glb.on;
glb.on = function(event, listener) {
if(event === "match") {
var handler = function(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);

remaining = options.length;

options.forEach( function( options ) {
var
pattern,
glob,
ignore;

if (typeof options === "string") {
pattern = options;
options = {};
} else {
pattern = options.pattern;
options = _.extend({}, options);
delete options.pattern;
}

glob = new Glob(pattern, options);
ignore = options.ignore;

if (ignore && !util.isArray(ignore)) {
ignore = [ignore];
}

glob.on( "match", function( filepath ) {
for (var i = 0; ignore && (i < ignore.length); i++) {
if (minimatch(filepath, ignore[i])) return;
}
};
}


return glb;
emitter.emit( "match", filepath, glob.cwd );
});

glob.on( "end", function() {
if ( --remaining === 0 ) {
emitter.emit( "end" );
}
})

});

return emitter;
};
6 changes: 3 additions & 3 deletions lib/process/file_event_emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function processWithTags(fileEventEmitter, options) {
}
};

fileEventEmitter.on("match",function(src){
fileEventEmitter.on("match",function(src, cwd){
matched++;

src = path.normalize(src);
Expand All @@ -80,8 +80,8 @@ function processWithTags(fileEventEmitter, options) {
console.log("FIND:", path.relative(process.cwd(),src));
}

if( src.indexOf(fileEventEmitter.cwd) !== 0 ) {
var readSrc = path.join(fileEventEmitter.cwd, src);
if( src.indexOf(cwd) !== 0 ) {
var readSrc = path.join(cwd, src);
} else {
var readSrc = src;
}
Expand Down