main
1/// <reference types="node" />
2/// <reference types="fs-extra" />
3
4import { Buffer } from 'node:buffer';
5import cp, { type ChildProcess, type IOType, type StdioOptions } from 'node:child_process';
6import { type Encoding } from 'node:crypto';
7import { EventEmitter } from 'node:events';
8import { type Readable, type Writable } from 'node:stream';
9import { inspect } from 'node:util';
10import { Fail } from './error.js';
11import { log } from './log.js';
12import { type TSpawnStore } from './vendor-core.js';
13import { type Duration, quote } from './util.js';
14export { bus } from './internals.js';
15export { default as path } from 'node:path';
16export * as os from 'node:os';
17export { Fail } from './error.js';
18export { log, type LogEntry } from './log.js';
19export { chalk, which, ps } from './vendor-core.js';
20export { type Duration, quote, quotePowerShell } from './util.js';
21declare const CWD: unique symbol;
22declare const SYNC: unique symbol;
23declare const SHOT: unique symbol;
24export interface Options {
25 [CWD]: string;
26 [SYNC]: boolean;
27 cwd?: string;
28 ac?: AbortController;
29 signal?: AbortSignal;
30 input?: string | Buffer | Readable | ProcessOutput | ProcessPromise;
31 timeout?: Duration;
32 timeoutSignal?: NodeJS.Signals;
33 stdio: StdioOptions;
34 verbose: boolean;
35 sync: boolean;
36 env: NodeJS.ProcessEnv;
37 shell: string | true;
38 nothrow: boolean;
39 prefix?: string;
40 postfix?: string;
41 quote?: typeof quote;
42 quiet: boolean;
43 detached: boolean;
44 preferLocal: boolean | string | string[];
45 spawn: typeof cp.spawn;
46 spawnSync: typeof cp.spawnSync;
47 store?: TSpawnStore;
48 log: typeof log;
49 kill: typeof kill;
50 killSignal?: NodeJS.Signals;
51 halt?: boolean;
52 delimiter?: string | RegExp;
53}
54type Snapshot = Options & {
55 from: string;
56 pieces: TemplateStringsArray;
57 args: string[];
58 cmd: string;
59 ee: EventEmitter;
60 ac: AbortController;
61};
62export declare const defaults: Options;
63export declare function within<R>(callback: () => R): R;
64export interface Shell<S = false, R = S extends true ? ProcessOutput : ProcessPromise> {
65 (pieces: TemplateStringsArray, ...args: any[]): R;
66 <O extends Partial<Options> = Partial<Options>, R = O extends {
67 sync: true;
68 } ? Shell<true> : Shell>(opts: O): R;
69 sync: {
70 (pieces: TemplateStringsArray, ...args: any[]): ProcessOutput;
71 (opts: Partial<Omit<Options, 'sync'>>): Shell<true>;
72 };
73}
74export type $ = Shell & Options;
75export declare const $: $;
76type ProcessStage = 'initial' | 'halted' | 'running' | 'fulfilled' | 'rejected';
77type Resolve = (out: ProcessOutput) => void;
78type Reject = (error: ProcessOutput | Error) => void;
79type PromiseCallback = {
80 (resolve: Resolve, reject: Reject): void;
81 [SHOT]?: Snapshot;
82};
83type PromisifiedStream<D extends Writable = Writable> = D & PromiseLike<ProcessOutput & D> & {
84 run(): void;
85};
86type PipeAcceptor = Writable | ProcessPromise;
87type PipeMethod = {
88 (dest: TemplateStringsArray, ...args: any[]): ProcessPromise;
89 (file: string): PromisifiedStream;
90 <D extends Writable>(dest: D): PromisifiedStream<D>;
91 <D extends ProcessPromise>(dest: D): D;
92};
93export declare class ProcessPromise extends Promise<ProcessOutput> {
94 private _stage;
95 private _id;
96 private _snapshot;
97 private _timeoutId?;
98 private _piped;
99 private _stdin;
100 private _zurk;
101 private _output;
102 private _resolve;
103 private _reject;
104 constructor(executor: PromiseCallback);
105 private build;
106 run(): this;
107 private _breakerData?;
108 private break;
109 private finalize;
110 abort(reason?: string): void;
111 kill(signal?: NodeJS.Signals | null): Promise<void>;
112 stdio(stdin: IOType | StdioOptions, stdout?: IOType, stderr?: IOType): this;
113 nothrow(v?: boolean): this;
114 quiet(v?: boolean): this;
115 verbose(v?: boolean): this;
116 timeout(d?: Duration, signal?: NodeJS.Signals | undefined): this;
117 /**
118 * @deprecated Use $({halt: true})`cmd` instead.
119 */
120 halt(): this;
121 get id(): string;
122 get pid(): number | undefined;
123 get cmd(): string;
124 get fullCmd(): string;
125 get child(): ChildProcess | undefined;
126 get stdin(): Writable;
127 get stdout(): Readable;
128 get stderr(): Readable;
129 get exitCode(): Promise<number | null>;
130 get signal(): AbortSignal;
131 get ac(): AbortController;
132 get output(): ProcessOutput | null;
133 get stage(): ProcessStage;
134 get sync(): boolean;
135 get [Symbol.toStringTag](): string;
136 [Symbol.toPrimitive](): string;
137 json<T = any>(): Promise<T>;
138 text(encoding?: Encoding): Promise<string>;
139 lines(delimiter?: Options['delimiter']): Promise<string[]>;
140 buffer(): Promise<Buffer>;
141 blob(type?: string): Promise<Blob>;
142 isQuiet(): boolean;
143 isVerbose(): boolean;
144 isNothrow(): boolean;
145 isHalted(): boolean;
146 private isSettled;
147 private isRunning;
148 get pipe(): PipeMethod & {
149 [key in keyof TSpawnStore]: PipeMethod;
150 };
151 unpipe(to?: PipeAcceptor): this;
152 private _pipe;
153 private static bus;
154 private static promisifyStream;
155 then<R = ProcessOutput, E = ProcessOutput>(onfulfilled?: ((value: ProcessOutput) => PromiseLike<R> | R) | undefined | null, onrejected?: ((reason: ProcessOutput) => PromiseLike<E> | E) | undefined | null): Promise<R | E>;
156 catch<T = ProcessOutput>(onrejected?: ((reason: ProcessOutput) => PromiseLike<T> | T) | undefined | null): Promise<ProcessOutput | T>;
157 [Symbol.asyncIterator](): AsyncIterator<string>;
158 private writable;
159 private emit;
160 private on;
161 private once;
162 private write;
163 private end;
164 private removeListener;
165 private static disarm;
166}
167type ProcessDto = {
168 code: number | null;
169 signal: NodeJS.Signals | null;
170 duration: number;
171 error: any;
172 from: string;
173 store: TSpawnStore;
174 delimiter?: string | RegExp;
175};
176export declare class ProcessOutput extends Error {
177 private readonly _dto;
178 cause: Error | null;
179 message: string;
180 stdout: string;
181 stderr: string;
182 stdall: string;
183 constructor(dto: ProcessDto);
184 constructor(code?: number | null, signal?: NodeJS.Signals | null, stdout?: string, stderr?: string, stdall?: string, message?: string, duration?: number);
185 get exitCode(): number | null;
186 get signal(): NodeJS.Signals | null;
187 get duration(): number;
188 get [Symbol.toStringTag](): string;
189 get ok(): boolean;
190 json<T = any>(): T;
191 buffer(): Buffer;
192 blob(type?: string): Blob;
193 text(encoding?: Encoding): string;
194 lines(delimiter?: string | RegExp): string[];
195 toString(): string;
196 valueOf(): string;
197 [Symbol.toPrimitive](): string;
198 [Symbol.iterator](dlmtr?: Options['delimiter']): Iterator<string>;
199 [inspect.custom](): string;
200 static getExitMessage: typeof Fail.formatExitMessage;
201 static getErrorMessage: typeof Fail.formatErrorMessage;
202 static getErrorDetails: typeof Fail.formatErrorDetails;
203 static getExitCodeInfo: typeof Fail.getExitCodeInfo;
204 static fromError(error: Error): ProcessOutput;
205}
206export declare const useBash: () => void;
207export declare const usePwsh: () => void;
208export declare const usePowerShell: () => void;
209export declare function syncProcessCwd(flag?: boolean): void;
210export declare function cd(dir: string | ProcessOutput): void;
211export declare function kill(pid: number | `${number}`, signal?: NodeJS.Signals): Promise<void>;
212export declare function resolveDefaults(defs?: Options, prefix?: string, env?: NodeJS.ProcessEnv, allowed?: Set<string>): Options;