UNPKG

14 kBTypeScriptView Raw
1import type {SignalConstants} from 'node:os';
2import type {Readable} from 'node:stream';
3import type {Unless} from '../utils.js';
4import type {Message} from '../ipc.js';
5import type {StdinOptionCommon, StdoutStderrOptionCommon, StdioOptionsProperty} from '../stdio/type.js';
6import type {VerboseOption} from '../verbose.js';
7import type {FdGenericOption} from './specific.js';
8import type {EncodingOption} from './encoding-option.js';
9
10export type CommonOptions<IsSync extends boolean = boolean> = {
11 /**
12 Prefer locally installed binaries when looking for a binary to execute.
13
14 @default `true` with `$`, `false` otherwise
15 */
16 readonly preferLocal?: boolean;
17
18 /**
19 Preferred path to find locally installed binaries, when using the `preferLocal` option.
20
21 @default `cwd` option
22 */
23 readonly localDir?: string | URL;
24
25 /**
26 If `true`, runs with Node.js. The first argument must be a Node.js file.
27
28 The subprocess inherits the current Node.js [CLI flags](https://nodejs.org/api/cli.html#options) and version. This can be overridden using the `nodeOptions` and `nodePath` options.
29
30 @default `true` with `execaNode()`, `false` otherwise
31 */
32 readonly node?: boolean;
33
34 /**
35 List of [CLI flags](https://nodejs.org/api/cli.html#cli_options) passed to the Node.js executable.
36
37 Requires the `node` option to be `true`.
38
39 @default [`process.execArgv`](https://nodejs.org/api/process.html#process_process_execargv) (current Node.js CLI flags)
40 */
41 readonly nodeOptions?: readonly string[];
42
43 /**
44 Path to the Node.js executable.
45
46 Requires the `node` option to be `true`.
47
48 @default [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath) (current Node.js executable)
49 */
50 readonly nodePath?: string | URL;
51
52 /**
53 If `true`, runs the command inside of a [shell](https://en.wikipedia.org/wiki/Shell_(computing)).
54
55 Uses [`/bin/sh`](https://en.wikipedia.org/wiki/Unix_shell) on UNIX and [`cmd.exe`](https://en.wikipedia.org/wiki/Cmd.exe) on Windows. A different shell can be specified as a string. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
56
57 We recommend against using this option.
58
59 @default false
60 */
61 readonly shell?: boolean | string | URL;
62
63 /**
64 Current [working directory](https://en.wikipedia.org/wiki/Working_directory) of the subprocess.
65
66 This is also used to resolve the `nodePath` option when it is a relative path.
67
68 @default process.cwd()
69 */
70 readonly cwd?: string | URL;
71
72 /**
73 [Environment variables](https://en.wikipedia.org/wiki/Environment_variable).
74
75 Unless the `extendEnv` option is `false`, the subprocess also uses the current process' environment variables ([`process.env`](https://nodejs.org/api/process.html#processenv)).
76
77 @default [process.env](https://nodejs.org/api/process.html#processenv)
78 */
79 readonly env?: Readonly<Partial<Record<string, string>>>;
80
81 /**
82 If `true`, the subprocess uses both the `env` option and the current process' environment variables ([`process.env`](https://nodejs.org/api/process.html#processenv)).
83 If `false`, only the `env` option is used, not `process.env`.
84
85 @default true
86 */
87 readonly extendEnv?: boolean;
88
89 /**
90 Write some input to the subprocess' [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)).
91
92 See also the `inputFile` and `stdin` options.
93 */
94 readonly input?: string | Uint8Array | Readable;
95
96 /**
97 Use a file as input to the subprocess' [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)).
98
99 See also the `input` and `stdin` options.
100 */
101 readonly inputFile?: string | URL;
102
103 /**
104 How to setup the subprocess' [standard input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Readable` stream, a web `ReadableStream`, a `{ file: 'path' }` object, a file URL, an `Iterable`, an `AsyncIterable`, an `Uint8Array`, a generator function, a `Duplex` or a web `TransformStream`.
105
106 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.
107
108 @default `'inherit'` with `$`, `'pipe'` otherwise
109 */
110 readonly stdin?: StdinOptionCommon<IsSync>;
111
112 /**
113 How to setup the subprocess' [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.
114
115 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.
116
117 @default 'pipe'
118 */
119 readonly stdout?: StdoutStderrOptionCommon<IsSync>;
120
121 /**
122 How to setup the subprocess' [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.
123
124 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.
125
126 @default 'pipe'
127 */
128 readonly stderr?: StdoutStderrOptionCommon<IsSync>;
129
130 /**
131 Like the `stdin`, `stdout` and `stderr` options but for all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.
132
133 A single string can be used as a shortcut.
134
135 The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`.
136
137 @default 'pipe'
138 */
139 readonly stdio?: StdioOptionsProperty<IsSync>;
140
141 /**
142 Add a `subprocess.all` stream and a `result.all` property. They contain the combined/interleaved output of the subprocess' `stdout` and `stderr`.
143
144 @default false
145 */
146 readonly all?: boolean;
147
148 /**
149 If the subprocess outputs text, specifies its character encoding, either [`'utf8'`](https://en.wikipedia.org/wiki/UTF-8) or [`'utf16le'`](https://en.wikipedia.org/wiki/UTF-16).
150
151 If it outputs binary data instead, this should be either:
152 - `'buffer'`: returns the binary output as an `Uint8Array`.
153 - [`'hex'`](https://en.wikipedia.org/wiki/Hexadecimal), [`'base64'`](https://en.wikipedia.org/wiki/Base64), [`'base64url'`](https://en.wikipedia.org/wiki/Base64#URL_applications), [`'latin1'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) or [`'ascii'`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings): encodes the binary output as a string.
154
155 The output is available with `result.stdout`, `result.stderr` and `result.stdio`.
156
157 @default 'utf8'
158 */
159 readonly encoding?: EncodingOption;
160
161 /**
162 Set `result.stdout`, `result.stderr`, `result.all` and `result.stdio` as arrays of strings, splitting the subprocess' output into lines.
163
164 This cannot be used if the `encoding` option is binary.
165
166 By default, this applies to both `stdout` and `stderr`, but different values can also be passed.
167
168 @default false
169 */
170 readonly lines?: FdGenericOption<boolean>;
171
172 /**
173 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.
174
175 If the `lines` option is true, this applies to each output line instead.
176
177 By default, this applies to both `stdout` and `stderr`, but different values can also be passed.
178
179 @default true
180 */
181 readonly stripFinalNewline?: FdGenericOption<boolean>;
182
183 /**
184 Largest amount of data allowed on `stdout`, `stderr` and `stdio`.
185
186 By default, this applies to both `stdout` and `stderr`, but different values can also be passed.
187
188 When reached, `error.isMaxBuffer` becomes `true`.
189
190 @default 100_000_000
191 */
192 readonly maxBuffer?: FdGenericOption<number>;
193
194 /**
195 When `buffer` is `false`, the `result.stdout`, `result.stderr`, `result.all` and `result.stdio` properties are not set.
196
197 By default, this applies to both `stdout` and `stderr`, but different values can also be passed.
198
199 @default true
200 */
201 readonly buffer?: FdGenericOption<boolean>;
202
203 /**
204 Enables exchanging messages with the subprocess using `subprocess.sendMessage(message)`, `subprocess.getOneMessage()` and `subprocess.getEachMessage()`.
205
206 The subprocess must be a Node.js file.
207
208 @default `true` if the `node`, `ipcInput` or `gracefulCancel` option is set, `false` otherwise
209 */
210 readonly ipc?: Unless<IsSync, boolean>;
211
212 /**
213 Specify the kind of serialization used for sending messages between subprocesses when using the `ipc` option.
214
215 @default 'advanced'
216 */
217 readonly serialization?: Unless<IsSync, 'json' | 'advanced'>;
218
219 /**
220 Sends an IPC message when the subprocess starts.
221
222 The subprocess must be a Node.js file. The value's type depends on the `serialization` option.
223 */
224 readonly ipcInput?: Unless<IsSync, Message>;
225
226 /**
227 If `verbose` is `'short'`, prints the command on [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)): its file, arguments, duration and (if it failed) error message.
228
229 If `verbose` is `'full'` or a function, the command's [`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)), `stderr` and IPC messages are also printed.
230
231 A function can be passed to customize logging.
232
233 By default, this applies to both `stdout` and `stderr`, but different values can also be passed.
234
235 @default 'none'
236 */
237 readonly verbose?: VerboseOption;
238
239 /**
240 Setting this to `false` resolves the result's promise with the error instead of rejecting it.
241
242 @default true
243 */
244 readonly reject?: boolean;
245
246 /**
247 If `timeout` is greater than `0`, the subprocess will be terminated if it runs for longer than that amount of milliseconds.
248
249 On timeout, `error.timedOut` becomes `true`.
250
251 @default 0
252 */
253 readonly timeout?: number;
254
255 /**
256 When the `cancelSignal` is [aborted](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort), terminate the subprocess using a `SIGTERM` signal.
257
258 When aborted, `error.isCanceled` becomes `true`.
259
260 @example
261 ```
262 import {execaNode} from 'execa';
263
264 const controller = new AbortController();
265 const cancelSignal = controller.signal;
266
267 setTimeout(() => {
268 controller.abort();
269 }, 5000);
270
271 try {
272 await execaNode({cancelSignal})`build.js`;
273 } catch (error) {
274 if (error.isCanceled) {
275 console.error('Canceled by cancelSignal.');
276 }
277
278 throw error;
279 }
280 ```
281 */
282 readonly cancelSignal?: Unless<IsSync, AbortSignal>;
283
284 /**
285 When the `cancelSignal` option is [aborted](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort), do not send any `SIGTERM`. Instead, abort the [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) returned by `getCancelSignal()`. The subprocess should use it to terminate gracefully.
286
287 The subprocess must be a Node.js file.
288
289 When aborted, `error.isGracefullyCanceled` becomes `true`.
290
291 @default false
292 */
293 readonly gracefulCancel?: Unless<IsSync, boolean>;
294
295 /**
296 If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).
297
298 When this happens, `error.isForcefullyTerminated` becomes `true`.
299
300 @default 5000
301 */
302 readonly forceKillAfterDelay?: Unless<IsSync, number | boolean>;
303
304 /**
305 Default [signal](https://en.wikipedia.org/wiki/Signal_(IPC)) used to terminate the subprocess.
306
307 This can be either a name (like `'SIGTERM'`) or a number (like `9`).
308
309 @default 'SIGTERM'
310 */
311 readonly killSignal?: keyof SignalConstants | number;
312
313 /**
314 Run the subprocess independently from the current process.
315
316 @default false
317 */
318 readonly detached?: Unless<IsSync, boolean>;
319
320 /**
321 Kill the subprocess when the current process exits.
322
323 @default true
324 */
325 readonly cleanup?: Unless<IsSync, boolean>;
326
327 /**
328 Sets the [user identifier](https://en.wikipedia.org/wiki/User_identifier) of the subprocess.
329
330 @default current user identifier
331 */
332 readonly uid?: number;
333
334 /**
335 Sets the [group identifier](https://en.wikipedia.org/wiki/Group_identifier) of the subprocess.
336
337 @default current group identifier
338 */
339 readonly gid?: number;
340
341 /**
342 Value of [`argv[0]`](https://nodejs.org/api/process.html#processargv0) sent to the subprocess.
343
344 @default file being executed
345 */
346 readonly argv0?: string;
347
348 /**
349 On Windows, do not create a new console window.
350
351 @default true
352 */
353 readonly windowsHide?: boolean;
354
355 /**
356 If `false`, escapes the command arguments on Windows.
357
358 @default `true` if the `shell` option is `true`, `false` otherwise
359 */
360 readonly windowsVerbatimArguments?: boolean;
361};
362
363/**
364Subprocess options.
365
366Some options are related to the subprocess output: `verbose`, `lines`, `stripFinalNewline`, `buffer`, `maxBuffer`. By default, those options apply to all file descriptors (`stdout`, `stderr`, etc.). A plain object can be passed instead to apply them to only `stdout`, `stderr`, `all` (both stdout and stderr), `ipc`, `fd3`, etc.
367
368@example
369
370```
371// Same value for stdout and stderr
372await execa({verbose: 'full'})`npm run build`;
373
374// Different values for stdout and stderr
375await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;
376```
377*/
378export type Options = CommonOptions<false>;
379
380/**
381Subprocess options, with synchronous methods.
382
383Some options are related to the subprocess output: `verbose`, `lines`, `stripFinalNewline`, `buffer`, `maxBuffer`. By default, those options apply to all file descriptors (`stdout`, `stderr`, etc.). A plain object can be passed instead to apply them to only `stdout`, `stderr`, `all` (both stdout and stderr), `ipc`, `fd3`, etc.
384
385@example
386
387```
388// Same value for stdout and stderr
389execaSync({verbose: 'full'})`npm run build`;
390
391// Different values for stdout and stderr
392execaSync({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;
393```
394*/
395export type SyncOptions = CommonOptions<true>;
396
397export type StricterOptions<
398 WideOptions extends CommonOptions,
399 StrictOptions extends CommonOptions,
400> = WideOptions extends StrictOptions ? WideOptions : StrictOptions;