Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3.  
  4. const availableCommands = [];
  5.  
  6. function loadCommands(dir = path.join(__dirname, '../commands')) {
  7.   fs.readdirSync(dir).forEach((file) => {
  8.     const filepath = path.join(dir, file);
  9.     const stat = fs.statSync(filepath);
  10.  
  11.     if (stat.isDirectory() && !stat.isSymbolicLink()) {
  12.       // If read file is a directory, search in that directory
  13.       loadCommands(filepath);
  14.     } else if (file === 'index.js') {
  15.       // If we encounter index.js file, it means that we found one of commands
  16.       // Extract command from given dir path
  17.       const lastDir = dir.split('/').pop();
  18.       const command = lastDir !== 'commands' ? lastDir : '';
  19.  
  20.       availableCommands.push({
  21.         command,
  22.         ...require(filepath),
  23.       });
  24.     }
  25.   });
  26.  
  27.   return availableCommands;
  28. }
  29.  
  30. module.exports = loadCommands;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement