Skip to content

shell

Access the system shell. Allows you to spawn child processes and manage files and URLs using their default application.

Security

This API has a scope configuration that forces you to restrict the programs and arguments that can be used.

Restricting access to the open API

The open API can be used with any URL, as the argument is validated with the ^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+ regex.

Classes

Child

Constructors

new Child()
new Child(pid): Child
Parameters
ParameterType
pidnumber
Returns

Child

Properties

PropertyTypeDescriptionDefined in
pidnumberThe child process pid.

Methods

kill()
kill(): Promise<void>

Kills the child process.

Returns

Promise<void>

A promise indicating the success or failure of the operation.

write()
write(data): Promise<void>

Writes data to the stdin.

Parameters
ParameterTypeDescription
datanumber[] | IOPayloadThe message to write, either a string or a byte array.
Returns

Promise<void>

A promise indicating the success or failure of the operation.

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('node');
const child = await command.spawn();
await child.write('message');
await child.write([0, 1, 2, 3, 4, 5]);

Command<O>

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

Example

import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('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);

Extends

Type Parameters

Type Parameter
O extends IOPayload

Properties

PropertyModifierTypeDescriptionDefined in
stderrreadonlyEventEmitter<OutputEvents<O>>Event emitter for the stderr. Emits the data event.
stdoutreadonlyEventEmitter<OutputEvents<O>>Event emitter for the stdout. Emits the data event.

Methods

addListener()
addListener<N>(eventName, listener): this

Alias for emitter.on(eventName, listener).

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.addListener

execute()
execute(): Promise<ChildProcess<O>>

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

Returns

Promise<ChildProcess<O>>

A promise resolving to the child process output.

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

listenerCount()
listenerCount<N>(eventName): number

Returns the number of listeners listening to the event named eventName.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
Returns

number

Inherited from

EventEmitter.listenerCount

off()
off<N>(eventName, listener): this

Removes the all specified listener from the listener array for the event eventName Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.off

on()
on<N>(eventName, listener): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventNameand listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.on

once()
once<N>(eventName, listener): this

Adds a one-timelistener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.once

prependListener()
prependListener<N>(eventName, listener): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventNameand listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.prependListener

prependOnceListener()
prependOnceListener<N>(eventName, listener): this

Adds a one-timelistener function for the event named eventName to the_beginning_ of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.prependOnceListener

removeAllListeners()
removeAllListeners<N>(event?): this

Removes all listeners, or those of the specified eventName.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
event?N
Returns

this

Inherited from

EventEmitter.removeAllListeners

removeListener()
removeListener<N>(eventName, listener): this

Alias for emitter.off(eventName, listener).

Type Parameters
Type Parameter
N extends keyof CommandEvents
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Inherited from

EventEmitter.removeListener

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.

create()

Creates a command to execute the given program.

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('my-app', ['run', 'tauri']);
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

create(program, args)
static create(program, args?): Command<string>

Creates a command to execute the given program.

Parameters
ParameterType
programstring
args?string | string[]
Returns

Command<string>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('my-app', ['run', 'tauri']);
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

create(program, args, options)
static create(
program,
args?,
options?): Command<Uint8Array<ArrayBufferLike>>

Creates a command to execute the given program.

Parameters
ParameterType
programstring
args?string | string[]
options?SpawnOptions & object
Returns

Command<Uint8Array<ArrayBufferLike>>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('my-app', ['run', 'tauri']);
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

create(program, args, options)
static create(
program,
args?,
options?): Command<string>

Creates a command to execute the given program.

Parameters
ParameterType
programstring
args?string | string[]
options?SpawnOptions
Returns

Command<string>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.create('my-app', ['run', 'tauri']);
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

sidecar()

Creates a command to execute the given sidecar program.

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.sidecar('my-sidecar');
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

sidecar(program, args)
static sidecar(program, args?): Command<string>

Creates a command to execute the given sidecar program.

Parameters
ParameterType
programstring
args?string | string[]
Returns

Command<string>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.sidecar('my-sidecar');
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

sidecar(program, args, options)
static sidecar(
program,
args?,
options?): Command<Uint8Array<ArrayBufferLike>>

Creates a command to execute the given sidecar program.

Parameters
ParameterType
programstring
args?string | string[]
options?SpawnOptions & object
Returns

Command<Uint8Array<ArrayBufferLike>>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.sidecar('my-sidecar');
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.

sidecar(program, args, options)
static sidecar(
program,
args?,
options?): Command<string>

Creates a command to execute the given sidecar program.

Parameters
ParameterType
programstring
args?string | string[]
options?SpawnOptions
Returns

Command<string>

Example
import { Command } from '@crabnebula/taurify-api/shell';
const command = Command.sidecar('my-sidecar');
const output = await command.execute();
Param

The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope.


EventEmitter<E>

Extended by

Type Parameters

Type Parameter
E extends Record<string, any>

Constructors

new EventEmitter()
new EventEmitter<E>(): EventEmitter<E>
Returns

EventEmitter<E>

Methods

addListener()
addListener<N>(eventName, listener): this

Alias for emitter.on(eventName, listener).

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

listenerCount()
listenerCount<N>(eventName): number

Returns the number of listeners listening to the event named eventName.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
Returns

number

off()
off<N>(eventName, listener): this

Removes the all specified listener from the listener array for the event eventName Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

on()
on<N>(eventName, listener): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventNameand listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

once()
once<N>(eventName, listener): this

Adds a one-timelistener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

prependListener()
prependListener<N>(eventName, listener): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventNameand listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

prependOnceListener()
prependOnceListener<N>(eventName, listener): this

Adds a one-timelistener function for the event named eventName to the_beginning_ of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

removeAllListeners()
removeAllListeners<N>(event?): this

Removes all listeners, or those of the specified eventName.

Returns a reference to the EventEmitter, so that calls can be chained.

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
event?N
Returns

this

removeListener()
removeListener<N>(eventName, listener): this

Alias for emitter.off(eventName, listener).

Type Parameters
Type Parameter
N extends string | number | symbol
Parameters
ParameterType
eventNameN
listener(arg) => void
Returns

this

Interfaces

ChildProcess<O>

Type Parameters

Type Parameter
O extends IOPayload

Properties

PropertyTypeDescriptionDefined in
codenull | numberExit code of the process. null if the process was terminated by a signal on Unix.
signalnull | numberIf the process was terminated by a signal, represents that signal.
stderrOThe data that the process wrote to stderr.
stdoutOThe data that the process wrote to stdout.

CommandEvents

Properties

PropertyTypeDefined in
closeTerminatedPayload
errorstring

OutputEvents<O>

Type Parameters

Type Parameter
O extends IOPayload

Properties

PropertyTypeDefined in
dataO

SpawnOptions

Properties

PropertyTypeDescriptionDefined in
cwd?stringCurrent working directory.
encoding?stringCharacter encoding for stdout/stderr
env?Record<string, string>Environment variables. set to null to clear the process env.

TerminatedPayload

Payload for the Terminated command event.

Properties

PropertyTypeDescriptionDefined in
codenull | numberExit code of the process. null if the process was terminated by a signal on Unix.
signalnull | numberIf the process was terminated by a signal, represents that signal.

Type Aliases

IOPayload

type IOPayload: string | Uint8Array;

Event payload type

Functions

open()

function open(path, openWith?): Promise<void>

Opens a path or URL with the system’s default app, or the one specified with openWith.

The openWith value must be one of firefox, google chrome, chromium safari, open, start, xdg-open, gio, gnome-open, kde-open or wslview.

Parameters

ParameterTypeDescription
pathstringThe path or URL to open. This value is matched against the string regex defined on tauri.conf.json > plugins > shell > open, which defaults to `^((mailto:\w+)
openWith?stringThe app to open the file or URL with. Defaults to the system default application for the specified path type.

Returns

Promise<void>

Example

import { open } from '@crabnebula/taurify-api/shell';
// opens the given URL on the default browser:
await open('https://github.com/tauri-apps/tauri');
// opens the given URL using `firefox`:
await open('https://github.com/tauri-apps/tauri', 'firefox');
// opens a file using the default program:
await open('/path/to/file');