Command Decorator
This file defines the Command
decorator, which is used to mark class methods as script commands in the @sodacore/core
library. It attaches metadata to methods, making them accessible as commands with optional descriptions and options.
Features
- Command Registration: Marks methods as commands that can be executed by the script system.
- Metadata Support: Allows attaching descriptions and options to commands for better documentation and usage.
- Automatic Collection: Collects all decorated methods into a metadata array for later retrieval.
Usage
typescript
import Command from './decorator/command';
class MyScript {
@Command({ description: 'Say hello' })
hello() {
console.log('Hello!');
}
}
- Only methods decorated with
@Command
will be registered as script commands. - Classes without any
@Command
-decorated methods will not be loaded as script commands.
API
Command(options?: ICommandOptions): MethodDecorator
- options: Optional metadata for the command, such as description and options.
- Returns: A method decorator that registers the method as a command.
How It Works
- Retrieves the existing commands metadata from the target.
- Adds the new command (with its name, function, and options) to the metadata array.
- Updates the metadata on the target with the new array of commands.
Notes
- Use this decorator on all methods you want to expose as commands in your script system.
- The decorator relies on the
Utils.getMeta
andUtils.setMeta
helpers for metadata management.