{"size": 1516, "ext": "js", "max_stars_repo_path": "src/classes/command/CommandManager.js", "max_stars_repo_name": "Les-Laboratoires-JS/pancakes-handler", "max_stars_repo_stars_event_min_datetime": "2020-01-21T22:20:29.000Z", "max_stars_repo_stars_event_max_datetime": "2020-01-24T16:23:48.000Z", "max_issues_repo_path": "src/classes/command/CommandManager.js", "max_issues_repo_name": "Les-Laboratoires-JS/pancakes-handler", "max_issues_repo_issues_event_min_datetime": "2020-01-21T22:22:07.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-02T11:52:23.000Z", "max_forks_repo_path": "src/classes/command/CommandManager.js", "max_forks_repo_name": "Les-Laboratoires-JS/pancakes-handler", "max_forks_repo_forks_event_min_datetime": "2020-01-31T22:04:26.000Z", "max_forks_repo_forks_event_max_datetime": "2020-01-31T22:04:26.000Z"}
{"max_stars_count": 3, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 28.6037735849, "max_line_length": 99, "alphanum_fraction": 0.6147757256}
const readRecursive = require('../tools/readRecursive.js')
module.exports = class CommandManager extends Map {
  constructor(instance, ...args) {
    super(...args);
    this._instance = instance
  }
  async fetch(folder) {
    const files = await readRecursive(folder)
    files.children.forEach(file => {
      if (file.type === "file") {
        if (!file.endsWith('.js')) {
          return this.addCommand({
            path: file.path,
            command: require(file.path)
          })
          delete require.cache[file.path]
        }
      } else {
        this.fetch(file)
      }
    })
  }
  addCommand(command) {
    const c = new command.command({
      instance: this._instance
    })
    if (this.get(c.props.name)) {
      return console.log(`[ERROR] Command ${command.path} name is already taken (${c.props.name})`)
    }
    if (!c.run) {
      return console.log(`[ERROR] Command ${command.path} doesnt have run method`)
    }
    this.set(c.props.name, {
      command: command.command,
      path: command.path,
      name: c.props.name
    })
  }
  getCommand(nameOrAlias) {
    return this.find(c => c.name === nameOrAlias || c.aliases.includes(nameOrAlias))
  }
  removeCommand(nameOrAlias) {
    const command = this.getCommand(nameOrAlias)
    this.delete(nameOrAlias)
    return command
  }
  reloadCommand(nameOrAlias) {
    const command = this.removeCommand(nameOrAlias)
    delete require.cache[command.path]
    command.command = require(command.path)
    this.add(command)
  }
}