UNPKG

4.46 kBTypeScriptView Raw
1import type {
2 CommonOptions,
3 Options,
4 SyncOptions,
5 StricterOptions,
6} from '../arguments/options.js';
7import type {SyncResult} from '../return/result.js';
8import type {ResultPromise} from '../subprocess/subprocess.js';
9import type {TemplateString} from './template.js';
10
11/**
12Same as `execa()` but using script-friendly default options.
13
14When `command` is a template string, it includes both the `file` and its `arguments`.
15
16`$(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.
17
18This is the preferred method when executing multiple commands in a script file.
19
20@returns A `ResultPromise` that is both:
21- the subprocess.
22- a `Promise` either resolving with its successful `result`, or rejecting with its `error`.
23@throws `ExecaError`
24
25@example <caption>Basic</caption>
26```
27import {$} from 'execa';
28
29const branch = await $`git branch --show-current`;
30await $`dep deploy --branch=${branch}`;
31```
32
33@example <caption>Verbose mode</caption>
34```
35$ node build.js
36Building application...
37Done building.
38Running tests...
39Error: the entrypoint is invalid.
40
41$ NODE_DEBUG=execa node build.js
42[00:57:44.581] [0] $ npm run build
43[00:57:44.653] [0] Building application...
44[00:57:44.653] [0] Done building.
45[00:57:44.658] [0] ✔ (done in 78ms)
46[00:57:44.658] [1] $ npm run test
47[00:57:44.740] [1] Running tests...
48[00:57:44.740] [1] Error: the entrypoint is invalid.
49[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test
50[00:57:44.747] [1] ✘ (done in 89ms)
51```
52*/
53export const $: ExecaScriptMethod<{}>;
54
55/**
56`$()` method either exported by Execa, or bound using `$(options)`.
57*/
58export type ExecaScriptMethod<OptionsType extends CommonOptions = CommonOptions> =
59 & ExecaScriptBind<OptionsType>
60 & ExecaScriptTemplate<OptionsType>
61 & ExecaScriptArrayLong<OptionsType>
62 & ExecaScriptArrayShort<OptionsType>
63 & {sync: ExecaScriptSyncMethod<OptionsType>}
64 & {s: ExecaScriptSyncMethod<OptionsType>};
65
66// `$(options)` binding
67type ExecaScriptBind<OptionsType extends CommonOptions> =
68 <NewOptionsType extends CommonOptions = {}>(options: NewOptionsType)
69 => ExecaScriptMethod<OptionsType & NewOptionsType>;
70
71// `$`command`` template syntax
72type ExecaScriptTemplate<OptionsType extends CommonOptions> =
73 (...templateString: TemplateString)
74 => ResultPromise<StricterOptions<OptionsType, Options>>;
75
76// `$('file', ['arg'], {})` array syntax
77type ExecaScriptArrayLong<OptionsType extends CommonOptions> =
78 <NewOptionsType extends Options = {}>(file: string | URL, arguments?: readonly string[], options?: NewOptionsType)
79 => ResultPromise<StricterOptions<OptionsType & NewOptionsType, Options>>;
80
81// `$('file', {})` array syntax
82type ExecaScriptArrayShort<OptionsType extends CommonOptions> =
83 <NewOptionsType extends Options = {}>(file: string | URL, options?: NewOptionsType)
84 => ResultPromise<StricterOptions<OptionsType & NewOptionsType, Options>>;
85
86// We must intersect the overloaded methods with & instead of using a simple object as a workaround for a TypeScript bug
87// See https://github.com/microsoft/TypeScript/issues/58765
88/**
89`$.sync()` method either exported by Execa, or bound using `$.sync(options)`.
90*/
91export type ExecaScriptSyncMethod<OptionsType extends CommonOptions = CommonOptions> =
92 & ExecaScriptSyncBind<OptionsType>
93 & ExecaScriptSyncTemplate<OptionsType>
94 & ExecaScriptSyncArrayLong<OptionsType>
95 & ExecaScriptSyncArrayShort<OptionsType>;
96
97// `$.sync(options)` binding
98type ExecaScriptSyncBind<OptionsType extends CommonOptions> =
99 <NewOptionsType extends SyncOptions = {}>(options: NewOptionsType)
100 => ExecaScriptSyncMethod<OptionsType & NewOptionsType>;
101
102// $.sync`command` template syntax
103type ExecaScriptSyncTemplate<OptionsType extends CommonOptions> =
104 (...templateString: TemplateString)
105 => SyncResult<StricterOptions<OptionsType, SyncOptions>>;
106
107// `$.sync('file', ['arg'], {})` array syntax
108type ExecaScriptSyncArrayLong<OptionsType extends CommonOptions> =
109 <NewOptionsType extends SyncOptions = {}>(file: string | URL, arguments?: readonly string[], options?: NewOptionsType)
110 => SyncResult<StricterOptions<OptionsType & NewOptionsType, SyncOptions>>;
111
112// `$.sync('file', {})` array syntax
113type ExecaScriptSyncArrayShort<OptionsType extends CommonOptions> =
114 <NewOptionsType extends SyncOptions = {}>(file: string | URL, options?: NewOptionsType)
115 => SyncResult<StricterOptions<OptionsType & NewOptionsType, SyncOptions>>;