Home Reference Source
import CommandRegistry from 'ghastly/src/command/CommandRegistry.js'
public class | source

CommandRegistry

Class handling storage and access control for commands.

Constructor Summary

Public Constructor
public

Constructor.

Member Summary

Private Members
private

aliases: StringMap<string>

The command aliases stored in this registry.

private

commands: StringMap<CommandObject>

The commands stored in this registry.

private

groups: StringMap<CommandGroup>

The command groups stored in the registry.

Method Summary

Public Methods
public

add(configurators: ...commandConfigurator): CommandRegistry

Adds the given commands to the registry.

public

applyGroupMiddleware(group: string, middleware: middlewareLayer[]): CommandRegistry

Applies middleware to a command group.

public

get(identifier: string): CommandObject

Gets a command from the registry.

Private Methods
private

Adds a command to the registry.

private

alias(name: string, aliases: string[]): CommandRegistry

Adds command aliases to the registry.

private

Returns the main name associated with an identifier.

Public Constructors

public constructor() source

Constructor.

Private Members

private aliases: StringMap<string> source

The command aliases stored in this registry.

private commands: StringMap<CommandObject> source

The commands stored in this registry.

private groups: StringMap<CommandGroup> source

The command groups stored in the registry.

Public Methods

public add(configurators: ...commandConfigurator): CommandRegistry source

Adds the given commands to the registry.

Params:

NameTypeAttributeDescription
configurators ...commandConfigurator

The command configurators.

Return:

CommandRegistry

The instance this method was called on.

public applyGroupMiddleware(group: string, middleware: middlewareLayer[]): CommandRegistry source

Applies middleware to a command group.

Params:

NameTypeAttributeDescription
group string

The command group to apply middleware to.

middleware middlewareLayer[]

The middleware to apply.

Return:

CommandRegistry

The instance this method was called on.

Throw:

CommandError

Thrown if the given group does not exist.

TypeError

Thrown if middleware is not an array.

public get(identifier: string): CommandObject source

Gets a command from the registry.

Params:

NameTypeAttributeDescription
identifier string

The identifier of the command in the registry, or an alias of a command.

Return:

CommandObject (nullable: true)

The CommandObject in the registry, or null if it couldn't be found.

Throw:

TypeError

Thrown if the given command name is not a string.

Private Methods

private addCommand(command: CommandObject): CommandRegistry source

Adds a command to the registry.

Params:

NameTypeAttributeDescription
command CommandObject

The command to register.

Return:

CommandRegistry

The instance this method was called on.

Throw:

CommandError

Thrown if a duplicate command name or alias is found.

TypeError

Thrown if the given command is not a CommandObject instance.

private alias(name: string, aliases: string[]): CommandRegistry source

Adds command aliases to the registry.

Params:

NameTypeAttributeDescription
name string

The name of the command to add aliases to.

aliases string[]

The command aliases.

Return:

CommandRegistry

The instance this method was called on.

Throw:

CommandError

Thrown if given command name doesn't exist in the registry, or if a duplicate alias is detected.

TypeError

Thrown if the given name and aliases are not strings.

private getMainName(identifier: string): string | undefined source

Returns the main name associated with an identifier.

Params:

NameTypeAttributeDescription
identifier string

The identifier.

Return:

string | undefined

The main name associated with this identifier, or undefined if none exists.

Throw:

TypeError

Thrown if the given command identifier is not a string.