Skip to content

Instantly share code, notes, and snippets.

@rubydoc
Last active December 21, 2015 05:09
Show Gist options
  • Save rubydoc/6255152 to your computer and use it in GitHub Desktop.
Save rubydoc/6255152 to your computer and use it in GitHub Desktop.
Walker // TODO :: need to order by orderRule
// TODO :: need to order by orderRule
var Walker = function(dirPath, orderRule, whitelist, blacklist){
if (blacklist === undefined){ blacklist = function(path){ return false; }; };
if (whitelist === undefined){ whitelist = function(path){ return true; }; };
if (orderRule == undefined) { orderRule = function(a, b){ return -1; }; };
if (dirPath == undefined) {
console.log('please input root directory path.');
return false;
}
this.whitelist = whitelist;
this.blacklist = blacklist;
this.orderRule = orderRule;
this.pathList = [];
this.findDir(dirPath);
}
Walker.prototype.findDir = function(dir){
var that = this;
var path = require('path')
, fs = require('fs');
var pathes = fs.readdirSync(dir);
pathes.forEach(function(filepath, key, pathes){
var fullPath = path.join(dir, filepath);
if (that.whitelist(fullPath) && !that.blacklist(fullPath)){
var lpath = fs.lstatSync(fullPath);
if (lpath.isDirectory()) {
that.findDir(fullPath);
}else{
that.pathList.push(fullPath);
}
}
});
};
Walker.prototype.display = function(){
this.pathList.forEach(function(path, index, pl){
console.log(path);
});
};
var rootPath = '/Users';
walker = new Walker(rootPath);
walker.display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment