diff --git a/electron/.gitignore b/electron/.gitignore index 51f8683ef..4ba7a076a 100644 --- a/electron/.gitignore +++ b/electron/.gitignore @@ -5,6 +5,7 @@ package-lock.json build/ dist/ +updater/* .devload .native diff --git a/electron/electron.js b/electron/electron.js index 86a20bbc2..843c4cbbb 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -20,6 +20,7 @@ const isMac = process.platform === 'darwin' const isWin = process.platform === 'win32' const allowedUrls = /^(?:https?|mailto|tel|callto):/i; const allowedCalls = /^(?:mailto|tel|callto):/i; +let updaterLockFile = path.join(os.tmpdir(), '.dootask_updater.lock'); let enableStoreBkp = true; let dialogOpen = false; let enablePlugins = false; @@ -183,6 +184,55 @@ function createMainWindow() { }) } +/** + * 创建更新程序子进程 + */ +function createUpdaterWindow() { + // 检查平台是否支持 + if (!['darwin', 'win32'].includes(process.platform)) { + return; + } + + try { + // 构建updater应用路径 + let updaterPath; + if (isWin) { + updaterPath = path.join(__dirname, 'updater', 'updater.exe'); + } else { + updaterPath = path.join(__dirname, 'updater', 'updater'); + } + + // 检查updater应用是否存在 + if (!fs.existsSync(updaterPath)) { + console.log('Updater not found:', updaterPath); + return; + } + + // 创建锁文件 + fs.writeFileSync(updaterLockFile, '1'); + + // 启动子进程,传入锁文件路径作为第一个参数 + const child = spawn(updaterPath, [updaterLockFile], { + detached: true, + stdio: 'ignore', + env: { + ...process.env, + ELECTRON_RUN_AS_NODE: '1', + UPDATER_LOCK_FILE: updaterLockFile + } + }); + + child.unref(); + + child.on('error', (err) => { + console.log('Updater process error:', err); + }); + + } catch (e) { + console.log('Failed to create updater process:', e); + } +} + /** * 创建子窗口 * @param args {path, hash, title, titleFixed, force, userAgent, config, webPreferences} @@ -686,6 +736,14 @@ if (!getTheLock) { mainTray.setContextMenu(trayMenu) } } + // 删除updater锁文件(如果存在) + if (fs.existsSync(updaterLockFile)) { + try { + fs.unlinkSync(updaterLockFile); + } catch (e) { + //忽略错误 + } + } // if (process.platform === 'win32') { app.setAppUserModelId(config.name) @@ -1215,13 +1273,20 @@ ipcMain.on('mainWindowActive', (event) => { */ ipcMain.on('updateQuitAndInstall', (event) => { event.returnValue = "ok" + + // 关闭所有子窗口 willQuitApp = true childWindow.some(({browser}) => { browser && browser.destroy() }) + + // 启动更新子窗口 + createUpdaterWindow() + + // 退出并安装更新 setTimeout(_ => { autoUpdater.quitAndInstall(true, true) - }, 1) + }, 1000) }) //================================================================ diff --git a/electron/package.json b/electron/package.json index a11f59a1f..6c08a00a5 100755 --- a/electron/package.json +++ b/electron/package.json @@ -71,6 +71,7 @@ "files": [ "render/**/*", "public/**/*", + "updater/**/*", "electron-menu.js", "electron-preload.js", "electron.js",