diff --git a/lib/find/files.js b/lib/find/files.js index cf877fcfb..86ca7b84e 100644 --- a/lib/find/files.js +++ b/lib/find/files.js @@ -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 @@ -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; }; \ No newline at end of file diff --git a/lib/process/file_event_emitter.js b/lib/process/file_event_emitter.js index 6ceffbaad..1eadfe89d 100644 --- a/lib/process/file_event_emitter.js +++ b/lib/process/file_event_emitter.js @@ -71,7 +71,7 @@ function processWithTags(fileEventEmitter, options) { } }; - fileEventEmitter.on("match",function(src){ + fileEventEmitter.on("match",function(src, cwd){ matched++; src = path.normalize(src); @@ -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; }