Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // fun stuff -.-
  2. function requireUncached(module){
  3. delete require.cache[require.resolve(module)]
  4. return require(module)
  5. }
  6. var walk = function(dir, action, done) {
  7. var dead = false;
  8. var pending = 0;
  9. var fail = function(err) {
  10. if(!dead) {
  11. dead = true;
  12. done(err);
  13. }
  14. };
  15. var checkSuccess = function() {
  16. if(!dead && pending == 0) {
  17. done();
  18. }
  19. };
  20. var performAction = function(file, stat) {
  21. if(!dead) {
  22. try {
  23. action(file, stat);
  24. }
  25. catch(error) {
  26. fail(error);
  27. }
  28. }
  29. };
  30. var dive = function(dir) {
  31. pending++; // async operation starting after this line
  32. fs.readdir(dir, function(err, list) {
  33. if(!dead) { // if we are already dead, we don't do anything
  34. if (err) {
  35. fail(err); // if an error occured, let's fail
  36. }
  37. else { // iterate over the files
  38. list.forEach(function(file) {
  39. if(!dead) { // if we are already dead, we don't do anything
  40. var path = dir + "/" + file;
  41. pending++; // async operation starting after this line
  42. fs.stat(path, function(err, stat) {
  43. if(!dead) { // if we are already dead, we don't do anything
  44. if (err) {
  45. fail(err); // if an error occured, let's fail
  46. }
  47. else {
  48. if (stat && stat.isDirectory()) {
  49. dive(path); // it's a directory, let's explore recursively
  50. }
  51. else {
  52. performAction(path, stat); // it's not a directory, just perform the action
  53. }
  54. pending--; checkSuccess(); // async operation complete
  55. }
  56. }
  57. });
  58. }
  59. });
  60. pending--; checkSuccess(); // async operation complete
  61. }
  62. }
  63. });
  64. };
  65.  
  66. // start exploration
  67. dive(dir);
  68. };
  69.  
  70.  
  71. function updateCommands(heh){
  72. var i = 0;
  73. walk("/home/xeptix/MrMellow/cmd", function(a, b){
  74. var x = a.split("/");
  75. var y = x[x.length - 1].split(".js");
  76. var cmd = y[0].toLowerCase();
  77. var f = requireUncached(a);
  78. if(cmd == heh || !heh){
  79. i = i = 1;
  80. CmdManager.Commands[f.Name] = f;
  81. }
  82. }, function(a){
  83. Debug.log("lib/CmdManager.js", "Found and loaded " + i + " command(s)!", 2);
  84. });
  85. }
  86.  
  87. updateCommands();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement