/** * Python subprocess module for TypeScript * * Provides functions to spawn new processes and connect to their I/O. * * @see {@link https://docs.python.org/3/library/subprocess.html | Python subprocess documentation} * @module */ /** * Special constant to indicate that a pipe should be created. */ declare const PIPE: "pipe"; /** * Special constant to indicate that the subprocess's stderr should be redirected to stdout. */ declare const STDOUT: "stdout"; /** * Special constant to indicate that output should be discarded. */ declare const DEVNULL: "devnull"; /** * Result of a completed process. */ interface CompletedProcess { /** The arguments used to launch the process */ args: string[]; /** Exit status of the process */ returncode: number; /** Captured stdout (if PIPE was used) */ stdout: string | null; /** Captured stderr (if PIPE was used) */ stderr: string | null; } /** * Options for subprocess functions. */ interface SubprocessOptions { /** Working directory for the command */ cwd?: string; /** Environment variables */ env?: Record; /** Input to send to stdin */ input?: string | Uint8Array; /** How to handle stdout */ stdout?: typeof PIPE | typeof DEVNULL | null; /** How to handle stderr */ stderr?: typeof PIPE | typeof STDOUT | typeof DEVNULL | null; /** Timeout in milliseconds */ timeout?: number; /** Shell to use (if true, uses default shell) */ shell?: boolean | string; /** Encoding for text mode (default: utf-8) */ encoding?: BufferEncoding; /** Whether to capture output as text (default: true) */ text?: boolean; /** Whether to raise CalledProcessError on non-zero exit */ check?: boolean; } /** * Error thrown when a subprocess returns a non-zero exit code. */ declare class CalledProcessError extends Error { readonly returncode: number; readonly cmd: string[]; readonly stdout: string | null; readonly stderr: string | null; constructor(returncode: number, cmd: string[], stdout?: string | null, stderr?: string | null); } /** * Error thrown when a subprocess times out. */ declare class TimeoutExpired extends Error { readonly cmd: string[]; readonly timeout: number; readonly stdout: string | null; readonly stderr: string | null; constructor(cmd: string[], timeout: number, stdout?: string | null, stderr?: string | null); } /** * Run a command and wait for it to complete. * * @param args - Command and arguments (or a single string if shell=true) * @param options - Subprocess options * @returns CompletedProcess with exit code and captured output * * @example * ```typescript * // Run a simple command * const result = run(["ls", "-la"]) * * // Run with shell * const result = run("echo hello && echo world", { shell: true }) * * // Capture output * const result = run(["git", "status"], { stdout: PIPE }) * console.log(result.stdout) * * // Check for errors * const result = run(["false"], { check: true }) // Throws CalledProcessError * ``` */ declare function run(args: string[] | string, options?: SubprocessOptions): CompletedProcess; /** * Run a command and return its exit code. * * @param args - Command and arguments * @param options - Subprocess options * @returns Exit code */ declare function call(args: string[] | string, options?: SubprocessOptions): number; /** * Run a command and raise CalledProcessError if it returns non-zero. * * @param args - Command and arguments * @param options - Subprocess options * @returns Exit code (always 0 if no exception) */ declare function checkCall(args: string[] | string, options?: SubprocessOptions): number; /** * Run a command and return its output, raising CalledProcessError if it returns non-zero. * * @param args - Command and arguments * @param options - Subprocess options * @returns Captured stdout */ declare function checkOutput(args: string[] | string, options?: SubprocessOptions): string; /** * Get the output of a shell command (convenience function). * * @param cmd - Shell command to run * @returns Command output as string */ declare function getoutput(cmd: string): string; /** * Get the exit status and output of a shell command. * * @param cmd - Shell command to run * @returns Tuple of [exit_status, output] */ declare function getstatusoutput(cmd: string): [number, string]; /** * A subprocess.Popen-like class for more control over process execution. */ declare class Popen { readonly args: string[]; private readonly process; private _returncode; private _stdout; private _stderr; constructor(args: string[] | string, options?: SubprocessOptions); /** * Wait for the process to complete. */ wait(): Promise; /** * Get the exit code (or null if still running). */ poll(): number | null; /** * Communicate with the process and return stdout/stderr. */ communicate(input?: string | Uint8Array): Promise<[string | null, string | null]>; /** * Send a signal to the process. */ send_signal(signal: number | NodeJS.Signals): boolean; /** * Terminate the process. */ terminate(): boolean; /** * Kill the process. */ kill(): boolean; /** * The process ID. */ get pid(): number | undefined; /** * The exit code (or null if still running). */ get returncode(): number | null; /** * The captured stdout. */ get stdout(): string | null; /** * The captured stderr. */ get stderr(): string | null; } type subprocessModule_CalledProcessError = CalledProcessError; declare const subprocessModule_CalledProcessError: typeof CalledProcessError; type subprocessModule_CompletedProcess = CompletedProcess; declare const subprocessModule_DEVNULL: typeof DEVNULL; declare const subprocessModule_PIPE: typeof PIPE; type subprocessModule_Popen = Popen; declare const subprocessModule_Popen: typeof Popen; declare const subprocessModule_STDOUT: typeof STDOUT; type subprocessModule_SubprocessOptions = SubprocessOptions; type subprocessModule_TimeoutExpired = TimeoutExpired; declare const subprocessModule_TimeoutExpired: typeof TimeoutExpired; declare const subprocessModule_call: typeof call; declare const subprocessModule_checkCall: typeof checkCall; declare const subprocessModule_checkOutput: typeof checkOutput; declare const subprocessModule_getoutput: typeof getoutput; declare const subprocessModule_getstatusoutput: typeof getstatusoutput; declare const subprocessModule_run: typeof run; declare namespace subprocessModule { export { subprocessModule_CalledProcessError as CalledProcessError, type subprocessModule_CompletedProcess as CompletedProcess, subprocessModule_DEVNULL as DEVNULL, subprocessModule_PIPE as PIPE, subprocessModule_Popen as Popen, subprocessModule_STDOUT as STDOUT, type subprocessModule_SubprocessOptions as SubprocessOptions, subprocessModule_TimeoutExpired as TimeoutExpired, subprocessModule_call as call, subprocessModule_checkCall as checkCall, subprocessModule_checkOutput as checkOutput, subprocessModule_getoutput as getoutput, subprocessModule_getstatusoutput as getstatusoutput, subprocessModule_run as run }; } export { CalledProcessError as C, DEVNULL as D, PIPE as P, STDOUT as S, TimeoutExpired as T, type CompletedProcess as a, Popen as b, type SubprocessOptions as c, call as d, checkCall as e, checkOutput as f, getoutput as g, getstatusoutput as h, run as r, subprocessModule as s };