feat(plugins): inject API on window.__TAURI__ (#383)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-23 10:20:14 -07:00
committed by GitHub
parent 3c8577bc9a
commit b131bc8f7c
78 changed files with 754 additions and 337 deletions
+13 -6
View File
@@ -62,7 +62,14 @@
* @module
*/
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
transformCallback: <T>(cb: (payload: T) => void) => number;
};
}
}
/**
* @since 1.0.0
@@ -119,11 +126,11 @@ async function execute<O extends IOPayload>(
Object.freeze(args);
}
return invoke<number>("plugin:shell|execute", {
return window.__TAURI_INVOKE__<number>("plugin:shell|execute", {
program,
args,
options,
onEventFn: transformCallback(onEvent),
onEventFn: window.__TAURI__.transformCallback(onEvent),
});
}
@@ -345,7 +352,7 @@ class Child {
* @returns A promise indicating the success or failure of the operation.
*/
async write(data: IOPayload): Promise<void> {
return invoke("plugin:shell|stdin_write", {
return window.__TAURI_INVOKE__("plugin:shell|stdin_write", {
pid: this.pid,
// correctly serialize Uint8Arrays
buffer: typeof data === "string" ? data : Array.from(data),
@@ -358,7 +365,7 @@ class Child {
* @returns A promise indicating the success or failure of the operation.
*/
async kill(): Promise<void> {
return invoke("plugin:shell|kill", {
return window.__TAURI_INVOKE__("plugin:shell|kill", {
cmd: "killChild",
pid: this.pid,
});
@@ -629,7 +636,7 @@ type CommandEvent<O extends IOPayload> =
* @since 1.0.0
*/
async function open(path: string, openWith?: string): Promise<void> {
return invoke("plugin:shell|open", {
return window.__TAURI_INVOKE__("plugin:shell|open", {
path,
with: openWith,
});
+1
View File
@@ -0,0 +1 @@
if("__TAURI__"in window){var __TAURI_SHELL__=function(e){"use strict";class t{constructor(){this.eventListeners=Object.create(null)}addListener(e,t){return this.on(e,t)}removeListener(e,t){return this.off(e,t)}on(e,t){return e in this.eventListeners?this.eventListeners[e].push(t):this.eventListeners[e]=[t],this}once(e,t){const s=n=>{this.removeListener(e,s),t(n)};return this.addListener(e,s)}off(e,t){return e in this.eventListeners&&(this.eventListeners[e]=this.eventListeners[e].filter((e=>e!==t))),this}removeAllListeners(e){return e?delete this.eventListeners[e]:this.eventListeners=Object.create(null),this}emit(e,t){if(e in this.eventListeners){const s=this.eventListeners[e];for(const e of s)e(t);return!0}return!1}listenerCount(e){return e in this.eventListeners?this.eventListeners[e].length:0}prependListener(e,t){return e in this.eventListeners?this.eventListeners[e].unshift(t):this.eventListeners[e]=[t],this}prependOnceListener(e,t){const s=n=>{this.removeListener(e,s),t(n)};return this.prependListener(e,s)}}class s{constructor(e){this.pid=e}async write(e){return window.__TAURI_INVOKE__("plugin:shell|stdin_write",{pid:this.pid,buffer:"string"==typeof e?e:Array.from(e)})}async kill(){return window.__TAURI_INVOKE__("plugin:shell|kill",{cmd:"killChild",pid:this.pid})}}class n extends t{constructor(e,s=[],n){super(),this.stdout=new t,this.stderr=new t,this.program=e,this.args="string"==typeof s?[s]:s,this.options=null!=n?n:{}}static create(e,t=[],s){return new n(e,t,s)}static sidecar(e,t=[],s){const r=new n(e,t,s);return r.options.sidecar=!0,r}async spawn(){return async function(e,t,s=[],n){return"object"==typeof s&&Object.freeze(s),window.__TAURI_INVOKE__("plugin:shell|execute",{program:t,args:s,options:n,onEventFn:window.__TAURI__.transformCallback(e)})}((e=>{switch(e.event){case"Error":this.emit("error",e.payload);break;case"Terminated":this.emit("close",e.payload);break;case"Stdout":this.stdout.emit("data",e.payload);break;case"Stderr":this.stderr.emit("data",e.payload)}}),this.program,this.args,this.options).then((e=>new s(e)))}async execute(){return new Promise(((e,t)=>{this.on("error",t);const s=[],n=[];this.stdout.on("data",(e=>{s.push(e)})),this.stderr.on("data",(e=>{n.push(e)})),this.on("close",(t=>{e({code:t.code,signal:t.signal,stdout:this.collectOutput(s),stderr:this.collectOutput(n)})})),this.spawn().catch(t)}))}collectOutput(e){return"raw"===this.options.encoding?e.reduce(((e,t)=>new Uint8Array([...e,...t,10])),new Uint8Array):e.join("\n")}}return e.Child=s,e.Command=n,e.EventEmitter=t,e.open=async function(e,t){return window.__TAURI_INVOKE__("plugin:shell|open",{path:e,with:t})},e}({});Object.defineProperty(window.__TAURI__,"shell",{value:__TAURI_SHELL__})}
+4 -1
View File
@@ -67,8 +67,11 @@ impl<R: Runtime, T: Manager<R>> ShellExt<R> for T {
}
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
let mut init_script = include_str!("init.js").to_string();
init_script.push_str(include_str!("api-iife.js"));
Builder::<R, Option<Config>>::new("shell")
.js_init_script(include_str!("init.js").to_string())
.js_init_script(init_script)
.invoke_handler(tauri::generate_handler![
commands::execute,
commands::stdin_write,