Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fun stuff -.-
- function requireUncached(module){
- delete require.cache[require.resolve(module)]
- return require(module)
- }
- var walk = function(dir, action, done) {
- var dead = false;
- var pending = 0;
- var fail = function(err) {
- if(!dead) {
- dead = true;
- done(err);
- }
- };
- var checkSuccess = function() {
- if(!dead && pending == 0) {
- done();
- }
- };
- var performAction = function(file, stat) {
- if(!dead) {
- try {
- action(file, stat);
- }
- catch(error) {
- fail(error);
- }
- }
- };
- var dive = function(dir) {
- pending++; // async operation starting after this line
- fs.readdir(dir, function(err, list) {
- if(!dead) { // if we are already dead, we don't do anything
- if (err) {
- fail(err); // if an error occured, let's fail
- }
- else { // iterate over the files
- list.forEach(function(file) {
- if(!dead) { // if we are already dead, we don't do anything
- var path = dir + "/" + file;
- pending++; // async operation starting after this line
- fs.stat(path, function(err, stat) {
- if(!dead) { // if we are already dead, we don't do anything
- if (err) {
- fail(err); // if an error occured, let's fail
- }
- else {
- if (stat && stat.isDirectory()) {
- dive(path); // it's a directory, let's explore recursively
- }
- else {
- performAction(path, stat); // it's not a directory, just perform the action
- }
- pending--; checkSuccess(); // async operation complete
- }
- }
- });
- }
- });
- pending--; checkSuccess(); // async operation complete
- }
- }
- });
- };
- // start exploration
- dive(dir);
- };
- function updateCommands(heh){
- var i = 0;
- walk("/home/xeptix/MrMellow/cmd", function(a, b){
- var x = a.split("/");
- var y = x[x.length - 1].split(".js");
- var cmd = y[0].toLowerCase();
- var f = requireUncached(a);
- if(cmd == heh || !heh){
- i = i = 1;
- CmdManager.Commands[f.Name] = f;
- }
- }, function(a){
- Debug.log("lib/CmdManager.js", "Found and loaded " + i + " command(s)!", 2);
- });
- }
- updateCommands();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement