@decaf-ts/utils
Version:
module management utils for decaf-ts
92 lines (91 loc) • 3.73 kB
TypeScript
import { OutputWriter } from "./OutputWriter";
import { PromiseExecutor } from "../utils/types";
import { OutputType } from "./types";
import { Logger } from "@decaf-ts/logging";
/**
* @description A standard output writer for handling command execution output.
* @summary 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.
*
* @template R - The type of the resolved value, defaulting to number.
*
* @param lock - A PromiseExecutor to control the asynchronous flow.
* @param args - Additional arguments (unused in the current implementation).
*
* @class
*/
export declare class StandardOutputWriter<R = string> implements OutputWriter {
protected cmd: string;
protected lock: PromiseExecutor<R>;
protected logger: Logger;
/**
* @description Initializes a new instance of StandardOutputWriter.
* @summary Constructs the StandardOutputWriter with a lock mechanism and optional arguments.
*
* @param cmd
* @param lock - A PromiseExecutor to control the asynchronous flow.
* @param args - Additional arguments (currently unused).
*/
constructor(cmd: string, lock: PromiseExecutor<R>, ...args: unknown[]);
/**
* @description Logs output to the console.
* @summary Formats and logs the given data with a timestamp and type indicator.
*
* @param type - The type of output (stdout or stderr).
* @param data - The data to be logged.
*/
protected log(type: OutputType, data: string | Buffer): void;
/**
* @description Handles standard output data.
* @summary Logs the given chunk as standard output.
*
* @param chunk - The data chunk to be logged.
*/
data(chunk: any): void;
/**
* @description Handles error output data.
* @summary Logs the given chunk as error output.
*
* @param chunk - The error data chunk to be logged.
*/
error(chunk: any): void;
/**
* @description Handles error objects.
* @summary Logs the error message from the given Error object.
*
* @param err - The Error object to be logged.
*/
errors(err: Error): void;
/**
* @description Handles the exit of a command.
* @summary Logs the exit code and resolves or rejects the promise based on the code.
*
* @param code - The exit code of the command.
* @param logs
*/
exit(code: number | string, logs: string[]): void;
/**
* @description Parses a command string or array into components.
* @summary Converts the command into a consistent format and stores it, then returns it split into command and arguments.
*
* @param command - The command as a string or array of strings.
* @return A tuple containing the command and its arguments as separate elements.
*/
parseCommand(command: string | string[]): [string, string[]];
/**
* @description Resolves the promise with a success message.
* @summary Logs a success message and resolves the promise with the given reason.
*
* @param reason - The reason for resolving the promise.
*/
protected resolve(reason: R): void;
/**
* @description Rejects the promise with an error message.
* @summary Logs an error message and rejects the promise with the given reason.
*
* @param reason - The reason for rejecting the promise, either a number (exit code) or a string.
*/
protected reject(reason: number | string | Error): void;
}