import { type ChildProcess, type SpawnOptionsWithStdioTuple, type StdioPipe } from 'node:child_process'; /** * Represents the process result. */ export type ProcessResult = { /** The full command with args that was executed. */ bin: string; /** The exit code of the process (`null` if terminated by signal). */ code: number | null; /** The signal which terminated the process, if any. */ signal: NodeJS.Signals | null; /** The standard output from the process. */ stdout: string; /** The standard error from the process. */ stderr: string; }; /** * Error class for process errors. * Contains additional information about the process result. * * @example * const result = await executeProcess({ ... }).catch((error) => { * if (error instanceof ProcessError) { * console.error(error.message); * console.error(error.code); * console.error(error.stderr); * console.error(error.stdout); * } * }); * */ export declare class ProcessError extends Error { #private; bin: string; code: number | null; signal: NodeJS.Signals | null; constructor(result: ProcessResult); get stdout(): string; get stderr(): string; } /** * Process config object. Contains the command, args and observer. * @param cfg Process config object with command, args and observer (optional) * @property {string} command - The command to execute. * @property {string[]} args - The arguments for the command. * @property {ProcessObserver} observer - The observer for the process. * * @example * * // bash command * const cfg = { * command: 'bash', * args: ['-c', 'echo "hello world"'] * }; * * // node command * const cfg = { * command: 'node', * args: ['--version'] * }; * * // npx command * const cfg = { * command: 'npx', * args: ['--version'] * }; */ export type ProcessConfig = Omit, 'stdio'> & { command: string; args?: string[]; observer?: ProcessObserver; ignoreExitCode?: boolean; silent?: boolean; }; /** * Process observer object. * * @example * const observer = { * onStdout: (stdout) => console.info(stdout) * } */ export type ProcessObserver = { /** Called when the `stdout` stream receives new data (optional). */ onStdout?: (stdout: string, sourceProcess?: ChildProcess) => void; /** Called when the `stdout` stream receives new data (optional). */ onStderr?: (stderr: string, sourceProcess?: ChildProcess) => void; /** Called when the process ends in an error (optional). */ onError?: (error: ProcessError) => void; /** Called when the process ends successfully (optional). */ onComplete?: () => void; }; /** * Executes a process and returns a promise with the result as `ProcessResult`. * * @example * * // sync process execution * const result = await executeProcess({ * command: 'node', * args: ['--version'] * }); * * console.info(result); * * // async process execution * const result = await executeProcess({ * command: 'node', * args: ['download-data.js'], * observer: { * onStdout: updateProgress, * error: handleError, * complete: cleanLogs, * } * }); * * console.info(result); * * @param cfg - see {@link ProcessConfig} */ export declare function executeProcess(cfg: ProcessConfig): Promise;