/**
 * @file Command Strings Generator
 * @author Sankarsan Kampa (a.k.a k3rn31p4nic)
 * @license GPL-3.0
 */

const fs = require('fs');
const path = require('path');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);

/* eslint-disable no-sync */
let commands = {};

let modules = fs.readdirSync('./commands/').filter(file => fs.statSync(path.join('./commands/', file)).isDirectory());

for (let module of modules) {
  let commandFiles = fs.readdirSync(path.resolve(`./commands/${module}`)).
    filter(file => !fs.statSync(path.resolve('./commands/', module, file)).isDirectory()).
    filter(file => file.endsWith('.js'));

  for (let file of commandFiles) {
    file = file.substr(0, file.length - 3);

    file = require(path.resolve('./commands/', module, file));

    commands[file.help.name] = {
      description: file.help.description,
      module: module
    };
  }
}


let keys = Object.keys(commands);
keys.sort();
let sortedCommands = {};
for (let key of keys) {
  sortedCommands[key] = commands[key];
}


/* eslint-disable no-console */
// Generate commands file that will be used by the website to list commands
writeFile(path.resolve('./data', 'commands.json'), JSON.stringify(sortedCommands, null, 2)).
  then(() => console.log('Successfully generated command list for website.')).
  catch(console.error);

// Generate commands file that will be used by Bastion's String Handler
for (let command in sortedCommands) {
  if (sortedCommands.hasOwnProperty(command)) {
    delete sortedCommands[command].module;
  }
}
writeFile(path.resolve('./locales', 'en_us', 'command.json'), JSON.stringify(sortedCommands, null, 2)).
  then(() => console.log('Successfully generated command list for string handler.')).
  catch(console.error);

@@ -880,22 +880,53 @@
 module: 
-module
+toTitleCase(module.replace('_', ' '))
 %0A    %7D;%0A
@@ -1778,28 +1778,504 @@
 )).%0A  catch(console.error);%0A
+%0A%0A/**%0A * Convert a string to Title Case.%0A * @function toTitleCase%0A * @param %7BString%7D string the string to be converted%0A * @returns %7BString%7D the input string in Title Case%0A */%0Afunction toTitleCase(string) %7B%0A  let newstr = string.split(' ');%0A  for (let i = 0; i %3C newstr.length; i++) %7B%0A    if (newstr%5Bi%5D === '') continue;%0A    let copy = newstr%5Bi%5D.substring(1).toLowerCase();%0A    newstr%5Bi%5D = newstr%5Bi%5D%5B0%5D.toUpperCase() + copy;%0A  %7D%0A  newstr = newstr.join(' ');%0A  return newstr;%0A%7D%0A

update generateCommandStrings util function