From 7fef00f47e267a693fd40d72b229a9ef7449adbc Mon Sep 17 00:00:00 2001 From: rjgotten Date: Thu, 21 Jan 2016 10:18:41 +0100 Subject: [PATCH 1/2] Support arrays of glob objects on sites Extends the file finder to create and process multiple Glob instances. As this requires wrapping multiple Globs into a single unified EventEmitter, the FileEventEmitter contract had to be changed slightly to send cwd as a second parameter to the "match" event. --- lib/find/files.js | 91 ++++++++++++++++++------------- lib/process/file_event_emitter.js | 6 +- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/lib/find/files.js b/lib/find/files.js index cf877fcfb..8197d9e9c 100644 --- a/lib/find/files.js +++ b/lib/find/files.js @@ -1,6 +1,9 @@ -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"); /** * @function documentjs.find.files @@ -32,46 +35,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; } From 6b3daf1d27e7a08fd2293ec15fbb52db9100268f Mon Sep 17 00:00:00 2001 From: rjgotten Date: Thu, 21 Jan 2016 10:39:41 +0100 Subject: [PATCH 2/2] Resolve Node 0.10 EventEmitter API problem --- lib/find/files.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/find/files.js b/lib/find/files.js index 8197d9e9c..86ca7b84e 100644 --- a/lib/find/files.js +++ b/lib/find/files.js @@ -5,6 +5,9 @@ var Glob = require("glob").Glob, util = require("util"), EventEmitter = require("events"); +// Resolve API difference in Node 0.10 +if ("EventEmitter" in EventEmitter) { EventEmitter = EventEmitter.EventEmitter } + /** * @function documentjs.find.files *