From 056f07de00df6d4dd48942bf760690372f519065 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Wed, 31 Jul 2024 12:38:32 +0800 Subject: [PATCH] test autorun --- electron-builder.yml | 4 ++ package.json | 3 +- pnpm-lock.yaml | 3 + script/prepare.ts | 0 src/main/autoRun.ts | 91 +++++++++++++++++++++++++++++ src/main/cmds.ts | 10 ++++ src/main/index.ts | 2 +- src/main/mihomo-api.ts | 7 +-- src/renderer/src/main.tsx | 6 +- src/renderer/src/pages/settings.tsx | 26 +++++++-- src/renderer/src/utils/api.ts | 12 ++++ 11 files changed, 149 insertions(+), 15 deletions(-) create mode 100644 script/prepare.ts create mode 100644 src/main/autoRun.ts create mode 100644 src/main/cmds.ts diff --git a/electron-builder.yml b/electron-builder.yml index aabe7a4..1ecf921 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -11,9 +11,13 @@ files: - '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}' asarUnpack: - resources/** +extraResources: + - from: './resources/' + to: '' win: target: - nsis + requestedExecutionLevel: requireAdministrator executableName: mihomo-party nsis: artifactName: ${name}-windows-${version}-${arch}-setup.${ext} diff --git a/package.json b/package.json index 5b608bc..a26f3ce 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "next-themes": "^0.3.0", "react-icons": "^5.2.1", "react-router-dom": "^6.25.1", - "swr": "^2.2.5" + "swr": "^2.2.5", + "yaml": "^2.5.0" }, "devDependencies": { "@electron-toolkit/eslint-config-prettier": "^2.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7497521..e0cc9fc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: swr: specifier: ^2.2.5 version: 2.2.5(react@18.3.1) + yaml: + specifier: ^2.5.0 + version: 2.5.0 devDependencies: '@electron-toolkit/eslint-config-prettier': specifier: ^2.0.0 diff --git a/script/prepare.ts b/script/prepare.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/main/autoRun.ts b/src/main/autoRun.ts new file mode 100644 index 0000000..a5108bb --- /dev/null +++ b/src/main/autoRun.ts @@ -0,0 +1,91 @@ +import { exec } from 'child_process' +import { app } from 'electron' +import fs from 'fs' + +// 获取应用的可执行文件路径 +const exePath = app.getPath('exe') + +const taskName = 'mihomo-party' + +const taskXml = ` + + + ${new Date().toISOString()} + ${process.env.USERNAME} + + + + true + + + + + InteractiveToken + HighestAvailable + + + + IgnoreNew + false + false + false + true + false + + false + false + + true + true + false + false + false + PT0S + 7 + + + + ${exePath} + + + + ` + +export async function checkAutoRun(): Promise { + if (process.platform === 'win32') { + const { stdout } = (await new Promise((resolve) => { + exec(`schtasks /query /tn "${taskName}"`, (_err, stdout, stderr) => { + resolve({ stdout, stderr }) + }) + })) as { stdout: string; stderr: string } + return stdout.includes(taskName) + } else { + return app.getLoginItemSettings().openAtLogin + } +} + +export function enableAutoRun(): void { + if (process.platform === 'win32') { + const taskFilePath = `${app.getPath('userData')}\\${taskName}.xml` + fs.writeFileSync(taskFilePath, taskXml) + exec(`schtasks /create /tn "${taskName}" /xml "${taskFilePath}" /f`) + } else { + app.setLoginItemSettings({ + openAtLogin: true, + path: exePath + }) + } +} + +export function disableAutoRun(): void { + if (process.platform === 'win32') { + exec(`schtasks /delete /tn "${taskName}" /f`) + app.setLoginItemSettings({ + openAtLogin: false + }) + } else { + app.setLoginItemSettings({ + openAtLogin: false + }) + } +} diff --git a/src/main/cmds.ts b/src/main/cmds.ts new file mode 100644 index 0000000..676be57 --- /dev/null +++ b/src/main/cmds.ts @@ -0,0 +1,10 @@ +import { ipcMain } from 'electron' +import { mihomoVersion } from './mihomo-api' +import { checkAutoRun, disableAutoRun, enableAutoRun } from './autoRun' + +export function registerIpcMainHandlers(): void { + ipcMain.handle('mihomoVersion', mihomoVersion) + ipcMain.handle('checkAutoRun', checkAutoRun) + ipcMain.handle('enableAutoRun', enableAutoRun) + ipcMain.handle('disableAutoRun', disableAutoRun) +} diff --git a/src/main/index.ts b/src/main/index.ts index f15decc..a4ecc47 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,7 +3,7 @@ import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import pngIcon from '../../resources/icon.png?asset' import icoIcon from '../../resources/icon.ico?asset' -import { registerIpcMainHandlers } from './mihomo-api' +import { registerIpcMainHandlers } from './cmds' let window: BrowserWindow | null = null let tray: Tray | null = null diff --git a/src/main/mihomo-api.ts b/src/main/mihomo-api.ts index e4b5b2b..e887a5e 100644 --- a/src/main/mihomo-api.ts +++ b/src/main/mihomo-api.ts @@ -1,4 +1,3 @@ -import { ipcMain } from 'electron' import axios, { AxiosInstance } from 'axios' let axiosIns: AxiosInstance = null! @@ -21,11 +20,7 @@ export const getAxios = async (force: boolean = false): Promise = return axiosIns } -async function mihomoVersion(): Promise { +export async function mihomoVersion(): Promise { const instance = await getAxios() return instance.get('/version') as Promise } - -export function registerIpcMainHandlers(): void { - ipcMain.handle('mihomoVersion', mihomoVersion) -} diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index d3d4a7e..6b74a39 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -1,6 +1,6 @@ import React from 'react' import ReactDOM from 'react-dom/client' -import { BrowserRouter } from 'react-router-dom' +import { HashRouter } from 'react-router-dom' import { ThemeProvider as NextThemesProvider } from 'next-themes' import { NextUIProvider } from '@nextui-org/react' import '@renderer/utils/init' @@ -11,9 +11,9 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - + - + diff --git a/src/renderer/src/pages/settings.tsx b/src/renderer/src/pages/settings.tsx index 7d0ccfa..3f40133 100644 --- a/src/renderer/src/pages/settings.tsx +++ b/src/renderer/src/pages/settings.tsx @@ -1,17 +1,35 @@ import { Button } from '@nextui-org/react' -import { mihomoVersion } from '@renderer/utils/api' +import { checkAutoRun, enableAutoRun, disableAutoRun } from '@renderer/utils/api' import useSWR from 'swr' export default function Settings(): JSX.Element { - const { data, error, isLoading, mutate } = useSWR('mihomoVersion', mihomoVersion) + const { data, error, isLoading, mutate } = useSWR('checkAutoRun', checkAutoRun, { + errorRetryCount: 5, + errorRetryInterval: 200 + }) if (error) return
failed to load
if (isLoading) return
loading...
return (
- {data?.version} - + {`${data}`} + +
) } diff --git a/src/renderer/src/utils/api.ts b/src/renderer/src/utils/api.ts index aca2ede..d86a63b 100644 --- a/src/renderer/src/utils/api.ts +++ b/src/renderer/src/utils/api.ts @@ -1,3 +1,15 @@ export async function mihomoVersion(): Promise { return await window.electron.ipcRenderer.invoke('mihomoVersion') } + +export async function checkAutoRun(): Promise { + return await window.electron.ipcRenderer.invoke('checkAutoRun') +} + +export async function enableAutoRun(): Promise { + await window.electron.ipcRenderer.invoke('enableAutoRun') +} + +export async function disableAutoRun(): Promise { + await window.electron.ipcRenderer.invoke('disableAutoRun') +}