mirror of
https://github.com/pompurin404/mihomo-party.git
synced 2024-11-16 03:32:17 +08:00
test autorun
This commit is contained in:
parent
6bee06c4af
commit
056f07de00
|
@ -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}
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
0
script/prepare.ts
Normal file
0
script/prepare.ts
Normal file
91
src/main/autoRun.ts
Normal file
91
src/main/autoRun.ts
Normal file
|
@ -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 = `
|
||||
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
||||
<RegistrationInfo>
|
||||
<Date>${new Date().toISOString()}</Date>
|
||||
<Author>${process.env.USERNAME}</Author>
|
||||
</RegistrationInfo>
|
||||
<Triggers>
|
||||
<LogonTrigger>
|
||||
<Enabled>true</Enabled>
|
||||
</LogonTrigger>
|
||||
</Triggers>
|
||||
<Principals>
|
||||
<Principal id="Author">
|
||||
<LogonType>InteractiveToken</LogonType>
|
||||
<RunLevel>HighestAvailable</RunLevel>
|
||||
</Principal>
|
||||
</Principals>
|
||||
<Settings>
|
||||
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
||||
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
||||
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
||||
<AllowHardTerminate>false</AllowHardTerminate>
|
||||
<StartWhenAvailable>true</StartWhenAvailable>
|
||||
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
||||
<IdleSettings>
|
||||
<StopOnIdleEnd>false</StopOnIdleEnd>
|
||||
<RestartOnIdle>false</RestartOnIdle>
|
||||
</IdleSettings>
|
||||
<AllowStartOnDemand>true</AllowStartOnDemand>
|
||||
<Enabled>true</Enabled>
|
||||
<Hidden>false</Hidden>
|
||||
<RunOnlyIfIdle>false</RunOnlyIfIdle>
|
||||
<WakeToRun>false</WakeToRun>
|
||||
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
||||
<Priority>7</Priority>
|
||||
</Settings>
|
||||
<Actions Context="Author">
|
||||
<Exec>
|
||||
<Command>${exePath}</Command>
|
||||
</Exec>
|
||||
</Actions>
|
||||
</Task>
|
||||
`
|
||||
|
||||
export async function checkAutoRun(): Promise<boolean> {
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
10
src/main/cmds.ts
Normal file
10
src/main/cmds.ts
Normal file
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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<AxiosInstance> =
|
|||
return axiosIns
|
||||
}
|
||||
|
||||
async function mihomoVersion(): Promise<IMihomoVersion> {
|
||||
export async function mihomoVersion(): Promise<IMihomoVersion> {
|
||||
const instance = await getAxios()
|
||||
return instance.get('/version') as Promise<IMihomoVersion>
|
||||
}
|
||||
|
||||
export function registerIpcMainHandlers(): void {
|
||||
ipcMain.handle('mihomoVersion', mihomoVersion)
|
||||
}
|
||||
|
|
|
@ -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(
|
|||
<React.StrictMode>
|
||||
<NextUIProvider>
|
||||
<NextThemesProvider attribute="class" defaultTheme="dark">
|
||||
<BrowserRouter>
|
||||
<HashRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</HashRouter>
|
||||
</NextThemesProvider>
|
||||
</NextUIProvider>
|
||||
</React.StrictMode>
|
||||
|
|
|
@ -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 <div>failed to load</div>
|
||||
if (isLoading) return <div>loading...</div>
|
||||
return (
|
||||
<div>
|
||||
{data?.version}
|
||||
<Button onPress={() => mutate()}>mutate</Button>
|
||||
{`${data}`}
|
||||
<Button
|
||||
onPress={async () => {
|
||||
await enableAutoRun()
|
||||
await mutate()
|
||||
}}
|
||||
>
|
||||
enable
|
||||
</Button>
|
||||
<Button
|
||||
onPress={async () => {
|
||||
await disableAutoRun()
|
||||
await mutate()
|
||||
}}
|
||||
>
|
||||
disable
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
export async function mihomoVersion(): Promise<IMihomoVersion> {
|
||||
return await window.electron.ipcRenderer.invoke('mihomoVersion')
|
||||
}
|
||||
|
||||
export async function checkAutoRun(): Promise<boolean> {
|
||||
return await window.electron.ipcRenderer.invoke('checkAutoRun')
|
||||
}
|
||||
|
||||
export async function enableAutoRun(): Promise<void> {
|
||||
await window.electron.ipcRenderer.invoke('enableAutoRun')
|
||||
}
|
||||
|
||||
export async function disableAutoRun(): Promise<void> {
|
||||
await window.electron.ipcRenderer.invoke('disableAutoRun')
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user