1 | import type {ChildProcess} from 'node:child_process';
|
2 | import type {SignalConstants} from 'node:os';
|
3 | import type {Readable, Writable, Duplex} from 'node:stream';
|
4 | import type {Options} from '../arguments/options.js';
|
5 | import type {Result} from '../return/result.js';
|
6 | import type {PipableSubprocess} from '../pipe.js';
|
7 | import type {
|
8 | ReadableOptions,
|
9 | WritableOptions,
|
10 | DuplexOptions,
|
11 | SubprocessAsyncIterable,
|
12 | } from '../convert.js';
|
13 | import type {IpcMethods, HasIpc} from '../ipc.js';
|
14 | import type {SubprocessStdioStream} from './stdout.js';
|
15 | import type {SubprocessStdioArray} from './stdio.js';
|
16 | import type {SubprocessAll} from './all.js';
|
17 |
|
18 | type ExecaCustomSubprocess<OptionsType extends Options> = {
|
19 | /**
|
20 | Process identifier ([PID](https://en.wikipedia.org/wiki/Process_identifier)).
|
21 |
|
22 | This is `undefined` if the subprocess failed to spawn.
|
23 | */
|
24 | pid?: number;
|
25 |
|
26 | /**
|
27 | The subprocess [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)) as a stream.
|
28 |
|
29 | This is `null` if the `stdin` option is set to `'inherit'`, `'ignore'`, `Readable` or `integer`.
|
30 | */
|
31 | stdin: SubprocessStdioStream<'0', OptionsType>;
|
32 |
|
33 | /**
|
34 | The subprocess [`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) as a stream.
|
35 |
|
36 | This is `null` if the `stdout` option is set to `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false`.
|
37 | */
|
38 | stdout: SubprocessStdioStream<'1', OptionsType>;
|
39 |
|
40 | /**
|
41 | The subprocess [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)) as a stream.
|
42 |
|
43 | This is `null` if the `stderr` option is set to `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false`.
|
44 | */
|
45 | stderr: SubprocessStdioStream<'2', OptionsType>;
|
46 |
|
47 | /**
|
48 | Stream combining/interleaving `subprocess.stdout` and `subprocess.stderr`.
|
49 |
|
50 | This requires the `all` option to be `true`.
|
51 |
|
52 | This is `undefined` if `stdout` and `stderr` options are set to `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false`.
|
53 | */
|
54 | all: SubprocessAll<OptionsType>;
|
55 |
|
56 | /**
|
57 | The subprocess `stdin`, `stdout`, `stderr` and other files descriptors as an array of streams.
|
58 |
|
59 | Each array item is `null` if the corresponding `stdin`, `stdout`, `stderr` or `stdio` option is set to `'inherit'`, `'ignore'`, `Stream` or `integer`, or if the `buffer` option is `false`.
|
60 | */
|
61 | stdio: SubprocessStdioArray<OptionsType>;
|
62 |
|
63 | /**
|
64 | Sends a [signal](https://nodejs.org/api/os.html#signal-constants) to the subprocess. The default signal is the `killSignal` option. `killSignal` defaults to `SIGTERM`, which terminates the subprocess.
|
65 |
|
66 | This returns `false` when the signal could not be sent, for example when the subprocess has already exited.
|
67 |
|
68 | When an error is passed as argument, it is set to the subprocess' `error.cause`. The subprocess is then terminated with the default signal. This does not emit the [`error` event](https://nodejs.org/api/child_process.html#event-error).
|
69 |
|
70 | [More info.](https://nodejs.org/api/child_process.html#subprocesskillsignal)
|
71 | */
|
72 | kill(signal?: keyof SignalConstants | number, error?: Error): boolean;
|
73 | kill(error?: Error): boolean;
|
74 |
|
75 | /**
|
76 | Subprocesses are [async iterables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator). They iterate over each output line.
|
77 | */
|
78 | [Symbol.asyncIterator](): SubprocessAsyncIterable<undefined, OptionsType['encoding']>;
|
79 |
|
80 | /**
|
81 | Same as `subprocess[Symbol.asyncIterator]` except options can be provided.
|
82 | */
|
83 | iterable<IterableOptions extends ReadableOptions = {}>(readableOptions?: IterableOptions): SubprocessAsyncIterable<IterableOptions['binary'], OptionsType['encoding']>;
|
84 |
|
85 | /**
|
86 | Converts the subprocess to a readable stream.
|
87 | */
|
88 | readable(readableOptions?: ReadableOptions): Readable;
|
89 |
|
90 | /**
|
91 | Converts the subprocess to a writable stream.
|
92 | */
|
93 | writable(writableOptions?: WritableOptions): Writable;
|
94 |
|
95 | /**
|
96 | Converts the subprocess to a duplex stream.
|
97 | */
|
98 | duplex(duplexOptions?: DuplexOptions): Duplex;
|
99 | }
|
100 | & IpcMethods<HasIpc<OptionsType>, OptionsType['serialization']>
|
101 | & PipableSubprocess;
|
102 |
|
103 | /**
|
104 | [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with additional methods and properties.
|
105 | */
|
106 | export type Subprocess<OptionsType extends Options = Options> =
|
107 | & Omit<ChildProcess, keyof ExecaCustomSubprocess<OptionsType>>
|
108 | & ExecaCustomSubprocess<OptionsType>;
|
109 |
|
110 | /**
|
111 | The return value of all asynchronous methods is both:
|
112 | - the subprocess.
|
113 | - a `Promise` either resolving with its successful `result`, or rejecting with its `error`.
|
114 | */
|
115 | export type ResultPromise<OptionsType extends Options = Options> =
|
116 | & Subprocess<OptionsType>
|
117 | & Promise<Result<OptionsType>>;
|