| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- const fs = require('fs')
- 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 = false
- autoUpdater.setFeedURL({
- provider: data.provider,
- url: data.url
- })
- // 检查更新出错
- autoUpdater.on('error', (err) => {
- dialog.showMessageBox({
- type: 'error',
- title: '错误',
- noLink: true,
- message: '更新发生错误',
- detail: err.message
- })
- })
- // 检测到有版本更新
- autoUpdater.on('update-available', (e) => {
- dialog.showMessageBox({
- 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({
- type: 'info',
- title: '提示',
- noLink: true,
- message: e.version,
- detail: '已下载新版本,是否关闭应用更新?',
- buttons: ['取消', '安装']
- }).then((res) => {
- if (res.response === 1) {
- autoUpdater.quitAndInstall()
- } else {
- win.setProgressBar(-1)
- }
- })
- })
- autoUpdater.checkForUpdates()
- }
- }
- }
- const createWindow = () => {
- Menu.setApplicationMenu(null)
- const win = new BrowserWindow({
- center: true,
- minWidth: 1280,
- minHeight: 800,
- icon: 'dist/favicon.ico'
- })
- win.maximize()
- win.loadFile('dist/index.html')
- win.on('ready-to-show', () => updateHandle(win))
- }
- app.whenReady().then(() => {
- createWindow()
- })
|