From 87f65eeba73346b3b18f70e237a8eef3707781ea Mon Sep 17 00:00:00 2001 From: filip mertens Date: Thu, 1 Jun 2023 10:41:22 +0200 Subject: [PATCH] refactor --- apps/1test/src/electron/fsPlugin.ts | 8 +++- apps/1test/src/global.d.ts | 12 +++--- apps/1test/src/index.html | 5 --- apps/1test/src/preload.ts | 19 ++++++--- apps/1test/src/remix/fsPlugin.ts | 9 +--- apps/1test/src/remix/gitPlugin.ts | 9 +--- ...onPluginConnector.ts => electronPlugin.ts} | 41 +++++++++---------- apps/1test/src/renderer.ts | 10 +++++ 8 files changed, 61 insertions(+), 52 deletions(-) rename apps/1test/src/remix/lib/{electronPluginConnector.ts => electronPlugin.ts} (81%) diff --git a/apps/1test/src/electron/fsPlugin.ts b/apps/1test/src/electron/fsPlugin.ts index 6098eb0a89..f04db0a904 100644 --- a/apps/1test/src/electron/fsPlugin.ts +++ b/apps/1test/src/electron/fsPlugin.ts @@ -1,6 +1,6 @@ import { PluginClient } from "@remixproject/plugin"; import { createClient } from "./lib/electronPluginClient" -import { Engine, PluginManager, Plugin } from '@remixproject/engine'; +import { Plugin } from '@remixproject/engine'; import fs from 'fs/promises' import { Stats } from "fs"; @@ -17,7 +17,6 @@ export class FSPlugin extends Plugin { } onActivation(): void { - console.log('fsPlugin onActivation') this.client = new FSPluginClient() } @@ -66,5 +65,10 @@ class FSPluginClient extends PluginClient { return fs.stat(path) } + async exists(path: string): Promise { + return fs.access(path).then(() => true).catch(() => false) + } + + } \ No newline at end of file diff --git a/apps/1test/src/global.d.ts b/apps/1test/src/global.d.ts index 01eb0b442b..eefcc940a0 100644 --- a/apps/1test/src/global.d.ts +++ b/apps/1test/src/global.d.ts @@ -1,13 +1,15 @@ export interface IElectronAPI { activatePlugin: (name: string) => Promise - receiveFromFS: (cb: any) => void - sendToFS: (message: Partial) => void - receiveFromGit: (cb: any) => void - sendToGit: (message: Partial) => void + plugins: { + name: string + activate: () => Promise + on: (cb: any) => void + send: (message: Partial) => void + }[] } declare global { interface Window { - api: IElectronAPI + electronAPI: IElectronAPI } } \ No newline at end of file diff --git a/apps/1test/src/index.html b/apps/1test/src/index.html index 219d54b9a1..a37c733059 100644 --- a/apps/1test/src/index.html +++ b/apps/1test/src/index.html @@ -1,12 +1,7 @@ - - Hello World! - -

💖 Hello World!

-

Welcome to your Electron application.

diff --git a/apps/1test/src/preload.ts b/apps/1test/src/preload.ts index 079b2f7ef9..286a5ac623 100644 --- a/apps/1test/src/preload.ts +++ b/apps/1test/src/preload.ts @@ -4,12 +4,21 @@ import { contextBridge, ipcRenderer } from 'electron' console.log('preload.ts') -contextBridge.exposeInMainWorld('api', { +/* preload script needs statically defined API for each plugin */ + +const exposedPLugins = ['fs', 'git'] + +contextBridge.exposeInMainWorld('electronAPI', { activatePlugin: (name: string) => { return ipcRenderer.invoke('engine:activatePlugin', name) }, - receiveFromFS: (cb:any) => ipcRenderer.on('fs:send', cb), - sendToFS: (message: Partial) => ipcRenderer.send('fs:on', message), - receiveFromGit: (cb:any) => ipcRenderer.on('git:send', cb), - sendToGit: (message: Partial) => ipcRenderer.send('git:on', message) + + plugins: exposedPLugins.map(name => { + return { + name, + activate: () => ipcRenderer.invoke('engine:activatePlugin', name), + on: (cb:any) => ipcRenderer.on(`${name}:send`, cb), + send: (message: Partial) => ipcRenderer.send(`${name}:on`, message) + } + }) }) \ No newline at end of file diff --git a/apps/1test/src/remix/fsPlugin.ts b/apps/1test/src/remix/fsPlugin.ts index 43f778ec12..b96672a1f8 100644 --- a/apps/1test/src/remix/fsPlugin.ts +++ b/apps/1test/src/remix/fsPlugin.ts @@ -1,17 +1,12 @@ -import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; -import { Message, Profile } from '@remixproject/plugin-utils'; -import { ElectronPluginConnector } from './lib/electronPluginConnector'; +import { ElectronPlugin } from './lib/electronPlugin'; -export class fsPlugin extends ElectronPluginConnector { +export class fsPlugin extends ElectronPlugin { constructor(){ super({ displayName: 'fs', name: 'fs', description: 'fs', - }, { - sendAPI: window.api.sendToFS, - receiveAPI: window.api.receiveFromFS }) this.methods = ['readdir', 'readFile', 'writeFile', 'mkdir', 'rmdir', 'unlink', 'rename', 'stat', 'exists'] } diff --git a/apps/1test/src/remix/gitPlugin.ts b/apps/1test/src/remix/gitPlugin.ts index d8e2c08964..dcd64baf12 100644 --- a/apps/1test/src/remix/gitPlugin.ts +++ b/apps/1test/src/remix/gitPlugin.ts @@ -1,16 +1,11 @@ -import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; -import { Message, Profile } from '@remixproject/plugin-utils'; -import { ElectronPluginConnector } from './lib/electronPluginConnector'; +import { ElectronPlugin } from './lib/electronPlugin'; -export class gitPlugin extends ElectronPluginConnector { +export class gitPlugin extends ElectronPlugin { constructor(){ super({ displayName: 'git', name: 'git', description: 'git', - },{ - sendAPI: window.api.sendToGit, - receiveAPI: window.api.receiveFromGit }) this.methods = ['log', 'status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout', 'branch', 'merge', 'reset', 'revert', 'diff', 'stash', 'apply', 'cherryPick', 'rebase', 'tag', 'fetch', 'remote', 'config', 'show', 'init', 'help', 'version'] } diff --git a/apps/1test/src/remix/lib/electronPluginConnector.ts b/apps/1test/src/remix/lib/electronPlugin.ts similarity index 81% rename from apps/1test/src/remix/lib/electronPluginConnector.ts rename to apps/1test/src/remix/lib/electronPlugin.ts index 54bdef652b..0aa82a364f 100644 --- a/apps/1test/src/remix/lib/electronPluginConnector.ts +++ b/apps/1test/src/remix/lib/electronPlugin.ts @@ -1,26 +1,30 @@ -import type { ExternalProfile, Profile, Message, PluginOptions } from '@remixproject/plugin-utils' +import type { Profile, Message } from '@remixproject/plugin-utils' import { Plugin } from '@remixproject/engine'; - -export interface ElectronPluginConnectorOptions extends PluginOptions { - sendAPI?: (message: Partial) => void - receiveAPI?: (cb: (event:any, message: Partial) => void) => void -} - - -export abstract class ElectronPluginConnector extends Plugin { +export abstract class ElectronPlugin extends Plugin { protected loaded: boolean protected id = 0 protected pendingRequest: Record void> = {} - protected options: ElectronPluginConnectorOptions + protected api: { + send: (message: Partial) => void + on: (cb: (event: any, message: any) => void) => void + } profile: Profile - constructor(profile: Profile, options: ElectronPluginConnectorOptions = {}) { + constructor(profile: Profile) { super(profile) this.loaded = false - if(!options.sendAPI || !options.receiveAPI) throw new Error('ElectronPluginConnector requires sendAPI and receiveAPI') - this.options = options - options.receiveAPI((event: any, message: any) => { + if(!window.electronAPI) throw new Error('ElectronPluginConnector requires window.api') + if(!window.electronAPI.plugins) throw new Error('ElectronPluginConnector requires window.api.plugins') + + window.electronAPI.plugins.find((plugin: any) => { + if(plugin.name === profile.name){ + this.api = plugin + return true + } + }) + + this.api.on((event: any, message: any) => { this.getMessage(message) }) } @@ -31,14 +35,14 @@ export abstract class ElectronPluginConnector extends Plugin { */ protected send(message: Partial): void { if(this.loaded) - this.options.sendAPI(message) + this.api.send(message) } /** * Open connection with the plugin * @param name The name of the plugin should connect to */ protected async connect(name: string) { - if(await window.api.activatePlugin(name) && !this.loaded){ + if(await window.electronAPI.activatePlugin(name) && !this.loaded){ this.handshake() } } @@ -58,11 +62,6 @@ export abstract class ElectronPluginConnector extends Plugin { return super.deactivate() } - /** Set options for an external plugin */ - setOptions(options: Partial = {}) { - super.setOptions(options) - } - /** Call a method from this plugin */ protected callPluginMethod(key: string, payload: any[] = []): Promise { const action = 'request' diff --git a/apps/1test/src/renderer.ts b/apps/1test/src/renderer.ts index d06b9c1ffc..62cb1c9257 100644 --- a/apps/1test/src/renderer.ts +++ b/apps/1test/src/renderer.ts @@ -9,6 +9,16 @@ class MyAppManager extends PluginManager { this.on('fs', 'loaded', async () => { const files = await this.call('fs', 'readdir', './src') console.log('files', files) + let exists = await this.call('fs', 'exists', './src') + console.log('exists', exists) + exists = await this.call('fs', 'exists', './notexists') + console.log('exists', exists) + // stat test + const stat = await this.call('fs', 'stat', './src') + console.log('stat', stat) + // read file test + const content = await this.call('fs', 'readFile', './src/index.html') + console.log('content', content) }) this.on('git', 'loaded', async () => {