# new StandardOutputWriter(cmd, lock, args)
This class implements the OutputWriter interface and provides methods for handling various types of output from command execution, including standard output, error output, and exit codes. It also includes utility methods for parsing commands and resolving or rejecting promises based on execution results.
A standard output writer for handling command execution output.
Parameters:
Name | Type | Description |
---|---|---|
cmd |
The command string to be executed. |
|
lock |
A PromiseExecutor to control the asynchronous flow. |
|
args |
Additional arguments (unused in the current implementation). |
Example
```typescript
import { StandardOutputWriter } from '@decaf-ts/utils';
import { PromiseExecutor } from '@decaf-ts/utils';
// Create a promise executor
const executor: PromiseExecutor<string> = {
resolve: (value) => console.log(`Resolved: ${value}`),
reject: (error) => console.error(`Rejected: ${error.message}`)
};
// Create a standard output writer
const writer = new StandardOutputWriter('ls -la', executor);
// Use the writer to handle command output
writer.data('File list output...');
writer.exit(0, ['Command executed successfully']);
```
Classes
- StandardOutputWriter
This class implements the OutputWriter interface and provides methods for handling various types of output from command execution, including standard output, error output, and exit codes. It also includes utility methods for parsing commands and resolving or rejecting promises based on execution results.
Methods
# data(chunk)
Logs the given chunk as standard output.
Handles standard output data.
Parameters:
Name | Type | Description |
---|---|---|
chunk |
The data chunk to be logged. |
# error(chunk)
Logs the given chunk as error output.
Handles error output data.
Parameters:
Name | Type | Description |
---|---|---|
chunk |
The error data chunk to be logged. |
# errors(err)
Logs the error message from the given Error object.
Handles error objects.
Parameters:
Name | Type | Description |
---|---|---|
err |
The Error object to be logged. |
# exit(code, logs)
Logs the exit code and resolves or rejects the promise based on the code.
Handles the exit of a command.
Parameters:
Name | Type | Description |
---|---|---|
code |
The exit code of the command. |
|
logs |
Array of log messages to be processed before exiting. |
# protected log(type, data)
Formats and logs the given data with a timestamp and type indicator.
Logs output to the console.
Parameters:
Name | Type | Description |
---|---|---|
type |
The type of output (stdout or stderr). |
|
data |
The data to be logged. |
# parseCommand(command)
Converts the command into a consistent format and stores it, then returns it split into command and arguments.
Parses a command string or array into components.
Parameters:
Name | Type | Description |
---|---|---|
command |
The command as a string or array of strings. |
A tuple containing the command and its arguments as separate elements.
# protected reject(reason)
Logs an error message and rejects the promise with the given reason.
Rejects the promise with an error message.
Parameters:
Name | Type | Description |
---|---|---|
reason |
The reason for rejecting the promise, either a number (exit code) or a string. |
# protected resolve(reason)
Logs a success message and resolves the promise with the given reason.
Resolves the promise with a success message.
Parameters:
Name | Type | Description |
---|---|---|
reason |
The reason for resolving the promise. |