Source: core/helper.js

/**
 * Created by ruckola on 5-12-16.
 * @module core/helper
 * @author ruckola
 * @description Exports generic functions that can serve multiple (core) classes.
 */

/**
 * @description Converts a relative path (inside your project) into an absolute path.
 * @param {string} relativePath - relative filepath
 * @return {string} absolutePath - absolute path normalized for the current OS.
 */
module.exports.getAbsolutPath = function (relativePath) {
    var fs = require('fs');
    var path = require('path');
    var appRootDir = require('app-root-dir').get();
    var nRelPath = "";
    var absolutePath = "";

    appRootDir += "/";
    appRootDir = path.normalize(appRootDir);
    nRelPath = path.normalize(relativePath);
    absolutePath = path.join(appRootDir, nRelPath); 

    return absolutePath;
}




/**
 * @description Converts the first character of a string to uppercase.
 * @param {string} str - string to be converted
 */
module.exports.ucfirst = function (str) {
    str += '';
    var f = str.charAt(0)
        .toUpperCase();
    return f + str.substr(1);
}

/**
 * @param {boolean} [testMode=false] - if set to true, the configuration for the test-environment will be loaded,
 * instead of the user configuration.
 * @description Reads the modules config.json file and returns the content as
 * object. Any errors will be logged to the console.
 * (runs in sync | blocking code)
 * @return {Object} result - an object with three properties. See example output.
 * @example <caption>Sample result object</caption>
 * {
 *       userConfigFile: "/home/username/project/bin/mc/config/config.json",
 *       userHelperDir: "/home/username/project/bin/mc/helper",
 *       userCommandsRootDir: "/home/username/project/bin/mc/commands"
 * }
 *  
 */
module.exports.getModConfig = function (testMode = false) {

    var result = {};
    if (testMode) {

        result = {
            userConfigFile: "../../test/testEnv/bin/mc/config/config.json",
            userHelperDir: "./test/testEnv/bin/mc/helper",
            userCommandsRootDir: "./test/testEnv/bin/mc/commands"
        };
        return result;
    }

    var fs = require('fs');
    var path = require('path');
    var process = require('process');
    var appRootDir = require('app-root-dir').get();
    var modCfg = '';
    var moduleConfigFile = require('../../config/config.json');
    var err_msg = "Configuration Error! - Something went wrong while trying to read the configuration file "
        + "'config.json' for the macro module!\n"
        + "Please, check the file for errors or missing parameters.";

    if (!(moduleConfigFile.userConfigFile) ||
        !(moduleConfigFile.userHelperDir) ||
        !(moduleConfigFile.userCommandsRootDir)) {
        console.error(err_msg);
    }

    appRootDir += "/";
    appRootDir = path.normalize(appRootDir);
    var usrCfgFile = path.normalize(moduleConfigFile.userConfigFile);
    var usrHlpDir = path.normalize(moduleConfigFile.userHelperDir);
    var usrCmdDir = path.normalize(moduleConfigFile.userCommandsRootDir);

    var usrCfgFile = path.join(appRootDir, usrCfgFile);
    var usrHlpDir = path.join(appRootDir, usrHlpDir);
    var usrCmdDir = path.join(appRootDir, usrCmdDir);

    if (!(fs.existsSync(usrCfgFile))) {
        console.error("Configuration Error! - The user config file cannot be found: \n"
            + usrCfgFile);
    }

    if (!(fs.existsSync(usrHlpDir))) {
        console.error("Configuration Error! - The user helper directory cannot be found: \n"
            + usrHlpDir);
    }

    if (!(fs.existsSync(usrCmdDir))) {
        console.error("Configuration Error! - The user commands root directory cannot be found: \n"
            + usrCmdDir);
    }

    var result = {
        userConfigFile: usrCfgFile,
        userHelperDir: usrHlpDir,
        userCommandsRootDir: usrCmdDir
    };

    return result;

}

module.exports.validateUserConfiguration = function (userConfiguration, macroID) {
    //needs implementation
}

module.exports.getMacroConfiguration = function (userConfiguration, macroID) {

    return new Promise(function (resolve, reject) {
        for (var i = 0; i < userConfiguration.macros.length; i++) {
            var obj = userConfiguration.macros[i];
            if (obj.id == macroID) {
                resolve(obj)
            }
        }

        try {
            throw new Error("The macro with id [" + macroID + "] was not found in the user configuration file.")
        } catch (error) {
            reject(error);
        }
    })
}