add executable file basic check

This commit is contained in:
geekgeekrun
2026-02-08 00:11:07 +08:00
parent 3f2b709097
commit 8cc94baa18
2 changed files with 33 additions and 4 deletions
@@ -2,6 +2,7 @@ import { BrowserWindow, dialog, ipcMain, shell } from 'electron'
import gtag from './gtag' import gtag from './gtag'
import buildInfo from '../../common/build-info.json' import buildInfo from '../../common/build-info.json'
import os from 'node:os' import os from 'node:os'
import fs from 'node:fs'
import { import {
ensureStorageFileExist, ensureStorageFileExist,
readStorageFile, readStorageFile,
@@ -85,4 +86,28 @@ export default function initPublicIpc() {
return dialog.showOpenDialog(win, fileChooserConfig) return dialog.showOpenDialog(win, fileChooserConfig)
} }
}) })
ipcMain.handle('check-executable-file', (ev, filePath: string) => {
if (!filePath?.trim()) {
return {
message: '文件名无效'
}
}
if (!fs.existsSync(filePath)) {
return {
message: '文件不存在'
}
}
if (!fs.statSync(filePath).isFile()) {
const messageSeg = ['文件不是可执行文件']
if (os.platform() === 'darwin') {
messageSeg.push(
'macOS 平台,可执行文件位于“App包.app/Contents/MacOS/ 文件夹下”,而不是“App包.app”文件夹整体'
)
}
return {
message: messageSeg.join('')
}
}
return null
})
} }
@@ -124,15 +124,19 @@ const formData = ref({
const rules = { const rules = {
browserPath: { browserPath: {
validator: (_, value, callback) => { validator: async (_, value, callback) => {
if (!value?.trim()) { if (!value?.trim()) {
callback(new Error('请输入浏览器可执行文件路径')) callback(new Error('请输入浏览器可执行文件路径'))
return
} }
// TODO: 检查文件是否存在 const err = await ipcRenderer.invoke('check-executable-file', value)
else { if (err) {
callback(err?.message ?? '文件无效 - 未知原因')
} else {
callback() callback()
} }
} },
trigger: 'blur'
} }
} }