CoreScripts
This file defines the CoreScripts
class, which provides core CLI script commands for the @sodacore/core
library. These scripts allow users to watch logs in real-time, view system and heap usage statistics, and manually run tasks from the CLI. The class is registered under the core
namespace and uses dependency injection for logger and task management.
Features
- Log Watching: Stream log output to the CLI in real-time.
- Usage Information: Display detailed system and heap memory statistics.
- Task Execution: List and manually run available tasks from the CLI.
- Dependency Injection: Integrates with the Sodacore DI system for logger and task provider access.
- Script Decorators: Uses
@Namespace
and@Script
decorators for script registration and routing.
Usage
These scripts are intended to be run via the Sodacore CLI interface.
import CoreScripts from './script/core';
// Example usage in the CLI:
// > core:logs:watch
// > core:usage:info
// > core:task:run
API
Methods
@Script('logs:watch') async watchLogs(context: ScriptContext): Promise<void>
Streams log messages to the CLI in real-time. Adds a listener to the logger and removes it when the socket exits.
@Script('usage:info') async usage(): Promise<string>
Returns a formatted string with system memory and heap usage statistics, including heap size, object counts, and memory usage.
@Script('task:run') async taskRun(context: ScriptContext): Promise<boolean>
Prompts the user to select a task to run from the list of available tasks. Validates the selection and executes the chosen task, providing feedback via the CLI.
How It Works
- Namespace Registration: The class is registered under the
core
namespace using the@Namespace('core')
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 theTasksProvider
andLogger
instances. - Context Usage: Script methods receive a
ScriptContext
object, providing access to logging, prompts, and socket management.
Notes
- The
watchLogs
method streams logs until the CLI connection is closed. - The
usage
method provides a snapshot of current system and heap statistics. - The
taskRun
method allows interactive task selection and execution from the CLI. - All scripts are accessible via the CLI using the
core
namespace and their registered command names.