# Module: commandBroker

Executes commands by calling the appropriate command-function from the commandLibrary, which will actually be stored functions on the D-Data structure.

Author:
  - Seth Hollingsead
Copyright:
  - Copyright © 2022-… by Seth Hollingsead. All rights reserved
Source:
  - brokers/commandBroker.js, line 1

### Requires

- module:ruleBroker
- module:commandsLibrary
- module:configurator
- module:loggers
- module:data
- module:queue
- module:stack
- haystacks
- @haystacks/constants
- path

### Methods

#### (inner) addClientCommands(clientCommands) → {void}

Merges client defined commands with the system defined commands.

##### Parameters:

Name | Type | Description
--- | --- | ---
`clientCommands` | array.<object> | The client rules that should be merged with the system rules.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 56

##### Returns:

Type
  void

#### (inner) addPluginCommandAliases(pluginName, pluginCommandAliases) → {boolean}

Merges plugin defined command aliases with the system defined command aliases.

##### Parameters:

Name | Type | Description
--- | --- | ---
`pluginName` | string | The name of the current plugin these command aliases belong to.
`pluginCommandAliases` | object | A JSON object that contains the plugin command aliases that should be merged with the system command aliases.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 122

##### Returns:

True or False to indicate if the merge was successful or not.

Type
  boolean

#### (inner) addPluginCommands(pluginName, pluginCommands) → {boolean}

Merges plugin defined commands with the system defined commands.

##### Parameters:

Name | Type | Description
--- | --- | ---
`pluginName` | string | The name of the current plugin these commands belong to.
`pluginCommands` | array.<object> | The plugin commands that should be merged with the system commands.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 76

##### Returns:

True or False to indicate if the merge was successful or not.

Type
  boolean

#### (inner) bootStrapCommands() → {void}

Captures all of the commands string-to-function cal map data in the commandsLibrary and migrates that dat a to the D-data structure. This is important now because we are going to allow the client to define their own commands separate from the system defined commands. So we need a way to merge al the client defined and system defined commands into one location. Then the command broker will execute commands rom the D-Data structure and not the commands library per-say. This will allow the system to expand much more dynamically and even be user-defined & flexible to client needs.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 38

##### Returns:

Type
  void

#### (inner) countMatchingCommandAlias(commandAliasData, commandAliasName) → {integer}

This is a recursive function that searches through all teh command aliases data structures and counts the number of command aliases that match the input alias.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandAliasData` | object | The command alias data that should be searched recursively for the specified command alias.
`commandAliasName` | string | The command alias name/string that should be searched for and counted when matches are found.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 228

##### Returns:

The count of the number of command aliases that match the given input alias.

Type
  integer

#### (inner) executeCommand(commandString) → {array.<boolean, (string|integer|boolean|object|array)>}

Takes a command string with all its associated arguments, data & meta-data. This function will parse all of that out of the command lien variable that is passed in. And finally pass all of that data on the execution of the actual command.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandString` | string | The command to execute along with all the associated command arguments, data & meta-data.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 623

##### Returns:

An array with a boolean True or False value to indicate if the application should exit or not exit, followed by the command output.

Type
  array.<boolean, (string|integer|boolean|object|array)>

#### (inner) getAllCommandAliasData(commandAliasDataStructure) → {array.<string>|boolean}

Recursively gets all of the commands alias data from all levels and flattens them into a single array for printing out to the help command.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandAliasDataStructure` | object | The command alias data structure that should be recursively flattened into a single array for output. If the input is undefined then the main CommandsAliases data structure will be used at the root of the command aliases data hive.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 368

##### Returns:

An array of all the command aliases currently needing to be flattened or a boolean True or False to indicate that a leaf-node has been found by the recursive caller.

Type
  array.<string> | boolean

#### (inner) getCommandArgs(commandString, commandDelimiter) → {array.<(boolean|string|integer)>}

Gets the arguments of the current command.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandString` | string | The command string that should be parsed fro command arguments.
`commandDelimiter` | string | The delimiter that should be used to parse the command line.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 474

##### Returns:

Any array of arguments, some times these might actually be nested command calls.

Type
  array.<(boolean|string|integer)>

#### (inner) getCommandNamespaceDataObject(commandAliasDataStructure, namespaceToFind) → {object|boolean}

Recursively scans through the entire command alias data structure looking for an object that matches the input namespace name. When that namespace is found, the entire object is returned.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandAliasDataStructure` | object | The command alias data structure that should be recursively searched for the namespace specified. if the input is undefined then the main cCommandsAliases data structure will be used at the root of the CommandAliases data hive.
`namespaceToFind` | string | The namespace to look for in the command alias metaData data structure.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 427

##### Returns:

The namespace object if it is found, or False if the namespace object was not found.

Type
  object | boolean

#### (inner) getValidCommand(commandString, commandDelimiter) → {boolean|string}

Parses the command string and returns an array that can be used to enqueue or execute that command. Useful for determining if a command is a valid command and working with multiple levels of delimiters for nested command calls, looking up a command alias, etc...

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandString` | string | The command string that should be parsed for a valid command.
`commandDelimiter` | string | The delimiter that should be used to parse the command line.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 157

##### Returns:

False if the command is not valid, otherwise it returns the command string.

Type
  boolean | string

#### (inner) searchCommandAlias(commandAliasData, commandAliasName) → {object}

This is a recursive function that searches through all the command aliases data structures and returns the one command data object that matches the input name.

##### Parameters:

Name | Type | Description
--- | --- | ---
`commandAliasData` | object | The command alias data that should be searched recursively for the specified command alias.
`commandAliasName` | string | The command alias name/string that should be found.

Author:
  - Seth Hollingsead
Source:
  - brokers/commandBroker.js, line 294

##### Returns:

The command object that corresponds to the input command alias name.

Type
  object