@tauri-apps/plugin-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
On the configuration object, open: true
means that the open API can be used with any URL,
as the argument is validated with the ^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+
regex.
You can change that regex by changing the boolean value to a string, e.g. open: ^https://github.com/
.
Restricting access to the Command
APIs
The plugin configuration object has a scope
field that defines an array of CLIs that can be used.
Each CLI is a configuration object { name: string, cmd: string, sidecar?: bool, args?: boolean | Arg[] }
.
name
: the unique identifier of the command, passed to the Command.create function. If it’s a sidecar, this must be the value defined ontauri.conf.json > tauri > bundle > externalBin
.cmd
: the program that is executed on this configuration. If it’s a sidecar, this value is ignored.sidecar
: whether the object configures a sidecar or a system program.args
: the arguments that can be passed to the program. By default no arguments are allowed.true
means that any argument list is allowed.false
means that no arguments are allowed.- otherwise an array can be configured. Each item is either a string representing the fixed argument value
or a
{ validator: string }
that defines a regex validating the argument value.
Example scope configuration
CLI: git commit -m "the commit message"
Configuration:
Usage:
Trying to execute any API with a program not configured on the scope results in a promise rejection due to denied access.
Classes
Child
Since
2.0.0
Constructors
new Child()
Parameters
Parameter | Type |
---|---|
pid | number |
Returns
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L300
Properties
Property | Type | Description |
---|---|---|
pid | number | The child process pid . |
Methods
kill()
Kills the child process.
Returns
Promise
<void
>
A promise indicating the success or failure of the operation.
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L336
write()
Writes data
to the stdin
.
Parameters
Parameter | Type | Description |
---|---|---|
data | IOPayload | The message to write, either a string or a byte array. |
Returns
Promise
<void
>
A promise indicating the success or failure of the operation.
Example
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L321
Command<O>
The entry point for spawning child processes.
It emits the close
and error
events.
Example
Since
2.0.0
Extends
Type parameters
Type parameter |
---|
O extends IOPayload |
Properties
Property | Modifier | Type | Default value | Description |
---|---|---|---|---|
stderr | readonly | EventEmitter <OutputEvents <O >> | ... | Event emitter for the stderr . Emits the data event. |
stdout | readonly | EventEmitter <OutputEvents <O >> | ... | Event emitter for the stdout . Emits the data event. |
Methods
addListener()
Alias for emitter.on(eventName, listener)
.
Type parameters
Type parameter |
---|
N extends keyof CommandEvents |
Parameters
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L117
execute()
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
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L530
listenerCount()
Returns the number of listeners listening to the event named eventName
.
Type parameters
Type parameter |
---|
N extends keyof CommandEvents |
Parameters
Parameter | Type |
---|---|
eventName | N |
Returns
number
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L240
off()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L185
on()
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 eventName
and 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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L146
once()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L168
prependListener()
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 eventName
and 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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L257
prependOnceListener()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
EventEmitter
.prependOnceListener
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L279
removeAllListeners()
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
Parameter | Type |
---|---|
event ? | N |
Returns
this
Inherited from
EventEmitter
.removeAllListeners
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L205
removeListener()
Alias for emitter.off(eventName, listener)
.
Type parameters
Type parameter |
---|
N extends keyof CommandEvents |
Parameters
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Inherited from
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L129
spawn()
Executes the command as a child process, returning a handle to it.
Returns
A promise resolving to the child process handle.
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L479
create()
create(program, args)
Creates a command to execute the given program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
Returns
Command
<string
>
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L406
create(program, args, options)
Creates a command to execute the given program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
options ? | SpawnOptions & object | - |
Returns
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L407
create(program, args, options)
Creates a command to execute the given program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
options ? | SpawnOptions | - |
Returns
Command
<string
>
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L412
sidecar()
sidecar(program, args)
Creates a command to execute the given sidecar program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
Returns
Command
<string
>
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L438
sidecar(program, args, options)
Creates a command to execute the given sidecar program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
options ? | SpawnOptions & object | - |
Returns
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L439
sidecar(program, args, options)
Creates a command to execute the given sidecar program.
Parameters
Parameter | Type | Description |
---|---|---|
program | string | The program to execute. It must be configured on tauri.conf.json > plugins > shell > scope . |
args ? | string | string [] | - |
options ? | SpawnOptions | - |
Returns
Command
<string
>
Example
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L444
EventEmitter<E>
Since
2.0.0
Extended by
Type parameters
Type parameter |
---|
E extends Record <string , any > |
Constructors
new EventEmitter()
Returns
EventEmitter
<E
>
Methods
addListener()
Alias for emitter.on(eventName, listener)
.
Type parameters
Type parameter |
---|
N extends string | number | symbol |
Parameters
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L117
listenerCount()
Returns the number of listeners listening to the event named eventName
.
Type parameters
Type parameter |
---|
N extends string | number | symbol |
Parameters
Parameter | Type |
---|---|
eventName | N |
Returns
number
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L240
off()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L185
on()
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 eventName
and 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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L146
once()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L168
prependListener()
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 eventName
and 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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L257
prependOnceListener()
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
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L279
removeAllListeners()
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
Parameter | Type |
---|---|
event ? | N |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L205
removeListener()
Alias for emitter.off(eventName, listener)
.
Type parameters
Type parameter |
---|
N extends string | number | symbol |
Parameters
Parameter | Type |
---|---|
eventName | N |
listener | (arg ) => void |
Returns
this
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L129
Interfaces
ChildProcess<O>
Since
2.0.0
Type parameters
Type parameter |
---|
O extends IOPayload |
Properties
CommandEvents
Properties
Property | Type |
---|---|
close | TerminatedPayload |
error | string |
OutputEvents<O>
Type parameters
Type parameter |
---|
O extends IOPayload |
Properties
Property | Type |
---|---|
data | O |
SpawnOptions
Since
2.0.0
Properties
Property | Type | Description |
---|---|---|
cwd? | string | Current working directory. |
encoding? | string | Character encoding for stdout/stderr Since 2.0.0 |
env? | Record <string , string > | Environment variables. set to null to clear the process env. |
TerminatedPayload
Payload for the Terminated
command event.
Properties
Type Aliases
IOPayload
Event payload type
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L566
Functions
open()
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
Parameter | Type | Description |
---|---|---|
path | string | The 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+)|(tel:\w+)|(https?://\w+)).+ . |
openWith ? | string | The app to open the file or URL with. Defaults to the system default application for the specified path type. |
Returns
Promise
<void
>
Example
Since
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L601
© 2024 Tauri Contributors. CC-BY / MIT