From ae48e7cc361695a2e181880b0990e5caa78f5d85 Mon Sep 17 00:00:00 2001 From: bossgeekgo Date: Sat, 17 Feb 2024 03:23:51 +0800 Subject: [PATCH] migrate the logic of check dependencies to on app launch --- packages/ui/src/main/window/mainWindow.ts | 68 ++++++++++++++++++- .../index.vue | 1 + .../GeekAutoStartChatWithBoss.vue | 16 +---- .../renderer/src/page/Configuration/index.vue | 43 ++++++++++++ 4 files changed, 110 insertions(+), 18 deletions(-) diff --git a/packages/ui/src/main/window/mainWindow.ts b/packages/ui/src/main/window/mainWindow.ts index 48fa0e9..7b77521 100644 --- a/packages/ui/src/main/window/mainWindow.ts +++ b/packages/ui/src/main/window/mainWindow.ts @@ -9,7 +9,10 @@ import { writeConfigFile } from '@bossgeekgo/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' import { ChildProcess } from 'child_process' -import { getExpectPuppeteerExecutablePath } from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer' +import { + checkPuppeteerExecutable, + getExpectPuppeteerExecutablePath +} from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer' import * as JSONStream from 'JSONStream' let mainWindow: BrowserWindow @@ -22,8 +25,8 @@ export function createMainWindow(): void { autoHideMenuBar: true, ...(process.platform === 'linux' ? { - /* icon */ - } + /* icon */ + } : {}), webPreferences: { preload: path.join(__dirname, '../preload/index.js'), @@ -126,6 +129,65 @@ export function createMainWindow(): void { }) // TODO: }) + + ipcMain.handle('check-dependencies', async () => { + return await checkPuppeteerExecutable() + }) + + let subProcessOfCheckAndDownloadDependencies: ChildProcess + ipcMain.handle('setup-dependencies', async () => { + if (subProcessOfCheckAndDownloadDependencies) { + return + } + const subProcessEnv = { + ...process.env, + MAIN_BOSSGEEKGO_UI_RUN_MODE: 'checkAndDownloadDependenciesForInit', + PUPPETEER_EXECUTABLE_PATH: await getExpectPuppeteerExecutablePath() + } + subProcessOfCheckAndDownloadDependencies = childProcess.spawn( + process.argv[0], + process.argv.slice(1), + { + env: subProcessEnv, + stdio: [null, null, null, 'pipe'] + } + ) + return new Promise((resolve) => { + subProcessOfCheckAndDownloadDependencies!.stdio[3]!.pipe(JSONStream.parse()).on( + 'data', + (raw) => { + const data = raw + switch (data.type) { + case 'PUPPETEER_DOWNLOAD_FINISHED': { + mainWindow.webContents.send(data.type, data) + resolve(data) + break + } + case 'NEED_RESETUP_DEPENDENCIES': + case 'PUPPETEER_DOWNLOAD_PROGRESS': { + mainWindow.webContents.send(data.type, data) + break + } + default: { + return + } + // case 'PUPPETEER_DOWNLOAD_ERROR': { + // subProcessOfCheckAndDownloadDependencies?.kill() + // pipe?.write(JSON.stringify(data) + '\r\n') + // resolve(data) + // break + // } + // case 'PUPPETEER_MAY_NOT_INSTALLED': { + // pipe?.write(JSON.stringify(data) + '\r\n') + // resolve(data) + // break + // } + } + } + ) + }) + }) + ipcMain.handle('stop-geek-auto-start-chat-with-boss', async () => { mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopping') subProcessOfPuppeteer?.kill('SIGINT') diff --git a/packages/ui/src/renderer/src/features/DependenciesSetupProgressIndicatorDialog/index.vue b/packages/ui/src/renderer/src/features/DependenciesSetupProgressIndicatorDialog/index.vue index 7a5c6ce..cbbc0f9 100644 --- a/packages/ui/src/renderer/src/features/DependenciesSetupProgressIndicatorDialog/index.vue +++ b/packages/ui/src/renderer/src/features/DependenciesSetupProgressIndicatorDialog/index.vue @@ -1,5 +1,6 @@ diff --git a/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue b/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue index 19d58d3..fddf75c 100644 --- a/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue +++ b/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue @@ -31,11 +31,10 @@