shell.Command
The entry point for spawning child processes.
It emits the close
and error
events.
example
const command = new Command('node')
command.on('close', data => {
console.log(`command finished with code ${data.code} and signal ${data.signal}`)
})
command.on('error', error => console.error(`command error: "${error}"`))
command.stdout.on('data', line => console.log(`command stdout: "${line}"`))
command.stderr.on('data', line => console.log(`command stderr: "${line}"`))
const child = await command.spawn()
console.log('pid:', child.pid)
#
HierarchyEventEmitter
<"close"
|"error"
>↳
Command
#
Constructors#
constructor• new Command(program
, args?
, options?
)
Creates a new Command
instance.
#
ParametersName | Type | Default value | Description |
---|---|---|---|
program | string | undefined | The program to execute. |
args | string | string [] | [] | Program arguments. |
options? | SpawnOptions | undefined | Spawn options. |
#
OverridesEventEmitter<'close' | 'error'\>.constructor
#
Defined in#
Properties#
stderr• Readonly
stderr: EventEmitter
<"data"
>
Event emitter for the stderr
. Emits the data
event.
#
Defined in#
stdout• Readonly
stdout: EventEmitter
<"data"
>
Event emitter for the stdout
. Emits the data
event.
#
Defined in#
Methods#
execute▸ execute(): Promise
<ChildProcess
>
Executes the command as a child process, waiting for it to finish and collecting all of its output.
example
const output = await new Command('echo', 'message').execute()
assert(output.code === 0)
assert(output.signal === null)
assert(output.stdout === 'message')
assert(output.stderr === '')
#
ReturnsPromise
<ChildProcess
>
A promise resolving to the child process output.
#
Defined in#
on▸ on(event
, handler
): EventEmitter
<"close"
| "error"
>
Listen to an event from the child process.
#
ParametersName | Type | Description |
---|---|---|
event | "close" | "error" | The event name. |
handler | (arg : any ) => void | The event handler. |
#
ReturnsEventEmitter
<"close"
| "error"
>
The this
instance for chained calls.
#
Inherited fromEventEmitter.on
#
Defined in#
spawn▸ spawn(): Promise
<Child
>
Executes the command as a child process, returning a handle to it.
#
ReturnsPromise
<Child
>
A promise resolving to the child process handle.
#
Defined in#
sidecar▸ Static
sidecar(program
, args?
, options?
): Command
Creates a command to execute the given sidecar program.
example
const command = Command.sidecar('my-sidecar')
const output = await command.execute()
#
ParametersName | Type | Default value | Description |
---|---|---|---|
program | string | undefined | The program to execute. |
args | string | string [] | [] | Program arguments. |
options? | SpawnOptions | undefined | Spawn options. |