Source: core/commandContext.js

/**
 * @class
 * @classdesc
 * The CommandContext is used to provide a command with additional information
 * that it requires in order to fullfill its task successfully.
 * You can use a server request as commandContext or create it manually,
 * by adding your own parameters via the addParameter method.
 * @author ruckola
 * @property {object} [parameters={}] - Contains the key-value pairs that are needed to run a command.
 * @property {string} [parameters.action = "macro"] - The name of the command to be invoked, sets {@link CommandContext#action}.
 * @property {boolean} [parameters.macroID = false] - The id of the macro as specified in the config.json file.
 * @property {boolean} [parameters.request = false] - A Server request (optional) {@link CommandContext#request}.
 * @property {boolean} [parameters.userConfigFile = false] - The absolute path to the config.json file of your project.<br>
 * (this filepath must be specified as a relative path in the config.json of the macro-command package. You can find it here: "./node_modules/macro-command/config/config.json".
 * @property {boolean} [parameters.userCommandsRootDir = false] - The path to the root of your 'commands' directory.<br> (this path must
 * be specified as a relative path in the config.json of the macro-command package. You can find it here: "./node_modules/macro-command/config/config.json".
 * @property {boolean} [parameters.userHelperDir = false] - The path to the helper directory.<br> (this path must
 * be specified as a relative path in the config.json of the macro-command package. You can find it here: "./node_modules/macro-command/config/config.json".
 * @property {boolean} [parameters.userConfiguration = false] - The content of the userConfigFile. 
 */
class CommandContext {

    /**
     * @constructor
     * @param {object} request - A server request object with an 'action' member.
     * for example {action:"myCommand"}
     * @returns parameters
     * @description The CommandContext is an object that is passed as a parameter
     * to each new command at instantiation. You can add new parameters to fit your own needs.
     */
    constructor(request = undefined) {

        /**
         * @name CommandContext#parameters
         * @type Object
         * @default Null
         */
        this.parameters = {};

        /**
         * @name CommandContext#action
         * @type string
         * @default "macro"
         */
        this.parameters.action = "macro";

        /**
         * @name CommandContext#macroID
         * @type Number
         * @default False
         */
        this.parameters.macroID = false;

        if (typeof request !== 'undefined' &&
            Object.prototype.toString.call(request) === '[object Object]') {
            
            /**
             * @name CommandContext#request
             * @type Object
             * @default false
             */
            this.parameters.request = request;
        } else {
            this.parameters.request = false;
        }

        /**
         * @name CommandContext#userConfigFile
         * @type string
         * @default false
         */
        this.parameters.userConfigFile = false;

        /**
         * @name CommandContext#userCommandsRootDir
         * @type string
         * @default false
         */
        this.parameters.userCommandsRootDir = false;

        /**
         * @name CommandContext#userHelperDir
         * @type string
         * @default false
         */
        this.parameters.userHelperDir = false;

        /**
         * @name CommandContext#userConfiguration
         * @type string
         * @default false
         */
        this.parameters.userConfiguration = false;

    }

    /**
     * @method
     * @param {string} key - Name of the new key that will be added to the context parameters.
     * @param {any} value - Any value for the new key;
     * @description Add a new member (key-value pair) to the context parameters.
     */
    addParameter(key, value) {
        this.parameters[key] = value;
    }

    /**
     * @method
     * @param {string} key - Name of the member to return.
     * @description Add a new member (key-value pair) to the context parameters.
     * @return {value} The value of member[key].
     */
    get(key) {
        return this.parameters[key];
    }
}

module.exports = CommandContext;