Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created May 10, 2011 15:04
Show Gist options
  • Select an option

  • Save mklabs/964640 to your computer and use it in GitHub Desktop.

Select an option

Save mklabs/964640 to your computer and use it in GitHub Desktop.
findit + node-git (git-fs) = findgit
// callback style
findgit(Path.join(dir, 'templates'), function(err, r) {
console.log('test findgit', r.files.length);
});
// event emitter style
findgit(Path.join(dir, 'templates'))
.on('error', function(e){ console.log('Doooooh, there was an error', e); })
.on('file', function(file){ console.log('A file: ', file); })
.on('directory', function(dir){ console.log('A dir: ', dir); })
.on('end', function(r) {
console.log('test findgit end', r.files.length);
});
var findgit = function findgit(abspath, callback) {
var files = [], dirs = [], count = 0,
em = new events.EventEmitter;
(function recurse(p, f) {
Git.readDir('fs', p, function(err, results) {
if(err) {
em.emit('error', err, []);
return callback(err, []);
}
var d = results.dirs.map(function(a) {
var dir = Path.join(p, a);
em.emit('directory', dir);
return dir;
}),
f = results.files.map(function(a) {
var file = Path.join(p, a);
em.emit('file', file);
return file;
});
files = files.concat(f);
dirs = dirs.concat(d);
// if we have dirs, recurse
d.forEach(recurse);
// incr count so that we know where we are
count++;
// if no dir to work with, return callback
// also make sure we're done with all dirs
if(!d.length && (dirs.length + 1) === count) {
console.log('end emit');
em.emit('end', {files: files, dirs: dirs});
return callback && callback(null, {files: files, dirs: dirs});
}
});
})(abspath);
return em;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment