DefaultScripts
This file defines the DefaultScripts
class, which provides internal CLI script commands for authentication, command listing, and interactive communication in the @sodacore/core
library. These scripts are registered under the _
namespace and are primarily used for internal CLI operations.
Features
- Authentication: Verifies CLI client credentials and manages authentication state.
- Command Listing: Returns a list of all available script commands, excluding internal commands.
- Interactive Communication: Handles interactive responses between the CLI and the server.
Usage
These scripts are intended for internal use by the Sodacore CLI server and are not typically called directly by users.
import DefaultScripts from './script/default';
// Example internal CLI commands:
// > _:authenticate
// > _:commands
// > _:interact
API
Methods
@Script('authenticate') async authenticate(context: ScriptContext): Promise<void>
Authenticates the CLI client by comparing the provided password with the configured password. Updates the socket's authentication state and sends the result back to the client. If authentication fails, the socket is terminated after a short delay.
@Script('commands') async commands(context: ScriptContext): Promise<void>
Sends a list of all available script commands (excluding those starting with _:
) to the client.
@Script('interact') async interact(context: ScriptContext): Promise<void>
Handles interactive responses by invoking a stored callback on the socket with the provided results, then cleans up the callback reference.
How It Works
- Namespace Registration: The class is registered under the
_
namespace using the@Namespace('_')
decorator. - Script Registration: Each method is registered as a CLI script using the
@Script
decorator with a unique command name. - Dependency Injection: Uses
@Inject
to access the application configuration and theScripts
module. - Context Usage: Script methods receive a
ScriptContext
object, providing access to context data, socket management, and response helpers.
Notes
- The
authenticate
method is used to secure CLI connections before allowing further interaction. - The
commands
method helps clients discover available script commands. - The
interact
method supports asynchronous, interactive CLI workflows. - These scripts are essential for the internal operation of the Sodacore CLI server.