@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 |
回傳
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L301
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
pid | number | 子程序的 pid 。 | 來源: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
來源: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
來源: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 extends IOPayload |
屬性
屬性 | 修飾詞 | 類型 | 描述 | 定義於 |
---|---|---|---|---|
stderr | readonly | EventEmitter <OutputEvents <O >> | stderr 的事件發射器。發出 data 事件。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L384 |
stdout | readonly | EventEmitter <OutputEvents <O >> | stdout 的事件發射器。發出 data 事件。 | 來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源: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
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L530
listenerCount()
listenerCount<N>(eventName): number
傳回監聽名為 eventName
的事件的監聽器數量。
類型參數
類型參數 |
---|
N extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
回傳
number
自從
2.0.0
繼承自
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
EventEmitter
.prependOnceListener
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L280
removeAllListeners()
removeAllListeners<N>(event?): this
移除所有監聽器,或指定 eventName 的監聽器。
傳回 EventEmitter
的參考,以便可以鏈式呼叫。
類型參數
類型參數 |
---|
N extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
event ? | N |
回傳
this
自從
2.0.0
繼承自
EventEmitter
.removeAllListeners
來源: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 extends keyof CommandEvents |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
繼承自
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L130
spawn()
spawn(): Promise<Child>
將命令作為子程序執行,並傳回其控制代碼。
回傳
解析為子程序控制代碼的 Promise。
自從
2.0.0
來源: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
上設定。
來源: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
上設定。
來源: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
上設定。
來源: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
上設定。
來源: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
上設定。
來源: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
上設定。
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L444
EventEmitter<E>
自從
2.0.0
擴展自
類型參數
類型參數 |
---|
E extends Record <string , any > |
建構函式
new EventEmitter()
new EventEmitter<E>(): EventEmitter<E>
回傳
EventEmitter
<E
>
方法
addListener()
addListener<N>(eventName, listener): this
emitter.on(eventName, listener)
的別名。
類型參數
類型參數 |
---|
N extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L118
listenerCount()
listenerCount<N>(eventName): number
傳回監聽名為 eventName
的事件的監聽器數量。
類型參數
類型參數 |
---|
N extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
回傳
number
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L280
removeAllListeners()
removeAllListeners<N>(event?): this
移除所有監聽器,或指定 eventName 的監聽器。
傳回 EventEmitter
的參考,以便可以鏈式呼叫。
類型參數
類型參數 |
---|
N extends string | number | symbol |
參數
參數 | 類型 |
---|---|
event ? | N |
回傳
this
自從
2.0.0
來源: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 extends string | number | symbol |
參數
參數 | 類型 |
---|---|
eventName | N |
listener | (arg ) => void |
回傳
this
自從
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L130
介面
ChildProcess<O>
自從
2.0.0
類型參數
類型參數 |
---|
O extends IOPayload |
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
code | null | number | 程序的結束代碼。如果程序在 Unix 上被訊號終止,則為 null 。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L94 |
signal | null | number | 如果程序被訊號終止,則表示該訊號。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L96 |
stderr | O | 程序寫入 stderr 的資料。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L100 |
stdout | O | 程序寫入 stdout 的資料。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L98 |
CommandEvents
屬性
OutputEvents<O>
類型參數
類型參數 |
---|
O extends IOPayload |
屬性
屬性 | 類型 | 定義於 |
---|---|---|
data | O | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L350 |
SpawnOptions
自從
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
cwd? | string | 目前工作目錄。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L73 |
encoding? | string | stdout/stderr 的字元編碼 自從 2.0.0 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L81 |
env? | Record <string , string > | 環境變數。設定為 null 以清除程序環境。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L75 |
TerminatedPayload
Terminated
命令事件的 Payload。
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
code | null | number | 程序的結束代碼。如果程序在 Unix 上被訊號終止,則為 null 。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L560 |
signal | null | number | 如果程序被訊號終止,則表示該訊號。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L562 |
類型別名
IOPayload
type IOPayload: string | Uint8Array;
事件 Payload 類型
來源: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
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/shell/guest-js/index.ts#L601
© 2025 Tauri 貢獻者。CC-BY / MIT