electron-vite-vue/packages/main/index.ts
2022-02-08 10:34:14 +08:00

71 lines
1.6 KiB
TypeScript

import os from 'os'
import path from 'path'
import { app, BrowserWindow } from 'electron'
// https://stackoverflow.com/questions/42524606/how-to-get-windows-version-using-node-js
const isWin7 = os.release().startsWith('6.1')
if (isWin7) app.disableHardwareAcceleration()
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
let win: BrowserWindow | null = null
async function createWindow() {
win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, '../preload/index.cjs'),
},
})
if (app.isPackaged) {
win.loadFile(path.join(__dirname, '../renderer/index.html'))
} else {
const pkg = await import('../../package.json')
const url = `http://${pkg.env.HOST || '127.0.0.1'}:${pkg.env.PORT}`
win.loadURL(url)
win.webContents.openDevTools()
}
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
win = null
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('second-instance', () => {
if (win) {
// someone tried to run a second instance, we should focus our window.
if (win.isMinimized()) win.restore()
win.focus()
}
})
app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
// @TODO
// auto update
/* if (app.isPackaged) {
app.whenReady()
.then(() => import('electron-updater'))
.then(({ autoUpdater }) => autoUpdater.checkForUpdatesAndNotify())
.catch((e) =>
// maybe you need to record some log files.
console.error('Failed check update:', e)
)
} */