24 changed files with 194 additions and 55 deletions
@ -0,0 +1,7 @@ |
|||||
|
const keys = ["progress"] as const |
||||
|
|
||||
|
type AllKeys = (typeof keys)[number] |
||||
|
|
||||
|
export type{ |
||||
|
AllKeys |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
import { broadcast } from "main/utils" |
||||
|
import { AllKeys } from "./common" |
||||
|
|
||||
|
function emitProgress(...argu) { |
||||
|
broadcast<AllKeys>("progress", ...argu) |
||||
|
} |
||||
|
|
||||
|
export { |
||||
|
emitProgress |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
import type { AllKeys } from "./common" |
||||
|
|
||||
|
const curProgress = ref(0) |
||||
|
api.on<AllKeys>("progress", () => { |
||||
|
curProgress.value = 10 |
||||
|
}) |
||||
|
|
||||
|
function useUpdate() { |
||||
|
return { |
||||
|
curProgress, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export { useUpdate } |
@ -0,0 +1,12 @@ |
|||||
|
// import { inject } from "inversify"
|
||||
|
// import Setting from "main/modules/setting"
|
||||
|
|
||||
|
// class SettingCommand {
|
||||
|
// constructor(@inject(Setting) private _Setting: Setting) {
|
||||
|
// console.log(this._Setting)
|
||||
|
// }
|
||||
|
|
||||
|
// getAll() {
|
||||
|
// return this._Setting.config()
|
||||
|
// }
|
||||
|
// }
|
@ -0,0 +1,62 @@ |
|||||
|
import { spawn } from "node:child_process" |
||||
|
import fs from "node:fs" |
||||
|
import path from "node:path" |
||||
|
import os from "node:os" |
||||
|
import { app } from "electron" |
||||
|
|
||||
|
function getUpdateScriptTemplate() { |
||||
|
return process.platform === "win32" |
||||
|
? ` |
||||
|
@echo off |
||||
|
timeout /t 2 |
||||
|
taskkill /IM "{{EXE_NAME}}" /F |
||||
|
xcopy /Y /E "{{UPDATE_DIR}}\\*" "{{APP_PATH}}" |
||||
|
start "" "{{EXE_PATH}}" |
||||
|
` |
||||
|
: ` |
||||
|
#!/bin/bash |
||||
|
sleep 2 |
||||
|
pkill -f "{{EXE_NAME}}" |
||||
|
cp -Rf "{{UPDATE_DIR}}/*" "{{APP_PATH}}/" |
||||
|
open "{{EXE_PATH}}" |
||||
|
` |
||||
|
} |
||||
|
|
||||
|
function generateUpdateScript() { |
||||
|
const scriptContent = getUpdateScriptTemplate() |
||||
|
.replace(/{{APP_PATH}}/g, process.platform === "win32" ? "%APP_PATH%" : "$APP_PATH") |
||||
|
.replace(/{{UPDATE_DIR}}/g, process.platform === "win32" ? "%UPDATE_DIR%" : "$UPDATE_DIR") |
||||
|
.replace(/{{EXE_PATH}}/g, process.platform === "win32" ? "%EXE_PATH%" : "$EXE_PATH") |
||||
|
.replace(/{{EXE_NAME}}/g, process.platform === "win32" ? "%EXE_NAME%" : "$EXE_NAME") |
||||
|
|
||||
|
const scriptPath = path.join(os.tmpdir(), `update.${process.platform === "win32" ? "bat" : "sh"}`) |
||||
|
fs.writeFileSync(scriptPath, scriptContent) |
||||
|
return scriptPath |
||||
|
} |
||||
|
|
||||
|
app.on("will-quit", event => { |
||||
|
event.preventDefault() |
||||
|
|
||||
|
// 假设已下载更新到临时目录
|
||||
|
const updateTempDir = path.join(os.tmpdir(), "app-update") |
||||
|
const appPath = app.getAppPath() |
||||
|
const appExePath = process.execPath |
||||
|
|
||||
|
// 生成动态脚本
|
||||
|
const scriptPath = generateUpdateScript() |
||||
|
|
||||
|
fs.chmodSync(scriptPath, 0o755) |
||||
|
|
||||
|
// 执行脚本
|
||||
|
const child = spawn(scriptPath, [], { |
||||
|
detached: true, |
||||
|
shell: true, |
||||
|
env: { |
||||
|
APP_PATH: appPath, |
||||
|
UPDATE_DIR: updateTempDir, |
||||
|
EXE_PATH: appExePath, |
||||
|
}, |
||||
|
}) |
||||
|
child.unref() |
||||
|
app.exit() |
||||
|
}) |
@ -1,8 +0,0 @@ |
|||||
import { ElectronAPI } from "@electron-toolkit/preload" |
|
||||
|
|
||||
declare global { |
|
||||
interface Window { |
|
||||
electron: ElectronAPI |
|
||||
api: unknown |
|
||||
} |
|
||||
} |
|
@ -1 +0,0 @@ |
|||||
declare const api |
|
@ -0,0 +1,21 @@ |
|||||
|
|
||||
|
type Api = { |
||||
|
call: (command: string, ...args: any[]) => Promise<any> |
||||
|
callLong: (command: string, ...args: any[]) => Promise<any> |
||||
|
callSync: (command: string, ...args: any[]) => any |
||||
|
send: (command: string, ...argu: any[]) => any |
||||
|
sendSync: (command: string, ...argu: any[]) => any |
||||
|
on: <T extends string>(command: T, cb: (event: IpcRendererEvent, ...args: any[]) => void) => () => void |
||||
|
once: (command: string, cb: (event: IpcRendererEvent, ...args: any[]) => void) => () => void |
||||
|
off: (command: string, cb: (event: IpcRendererEvent, ...args: any[]) => void) => void |
||||
|
offAll: (command: string) => void |
||||
|
popupMenu: (options: IPopupMenuOption) => void |
||||
|
} |
||||
|
|
||||
|
declare const electron: typeof import("@electron-toolkit/preload").electronAPI |
||||
|
declare const api: Api |
||||
|
|
||||
|
interface Window { |
||||
|
electron: typeof import("@electron-toolkit/preload").electronAPI |
||||
|
api: Api |
||||
|
} |
@ -1,4 +1,6 @@ |
|||||
interface IMenuItemOption extends Electron.MenuItemConstructorOptions { |
import type { PopupOptions } from "electron" |
||||
|
|
||||
|
export interface IMenuItemOption extends Electron.MenuItemConstructorOptions { |
||||
// 参见:https://www.electronjs.org/docs/api/menu-item
|
// 参见:https://www.electronjs.org/docs/api/menu-item
|
||||
_click_evt?: string |
_click_evt?: string |
||||
} |
} |
Loading…
Reference in new issue