const fs = require('fs') const path = require('path') const yaml = require('js-yaml') const { app, BrowserWindow, Menu, dialog } = require('electron') const { autoUpdater } = require('electron-updater') const updateHandle = (win) => { if (app.isPackaged) { const fileContents = fs.readFileSync('resources/app-update.yml', 'utf-8') const data = yaml.load(fileContents) if (data.url) { autoUpdater.autoDownload = true // 自动下载更新 autoUpdater.autoInstallOnAppQuit = false autoUpdater.setFeedURL({ provider: data.provider, url: data.url }) // 检查更新出错 // autoUpdater.on('error', (err) => { // dialog.showMessageBox(win, { // type: 'error', // title: '错误', // noLink: true, // message: '更新发生错误', // detail: err.message // }) // }) // 检测到有版本更新 // autoUpdater.on('update-available', (e) => { // dialog.showMessageBox(win, { // type: 'info', // title: '提示', // noLink: true, // message: e.version, // detail: '发现新版本,是否现在下载?', // buttons: ['下载', '取消'] // }).then((res) => { // if (res.response === 1) { // autoUpdater.downloadUpdate() // } // }) // }) // 更新下载进度事件 autoUpdater.on('download-progress', (progress) => { // https://www.electronjs.org/zh/docs/latest/tutorial/progress-bar win.setProgressBar(progress.percent / 100) }) // 下载完成,询问用户是否更新 autoUpdater.on('update-downloaded', (e) => { dialog.showMessageBox(win, { type: 'info', title: '提示', noLink: true, message: e.version, detail: '已下载新版本,是否立即安装?', buttons: ['安装', '退出'] }).then((res) => { win.setProgressBar(-1) if (res.response === 0) { autoUpdater.quitAndInstall() } else { app.quit() } }) }) autoUpdater.checkForUpdates() } } } const createWindow = () => { Menu.setApplicationMenu(null) const win = new BrowserWindow({ show: false, center: true, width: 1280, height: 800, minWidth: 480, minHeight: 300, icon: path.resolve(__dirname, 'dist/favicon.ico') }) win.loadFile(path.resolve(__dirname, 'dist/index.html')) win.maximize() win.once('ready-to-show', () => { win.show() updateHandle(win) }) } app.whenReady().then(() => { createWindow() }) // 阻止应用多开 // const gotTheLock = app.requestSingleInstanceLock() // if (!gotTheLock) { // app.quit() // } else { // app.whenReady().then(() => { // createWindow() // }) // }