UNPKG

4.14 kBTypeScriptView Raw
1import type {Options, SyncOptions} from '../arguments/options.js';
2import type {SyncResult} from '../return/result.js';
3import type {ResultPromise} from '../subprocess/subprocess.js';
4import type {SimpleTemplateString} from './template.js';
5
6/**
7Executes a command. `command` is a string that includes both the `file` and its `arguments`.
8
9When `command` is a template string, it includes both the `file` and its `arguments`.
10
11`execaCommand(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.
12
13This is only intended for very specific cases, such as a REPL. This should be avoided otherwise.
14
15@param command - The program/script to execute and its arguments.
16@returns A `ResultPromise` that is both:
17- the subprocess.
18- a `Promise` either resolving with its successful `result`, or rejecting with its `error`.
19@throws `ExecaError`
20
21@example
22```
23import {execaCommand} from 'execa';
24
25for await (const commandAndArguments of getReplLine()) {
26 await execaCommand(commandAndArguments);
27}
28```
29*/
30export declare const execaCommand: ExecaCommandMethod<{}>;
31
32type ExecaCommandMethod<OptionsType extends Options> =
33 & ExecaCommandBind<OptionsType>
34 & ExecaCommandTemplate<OptionsType>
35 & ExecaCommandArray<OptionsType>;
36
37// `execaCommand(options)` binding
38type ExecaCommandBind<OptionsType extends Options> =
39 <NewOptionsType extends Options = {}>(options: NewOptionsType)
40 => ExecaCommandMethod<OptionsType & NewOptionsType>;
41
42// `execaCommand`command`` template syntax
43type ExecaCommandTemplate<OptionsType extends Options> =
44 (...templateString: SimpleTemplateString)
45 => ResultPromise<OptionsType>;
46
47// `execaCommand('command', {})` array syntax
48type ExecaCommandArray<OptionsType extends Options> =
49 <NewOptionsType extends Options = {}>(command: string, options?: NewOptionsType)
50 => ResultPromise<OptionsType & NewOptionsType>;
51
52/**
53Same as `execaCommand()` but synchronous.
54
55When `command` is a template string, it includes both the `file` and its `arguments`.
56
57`execaCommandSync(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.
58
59Returns a subprocess `result` or throws an `error`. The `subprocess` is not returned: its methods and properties are not available.
60
61@param command - The program/script to execute and its arguments.
62@returns `SyncResult`
63@throws `ExecaSyncError`
64
65@example
66```
67import {execaCommandSync} from 'execa';
68
69for (const commandAndArguments of getReplLine()) {
70 execaCommandSync(commandAndArguments);
71}
72```
73*/
74export declare const execaCommandSync: ExecaCommandSyncMethod<{}>;
75
76type ExecaCommandSyncMethod<OptionsType extends SyncOptions> =
77 & ExecaCommandSyncBind<OptionsType>
78 & ExecaCommandSyncTemplate<OptionsType>
79 & ExecaCommandSyncArray<OptionsType>;
80
81// `execaCommandSync(options)` binding
82type ExecaCommandSyncBind<OptionsType extends SyncOptions> =
83 <NewOptionsType extends SyncOptions = {}>(options: NewOptionsType)
84 => ExecaCommandSyncMethod<OptionsType & NewOptionsType>;
85
86// `execaCommandSync`command`` template syntax
87type ExecaCommandSyncTemplate<OptionsType extends SyncOptions> =
88 (...templateString: SimpleTemplateString)
89 => SyncResult<OptionsType>;
90
91// `execaCommandSync('command', {})` array syntax
92type ExecaCommandSyncArray<OptionsType extends SyncOptions> =
93 <NewOptionsType extends SyncOptions = {}>(command: string, options?: NewOptionsType)
94 => SyncResult<OptionsType & NewOptionsType>;
95
96/**
97Split a `command` string into an array. For example, `'npm run build'` returns `['npm', 'run', 'build']` and `'argument otherArgument'` returns `['argument', 'otherArgument']`.
98
99@param command - The file to execute and/or its arguments.
100@returns fileOrArgument[]
101
102@example
103```
104import {execa, parseCommandString} from 'execa';
105
106const commandString = 'npm run task';
107const commandArray = parseCommandString(commandString);
108await execa`${commandArray}`;
109
110const [file, ...commandArguments] = commandArray;
111await execa(file, commandArguments);
112```
113*/
114export function parseCommandString(command: string): string[];