diff --git a/apps/electron/forge.config.js b/apps/electron/forge.config.js index 302c3b0b1b..eb9cf5273c 100644 --- a/apps/electron/forge.config.js +++ b/apps/electron/forge.config.js @@ -52,8 +52,6 @@ module.exports = { teamId: process.env.APPLE_TEAM_ID, } : undefined, - // do we need the following line? - extraResource: ['./resources/app-update.yml'], }, makers: [ { diff --git a/apps/electron/package.json b/apps/electron/package.json index 8b1efa0fbc..bc38ab5a3a 100644 --- a/apps/electron/package.json +++ b/apps/electron/package.json @@ -52,6 +52,7 @@ "electron-window-state": "^5.0.3", "esbuild": "^0.17.19", "fs-extra": "^11.1.1", + "jotai": "^2.1.1", "playwright": "=1.33.0", "ts-node": "^10.9.1", "undici": "^5.22.1", @@ -61,8 +62,8 @@ "dependencies": { "@toeverything/plugin-infra": "workspace:*", "async-call-rpc": "^6.3.1", - "cheerio": "^1.0.0-rc.12", "electron-updater": "^5.3.0", + "link-preview-js": "^3.0.4", "lodash-es": "^4.17.21", "nanoid": "^4.0.2", "rxjs": "^7.8.1", diff --git a/apps/electron/scripts/common.mjs b/apps/electron/scripts/common.mjs index c190b6e8c1..a8cf543dcc 100644 --- a/apps/electron/scripts/common.mjs +++ b/apps/electron/scripts/common.mjs @@ -63,7 +63,7 @@ export const config = () => { bundle: true, target: `node${NODE_MAJOR_VERSION}`, platform: 'node', - external: ['electron', 'electron-updater', '@toeverything/plugin-infra'], + external: ['@toeverything/plugin-infra', 'async-call-rpc'], define: define, format: 'cjs', loader: { diff --git a/apps/electron/src/main/index.ts b/apps/electron/src/main/index.ts index 3026af929a..bba291f6ce 100644 --- a/apps/electron/src/main/index.ts +++ b/apps/electron/src/main/index.ts @@ -60,9 +60,9 @@ app.on('activate', restoreOrCreateWindow); app .whenReady() .then(registerProtocol) + .then(registerPlugin) .then(registerHandlers) .then(registerEvents) - .then(registerPlugin) .then(ensureHelperProcess) .then(restoreOrCreateWindow) .then(createApplicationMenu) diff --git a/apps/electron/src/main/plugin.ts b/apps/electron/src/main/plugin.ts index 2038b34038..445d8fd390 100644 --- a/apps/electron/src/main/plugin.ts +++ b/apps/electron/src/main/plugin.ts @@ -1,6 +1,7 @@ import { join, resolve } from 'node:path'; import { Worker } from 'node:worker_threads'; +import { logger } from '@affine/electron/main/logger'; import { AsyncCall } from 'async-call-rpc'; import { ipcMain } from 'electron'; @@ -13,24 +14,31 @@ declare global { var asyncCall: Record PromiseLike>; } -export async function registerPlugin() { +export function registerPlugin() { const pluginWorkerPath = join(__dirname, './workers/plugin.worker.js'); const asyncCall = AsyncCall< Record PromiseLike> >( - {}, + { + log: (...args: any[]) => { + logger.log('Plugin Worker', ...args); + }, + }, { channel: new MessageEventChannel(new Worker(pluginWorkerPath)), } ); globalThis.asyncCall = asyncCall; - await import('@toeverything/plugin-infra/manager').then( - ({ rootStore, affinePluginsAtom }) => { + logger.info('import plugin manager'); + import('@toeverything/plugin-infra/manager') + .then(({ rootStore, affinePluginsAtom }) => { + logger.info('import plugin manager'); const bookmarkPluginPath = join( process.env.PLUGIN_DIR ?? resolve(__dirname, './plugins'), './bookmark-block/index.mjs' ); - import('file://' + bookmarkPluginPath); + logger.info('bookmark plugin path:', bookmarkPluginPath); + import(bookmarkPluginPath); let dispose: () => void = () => { // noop }; @@ -38,7 +46,9 @@ export async function registerPlugin() { dispose(); const plugins = rootStore.get(affinePluginsAtom); Object.values(plugins).forEach(plugin => { + logger.info('register plugin', plugin.definition.id); plugin.definition.commands.forEach(command => { + logger.info('register plugin command', command); ipcMain.handle(command, (event, ...args) => asyncCall[command](...args) ); @@ -47,11 +57,14 @@ export async function registerPlugin() { dispose = () => { Object.values(plugins).forEach(plugin => { plugin.definition.commands.forEach(command => { + logger.info('unregister plugin command', command); ipcMain.removeHandler(command); }); }); }; }); - } - ); + }) + .catch(error => { + logger.error('import plugin manager error', error); + }); } diff --git a/apps/electron/src/main/workers/plugin.worker.ts b/apps/electron/src/main/workers/plugin.worker.ts index c5ef298fcb..f4bcaf12d9 100644 --- a/apps/electron/src/main/workers/plugin.worker.ts +++ b/apps/electron/src/main/workers/plugin.worker.ts @@ -11,10 +11,20 @@ if (!parentPort) { throw new Error('parentPort is undefined'); } -AsyncCall(commandProxy, { +const mainThread = AsyncCall<{ + log: (...args: any[]) => Promise; +}>(commandProxy, { channel: new MessageEventChannel(parentPort), }); +globalThis.console.log = mainThread.log; +globalThis.console.error = mainThread.log; +globalThis.console.info = mainThread.log; +globalThis.console.debug = mainThread.log; +globalThis.console.warn = mainThread.log; + +console.log('import plugin infra'); + import('@toeverything/plugin-infra/manager') .then(({ rootStore, affinePluginsAtom }) => { const bookmarkPluginPath = join( @@ -22,20 +32,34 @@ import('@toeverything/plugin-infra/manager') './bookmark-block/index.mjs' ); - import('file://' + bookmarkPluginPath); + console.log('import bookmark plugin', bookmarkPluginPath); + + import(bookmarkPluginPath).catch(console.log); rootStore.sub(affinePluginsAtom, () => { const plugins = rootStore.get(affinePluginsAtom); Object.values(plugins).forEach(plugin => { + console.log('handle plugin', plugin.definition.id); if (plugin.serverAdapter) { - plugin.serverAdapter({ - registerCommand: (command, fn) => { - console.log('register command', command); - commandProxy[command] = fn; - }, - unregisterCommand: command => { - delete commandProxy[command]; - }, - }); + try { + plugin.serverAdapter({ + registerCommand: (command, fn) => { + console.log('register command', command); + commandProxy[command] = fn; + }, + unregisterCommand: command => { + console.log('unregister command', command); + delete commandProxy[command]; + }, + }); + } catch (e) { + console.log( + 'error when handle plugin', + plugin.definition.id, + `${e}` + ); + } + } else { + console.log('no server adapter, skipping.'); } }); }); diff --git a/packages/plugin-infra/package.json b/packages/plugin-infra/package.json index b2b0266aea..146d661800 100644 --- a/packages/plugin-infra/package.json +++ b/packages/plugin-infra/package.json @@ -22,7 +22,8 @@ "@blocksuite/editor": "0.0.0-20230607055421-9b20fcaf-nightly", "@blocksuite/global": "0.0.0-20230607055421-9b20fcaf-nightly", "@blocksuite/lit": "0.0.0-20230607055421-9b20fcaf-nightly", - "@blocksuite/store": "0.0.0-20230607055421-9b20fcaf-nightly" + "@blocksuite/store": "0.0.0-20230607055421-9b20fcaf-nightly", + "jotai": "^2.1.1" }, "devDependencies": { "jotai": "^2.1.1", diff --git a/packages/plugin-infra/src/manager.ts b/packages/plugin-infra/src/manager.ts index d816b57ffb..e83fd25e90 100644 --- a/packages/plugin-infra/src/manager.ts +++ b/packages/plugin-infra/src/manager.ts @@ -32,6 +32,7 @@ export function definePlugin( if (isServer) { if (serverAdapter) { + console.log('register server adapter'); serverAdapter .load() .then(({ default: adapter }) => { diff --git a/yarn.lock b/yarn.lock index 5ff607beeb..0e55275da1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -176,7 +176,6 @@ __metadata: "@types/fs-extra": ^11.0.1 "@types/uuid": ^9.0.1 async-call-rpc: ^6.3.1 - cheerio: ^1.0.0-rc.12 cross-env: 7.0.3 electron: =25.0.1 electron-log: ^5.0.0-beta.24 @@ -185,6 +184,8 @@ __metadata: electron-window-state: ^5.0.3 esbuild: ^0.17.19 fs-extra: ^11.1.1 + jotai: ^2.1.1 + link-preview-js: ^3.0.4 lodash-es: ^4.17.21 nanoid: ^4.0.2 playwright: =1.33.0 @@ -12732,21 +12733,6 @@ __metadata: languageName: node linkType: hard -"cheerio@npm:^1.0.0-rc.12": - version: 1.0.0-rc.12 - resolution: "cheerio@npm:1.0.0-rc.12" - dependencies: - cheerio-select: ^2.1.0 - dom-serializer: ^2.0.0 - domhandler: ^5.0.3 - domutils: ^3.0.1 - htmlparser2: ^8.0.1 - parse5: ^7.0.0 - parse5-htmlparser2-tree-adapter: ^7.0.0 - checksum: 5d4c1b7a53cf22d3a2eddc0aff70cf23cbb30d01a4c79013e703a012475c02461aa1fcd99127e8d83a02216386ed6942b2c8103845fd0812300dd199e6e7e054 - languageName: node - linkType: hard - "chokidar@npm:3.5.3, chokidar@npm:^3.4.2, chokidar@npm:^3.5.2, chokidar@npm:^3.5.3": version: 3.5.3 resolution: "chokidar@npm:3.5.3"