@tauri-apps/plugin-shell
访问系统 Shell。允许您生成子进程并使用其默认应用程序管理文件和 URL。
安全性
此 API 具有作用域配置,强制您限制可以使用的程序和参数。
限制对 open
API 的访问
在配置对象上,open: true
表示 open API 可以与任何 URL 一起使用,因为参数已通过 ^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+
正则表达式验证。您可以通过将布尔值更改为字符串来更改该正则表达式,例如 open: ^https://github.com/
。
限制对 Command
API 的访问
插件权限对象具有一个 scope
字段,该字段定义了可以使用的 CLI 数组。每个 CLI 都是一个配置对象 { name: string, cmd: string, sidecar?: bool, args?: boolean | Arg[] }
。
name
:命令的唯一标识符,传递给 Command.create 函数。如果是 Sidecar,则必须是tauri.conf.json > bundle > externalBin
上定义的值。cmd
:在此配置上执行的程序。如果是 Sidecar,则忽略此值。sidecar
:对象是配置 Sidecar 还是系统程序。args
:可以传递给程序的参数。默认情况下,不允许任何参数。true
表示允许任何参数列表。false
表示不允许任何参数。- 否则,可以配置一个数组。每个项目可以是表示固定参数值的字符串,也可以是定义正则表达式以验证参数值的
{ validator: string }
。
作用域配置示例
CLI:git commit -m "the commit message"
功能
{ "permissions": [ { "identifier": "shell:allow-execute", "allow": [ { "name": "run-git-commit", "cmd": "git", "args": ["commit", "-m", { "validator": "\\S+" }] } ] } ]}
用法
import { Command } from '@tauri-apps/plugin-shell'Command.create('run-git-commit', ['commit', '-m', 'the commit message'])
尝试使用作用域上未配置的程序执行任何 API 都会导致由于拒绝访问而拒绝 Promise。
类
Child
始于
2.0.0
构造函数
new Child()
new Child(pid): Child
参数
参数 | 类型 |
---|---|
pid | number |
返回值
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L301
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
pid | number | 子进程 pid 。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L299 |
方法
kill()
kill(): Promise<void>
杀死子进程。
返回值
Promise
<void
>
指示操作成功或失败的 Promise。
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L336
write()
write(data): Promise<void>
将 data
写入 stdin
。
参数
参数 | 类型 | 描述 |
---|---|---|
data | IOPayload | number [] | 要写入的消息,可以是字符串或字节数组。 |
返回值
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.create('node');const child = await command.spawn();await child.write('message');await child.write([0, 1, 2, 3, 4, 5]);
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L322
Command<O>
用于生成子进程的入口点。它发出 close
和 error
事件。
示例
import { Command } from '@tauri-apps/plugin-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);
始于
2.0.0
继承自
类型参数
类型参数 |
---|
O 继承自 IOPayload |
属性
属性 | 修饰符 | 类型 | 描述 | 定义在 |
---|---|---|---|---|
stderr | 只读 | EventEmitter <OutputEvents <O >> | stderr 的事件发射器。发出 data 事件。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L384 |
stdout | 只读 | EventEmitter <OutputEvents <O >> | stdout 的事件发射器。发出 data 事件。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L382 |
方法
addListener()
addListener<N>(eventName, listener): this
emitter.on(eventName, listener)
的别名。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L118
execute()
execute(): Promise<ChildProcess<O>>
将命令作为子进程执行,等待其完成并收集其所有输出。
返回值
Promise
<ChildProcess
<O
>>
解析为子进程输出的 Promise。
示例
import { Command } from '@tauri-apps/plugin-shell';const output = await Command.create('echo', 'message').execute();assert(output.code === 0);assert(output.signal === null);assert(output.stdout === 'message');assert(output.stderr === '');
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L530
listenerCount()
listenerCount<N>(eventName): number
返回侦听名为 eventName
的事件的侦听器数量。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
返回值
number
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L241
off()
off<N>(eventName, listener): this
从事件 eventName 的侦听器数组中删除所有指定的侦听器。返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L186
on()
on<N>(eventName, listener): this
将 listener
函数添加到名为 eventName
的事件的侦听器数组的末尾。不检查是否已添加 listener
。多次调用传递相同的 eventName
和 listener
组合将导致 listener
被添加和调用多次。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L147
once()
once<N>(eventName, listener): this
为名为 eventName
的事件添加一次性listener
函数。下次触发 eventName
时,将删除此侦听器,然后调用它。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L169
prependListener()
prependListener<N>(eventName, listener): this
将 listener
函数添加到名为 eventName
的事件的侦听器数组的开头。不检查是否已添加 listener
。多次调用传递相同的 eventName
和 listener
组合将导致 listener
被添加和调用多次。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L258
prependOnceListener()
prependOnceListener<N>(eventName, listener): this
为名为 eventName
的事件添加到侦听器数组开头的一次性listener
函数。下次触发 eventName
时,将删除此侦听器,然后调用它。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
EventEmitter
.prependOnceListener
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L280
removeAllListeners()
removeAllListeners<N>(event?): this
删除所有侦听器,或指定 eventName 的侦听器。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
event ? | N |
返回值
this
始于
2.0.0
继承自
EventEmitter
.removeAllListeners
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L206
removeListener()
removeListener<N>(eventName, listener): this
emitter.off(eventName, listener)
的别名。
类型参数
类型参数 |
---|
N 继承自 keyof CommandEvents |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L130
spawn()
spawn(): Promise<Child>
将命令作为子进程执行,返回其句柄。
返回值
解析为子进程句柄的 Promise。
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L479
create()
创建用于执行给定程序的命令。
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.create('my-app', ['run', 'tauri']);const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
create(program, args)
static create(program, args?): Command<string>
创建用于执行给定程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
返回值
Command
<string
>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.create('my-app', ['run', 'tauri']);const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L406
create(program, args, options)
static create( program, args?,options?): Command<Uint8Array<ArrayBufferLike>>
创建用于执行给定程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
options ? | SpawnOptions & object |
返回值
Command
<Uint8Array
<ArrayBufferLike
>>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.create('my-app', ['run', 'tauri']);const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L407
create(program, args, options)
static create( program, args?,options?): Command<string>
创建用于执行给定程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
options ? | SpawnOptions |
返回值
Command
<string
>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.create('my-app', ['run', 'tauri']);const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L412
sidecar()
创建用于执行给定 Sidecar 程序的命令。
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.sidecar('my-sidecar');const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
sidecar(program, args)
static sidecar(program, args?): Command<string>
创建用于执行给定 Sidecar 程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
返回值
Command
<string
>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.sidecar('my-sidecar');const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L438
sidecar(program, args, options)
static sidecar( program, args?,options?): Command<Uint8Array<ArrayBufferLike>>
创建用于执行给定 Sidecar 程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
options ? | SpawnOptions & object |
返回值
Command
<Uint8Array
<ArrayBufferLike
>>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.sidecar('my-sidecar');const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L439
sidecar(program, args, options)
static sidecar( program, args?,options?): Command<string>
创建用于执行给定 Sidecar 程序的命令。
参数
参数 | 类型 |
---|---|
program | string |
args ? | string | string [] |
options ? | SpawnOptions |
返回值
Command
<string
>
示例
import { Command } from '@tauri-apps/plugin-shell';const command = Command.sidecar('my-sidecar');const output = await command.execute();
参数
要执行的程序。它必须在 tauri.conf.json > plugins > shell > scope
上配置。
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L444
EventEmitter<E>
始于
2.0.0
扩展自
类型参数
类型参数 |
---|
E 继承自 Record <string , any > |
构造函数
new EventEmitter()
new EventEmitter<E>(): EventEmitter<E>
返回值
EventEmitter
<E
>
方法
addListener()
addListener<N>(eventName, listener): this
emitter.on(eventName, listener)
的别名。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L118
listenerCount()
listenerCount<N>(eventName): number
返回侦听名为 eventName
的事件的侦听器数量。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
返回值
number
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L241
off()
off<N>(eventName, listener): this
从事件 eventName 的侦听器数组中删除所有指定的侦听器。返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L186
on()
on<N>(eventName, listener): this
将 listener
函数添加到名为 eventName
的事件的侦听器数组的末尾。不检查是否已添加 listener
。多次调用传递相同的 eventName
和 listener
组合将导致 listener
被添加和调用多次。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L147
once()
once<N>(eventName, listener): this
为名为 eventName
的事件添加一次性listener
函数。下次触发 eventName
时,将删除此侦听器,然后调用它。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L169
prependListener()
prependListener<N>(eventName, listener): this
将 listener
函数添加到名为 eventName
的事件的侦听器数组的开头。不检查是否已添加 listener
。多次调用传递相同的 eventName
和 listener
组合将导致 listener
被添加和调用多次。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L258
prependOnceListener()
prependOnceListener<N>(eventName, listener): this
为名为 eventName
的事件添加到侦听器数组开头的一次性listener
函数。下次触发 eventName
时,将删除此侦听器,然后调用它。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L280
removeAllListeners()
removeAllListeners<N>(event?): this
删除所有侦听器,或指定 eventName 的侦听器。
返回对 EventEmitter
的引用,以便可以链式调用。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
event ? | N |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L206
removeListener()
removeListener<N>(eventName, listener): this
emitter.off(eventName, listener)
的别名。
类型参数
类型参数 |
---|
N 继承自 string | number | symbol |
参数
参数 | 类型 |
---|---|
eventName | N |
listener | (arg ) => void |
返回值
this
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L130
接口
ChildProcess<O>
始于
2.0.0
类型参数
类型参数 |
---|
O 继承自 IOPayload |
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
code | null | number | 进程的退出代码。null 如果进程在 Unix 上被信号终止。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L94 |
signal | null | number | 如果进程被信号终止,则表示该信号。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L96 |
stderr | O | 进程写入 stderr 的数据。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L100 |
stdout | O | 进程写入 stdout 的数据。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L98 |
CommandEvents
属性
OutputEvents<O>
类型参数
类型参数 |
---|
O 继承自 IOPayload |
属性
属性 | 类型 | 定义在 |
---|---|---|
data | O | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L350 |
SpawnOptions
始于
2.0.0
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
cwd? | string | 当前工作目录。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L73 |
encoding? | string | stdout/stderr 的字符编码 始于 2.0.0 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L81 |
env? | Record <string , string > | 环境变量。设置为 null 以清除进程 env。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L75 |
TerminatedPayload
用于 Terminated
命令事件的负载。
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
code | null | number | 进程的退出代码。null 如果进程在 Unix 上被信号终止。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L560 |
signal | null | number | 如果进程被信号终止,则表示该信号。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L562 |
类型别名
IOPayload
type IOPayload: string | Uint8Array;
事件负载类型
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L566
函数
open()
function open(path, openWith?): Promise<void>
使用系统的默认应用或使用 openWith
指定的应用打开路径或 URL。
openWith
值必须是 firefox
、google chrome
、chromium
safari
、open
、start
、xdg-open
、gio
、gnome-open
、kde-open
或 wslview
之一。
参数
参数 | 类型 | 描述 |
---|---|---|
path | string | 要打开的路径或 URL。此值与 tauri.conf.json > plugins > shell > open 上定义的字符串正则表达式匹配,默认为 `^((mailto:\w+) |
openWith ? | string | 用于打开文件或 URL 的应用程序。默认为指定路径类型的系统默认应用程序。 |
返回值
Promise
<void
>
示例
import { open } from '@tauri-apps/plugin-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');
始于
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L601
© 2025 Tauri Contributors. CC-BY / MIT