mirror of
https://github.com/tauri-apps/tauri-plugin-cli.git
synced 2026-01-31 00:45:18 +01:00
feat(plugins): inject API on window.__TAURI__ (#383)
Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/5060238742 Co-authored-by: lucasfernog <lucasfernog@users.noreply.github.com>
This commit is contained in:
committed by
tauri-bot
parent
19d342961d
commit
a3f150877f
10
dist-js/index.d.ts
vendored
10
dist-js/index.d.ts
vendored
@@ -1,3 +1,13 @@
|
||||
/**
|
||||
* Parse arguments from your Command Line Interface.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
8
dist-js/index.min.js
vendored
8
dist-js/index.min.js
vendored
@@ -1,8 +1,6 @@
|
||||
var f$1=Object.defineProperty;var g=(a,b)=>{for(var c in b)f$1(a,c,{get:b[c],enumerable:!0});};var e=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h$1=(a,b,c)=>(e(a,b,"read from private field"),c?c.call(a):b.get(a)),i=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j=(a,b,c,d)=>(e(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c);
|
||||
|
||||
var f={};g(f,{Channel:()=>a,PluginListener:()=>c,addPluginListener:()=>v,convertFileSrc:()=>m,invoke:()=>u,transformCallback:()=>o});function h(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function o(t,e=!1){let n=h(),r=`_${n}`;return Object.defineProperty(window,r,{value:i=>(e&&Reflect.deleteProperty(window,r),t?.(i)),writable:!1,configurable:!0}),n}var s,a=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i(this,s,()=>{});this.id=o(e=>{h$1(this,s).call(this,e);});}set onmessage(e){j(this,s,e);}get onmessage(){return h$1(this,s)}toJSON(){return `__CHANNEL__:${this.id}`}};s=new WeakMap;var c=class{constructor(e,n,r){this.plugin=e,this.event=n,this.channelId=r;}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function v(t,e,n){let r=new a;return r.onmessage=n,u(`plugin:${t}|register_listener`,{event:e,handler:r}).then(()=>new c(t,e,r.id))}async function u(t,e={}){return new Promise((n,r)=>{let i=o(d=>{n(d),Reflect.deleteProperty(window,`_${g}`);},!0),g=o(d=>{r(d),Reflect.deleteProperty(window,`_${i}`);},!0);window.__TAURI_IPC__({cmd:t,callback:i,error:g,...e});})}function m(t,e="asset"){let n=encodeURIComponent(t);return navigator.userAgent.includes("Windows")?`https://${e}.localhost/${n}`:`${e}://localhost/${n}`}
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* Parse the arguments provided to the current process and get the matches using the configuration defined [`tauri.cli`](https://tauri.app/v1/api/config/#tauriconfig.cli) in `tauri.conf.json`
|
||||
*
|
||||
@@ -25,7 +23,7 @@ var f={};g(f,{Channel:()=>a,PluginListener:()=>c,addPluginListener:()=>v,convert
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getMatches() {
|
||||
return await u("plugin:cli|cli_matches");
|
||||
return await window.__TAURI_INVOKE__("plugin:cli|cli_matches");
|
||||
}
|
||||
|
||||
export { getMatches };
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri';
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* Parse the arguments provided to the current process and get the matches using the configuration defined [`tauri.cli`](https://tauri.app/v1/api/config/#tauriconfig.cli) in `tauri.conf.json`
|
||||
*
|
||||
@@ -23,7 +23,7 @@ import { invoke } from '@tauri-apps/api/tauri';
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getMatches() {
|
||||
return await invoke("plugin:cli|cli_matches");
|
||||
return await window.__TAURI_INVOKE__("plugin:cli|cli_matches");
|
||||
}
|
||||
|
||||
export { getMatches };
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../guest-js/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;AA4CA;;;;;;;;;;;;;;;;;;;;AAoBG;AACH,eAAe,UAAU,GAAA;AACvB,IAAA,OAAO,MAAM,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAChD;;;;"}
|
||||
{"version":3,"file":"index.mjs","sources":["../guest-js/index.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;AACA;AACA;AA8CA;;;;;;;;;;;;;;;;;;;;AAoBG;AACH,eAAe,UAAU,GAAA;AACvB,IAAA,OAAO,MAAM,MAAM,CAAC,gBAAgB,CAAC,wBAAwB,CAAC,CAAC;AACjE;;;;"}
|
||||
@@ -8,7 +8,11 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
@@ -64,7 +68,7 @@ interface CliMatches {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getMatches(): Promise<CliMatches> {
|
||||
return await invoke("plugin:cli|cli_matches");
|
||||
return await window.__TAURI_INVOKE__("plugin:cli|cli_matches");
|
||||
}
|
||||
|
||||
export type { ArgMatch, SubcommandMatch, CliMatches };
|
||||
|
||||
1
src/api-iife.js
Normal file
1
src/api-iife.js
Normal file
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_CLI__=function(_){"use strict";return _.getMatches=async function(){return await window.__TAURI_INVOKE__("plugin:cli|cli_matches")},_}({});Object.defineProperty(window.__TAURI__,"cli",{value:__TAURI_CLI__})}
|
||||
@@ -40,6 +40,7 @@ fn cli_matches<R: Runtime>(_app: AppHandle<R>, cli: State<'_, Cli<R>>) -> Result
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Config> {
|
||||
Builder::new("cli")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![cli_matches])
|
||||
.setup(|app, api| {
|
||||
app.manage(Cli(api));
|
||||
|
||||
Reference in New Issue
Block a user