Class

StandardOutputWriter

StandardOutputWriter(cmd, lock, args)

Constructor

# 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.

sequenceDiagram participant Client participant StandardOutputWriter participant Logger participant PromiseExecutor Client->>StandardOutputWriter: new StandardOutputWriter(cmd, lock) StandardOutputWriter->>Logger: Logging.for(cmd) Client->>StandardOutputWriter: data(chunk) StandardOutputWriter->>StandardOutputWriter: log("stdout", chunk) StandardOutputWriter->>Logger: logger.info(log) Client->>StandardOutputWriter: error(chunk) StandardOutputWriter->>StandardOutputWriter: log("stderr", chunk) StandardOutputWriter->>Logger: logger.info(log) Client->>StandardOutputWriter: exit(code, logs) StandardOutputWriter->>StandardOutputWriter: log("stdout", exitMessage) alt code === 0 StandardOutputWriter->>StandardOutputWriter: resolve(logs) StandardOutputWriter->>PromiseExecutor: lock.resolve(reason) else code !== 0 StandardOutputWriter->>StandardOutputWriter: reject(error) StandardOutputWriter->>PromiseExecutor: lock.reject(reason) end
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).

View Source writers/StandardOutputWriter.ts, line 66

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.

View Source writers/StandardOutputWriter.ts, line 193

# 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.

View Source writers/StandardOutputWriter.ts, line 201

# 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.

View Source writers/StandardOutputWriter.ts, line 209

# 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.

View Source writers/StandardOutputWriter.ts, line 218

# 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.

View Source writers/StandardOutputWriter.ts, line 185

# 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.

View Source writers/StandardOutputWriter.ts, line 227

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.

View Source writers/StandardOutputWriter.ts, line 245

# 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.

View Source writers/StandardOutputWriter.ts, line 236