Files
archived-tauri-docs/docs/api/js/classes/shell.Command.md
2022-07-15 12:00:05 +02:00

4.2 KiB

@tauri-apps/api / shell / Command

Class: Command

shell.Command

The entry point for spawning child processes. It emits the close and error events.

Example

import { Command } from '@tauri-apps/api/shell';
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);

Hierarchy

Constructors

constructor

new Command(program, args?, options?)

Creates a new Command instance.

Parameters

Name Type Default value Description
program string undefined The program name to execute. It must be configured on tauri.conf.json > tauri > allowlist > shell > scope.
args string | string[] [] Program arguments.
options? SpawnOptions undefined Spawn options.

Overrides

EventEmitter.constructor

Properties

stderr

Readonly stderr: EventEmitter<"data">

Event emitter for the stderr. Emits the data event.

Defined in

shell.ts:258


stdout

Readonly stdout: EventEmitter<"data">

Event emitter for the stdout. Emits the data event.

Defined in

shell.ts:256

Methods

execute

execute(): Promise<ChildProcess>

Executes the command as a child process, waiting for it to finish and collecting all of its output.

Example

import { Command } from '@tauri-apps/api/shell';
const output = await new Command('echo', 'message').execute();
assert(output.code === 0);
assert(output.signal === null);
assert(output.stdout === 'message');
assert(output.stderr === '');

Returns

Promise<ChildProcess>

A promise resolving to the child process output.


on

on(event, handler): EventEmitter<"error" | "close">

Listen to an event from the child process.

Parameters

Name Type Description
event "error" | "close" The event name.
handler (arg: any) => void The event handler.

Returns

EventEmitter<"error" | "close">

The this instance for chained calls.

Inherited from

EventEmitter.on


spawn

spawn(): Promise<Child>

Executes the command as a child process, returning a handle to it.

Returns

Promise<Child>

A promise resolving to the child process handle.


sidecar

Static sidecar(program, args?, options?): Command

Creates a command to execute the given sidecar program.

Example

import { Command } from '@tauri-apps/api/shell';
const command = Command.sidecar('my-sidecar');
const output = await command.execute();

Parameters

Name Type Default value Description
program string undefined The program to execute. It must be configured on tauri.conf.json > tauri > allowlist > shell > scope.
args string | string[] [] Program arguments.
options? SpawnOptions undefined Spawn options.

Returns

Command