refactor: 自动更新

pull/180/head
xiaojunnuo 2020-11-09 18:58:14 +08:00
parent 02ab38c299
commit 161411f9c6
13 changed files with 290 additions and 1163 deletions

View File

@ -57,7 +57,10 @@ const serverApi = {
if (msg.type === 'status') { if (msg.type === 'status') {
fireStatus(msg.event) fireStatus(msg.event)
} else if (msg.type === 'error') { } else if (msg.type === 'error') {
event.fire('error', { key: 'server', error: msg.event }) if (msg.event.code && msg.event.code === 'EADDRINUSE') {
fireStatus(false) // 启动失败
}
event.fire('error', { key: 'server', value: 'EADDRINUSE', error: msg.event })
} }
}) })
return { port: config.port } return { port: config.port }

View File

@ -1,12 +1,9 @@
const event = require('./event') const event = require('./event')
const lodash = require('lodash') const lodash = require('lodash')
const status = { const status = {
server: false, server: { enabled: false },
proxy: { proxy: {},
}, plugin: {}
plugin: {
}
} }
event.register('status', (event) => { event.register('status', (event) => {

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,7 @@
"@docmirror/mitmproxy": "1.0.1", "@docmirror/mitmproxy": "1.0.1",
"ant-design-vue": "^1.6.5", "ant-design-vue": "^1.6.5",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"electron-store": "^6.0.1",
"electron-updater": "^4.3.5", "electron-updater": "^4.3.5",
"es-abstract": "^1.17.7", "es-abstract": "^1.17.7",
"json5": "^2.1.3", "json5": "^2.1.3",

View File

@ -4,6 +4,10 @@ import path from 'path'
import { app, protocol, BrowserWindow, Menu, Tray } from 'electron' import { app, protocol, BrowserWindow, Menu, Tray } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import bridge from './bridge/index' import bridge from './bridge/index'
import updateHandle from './bridge/update-handle'
// eslint-disable-next-line no-unused-vars
const isMac = process.platform === 'darwin'
// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer' // import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production' const isDevelopment = process.env.NODE_ENV !== 'production'
@ -114,6 +118,8 @@ function quit (app) {
} }
} }
// -------------执行开始---------------
const isFirstInstance = app.requestSingleInstanceLock() const isFirstInstance = app.requestSingleInstanceLock()
if (!isFirstInstance) { if (!isFirstInstance) {
@ -161,6 +167,7 @@ if (!isFirstInstance) {
} }
createWindow() createWindow()
bridge.init(win) bridge.init(win)
updateHandle(win, 'http://localhost/dev-sidecar/')
try { try {
// 最小化到托盘 // 最小化到托盘
tray = setTray(app) tray = setTray(app)

View File

@ -12,7 +12,7 @@ const localApi = {
lodash.merge(core, local) lodash.merge(core, local)
const list = [] const list = []
_deepFindFunction(list, core, '') _deepFindFunction(list, core, '')
console.log('api list:', list) // console.log('api list:', list)
return list return list
}, },
startup () { startup () {

View File

@ -0,0 +1,74 @@
import { ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
// win是所有窗口的引用
// const path = require('path') // 引入path模块
// const fs = require('fs-extra')
// eslint-disable-next-line no-unused-vars
const isMac = process.platform === 'darwin'
// 检测更新在你想要检查更新的时候执行renderer事件触发后的操作自行编写
function updateHandle (win, updateUrl) {
// // 更新前,删除本地安装包 ↓
// const updaterCacheDirName = 'dev-sidecar-updater'
// const updatePendingPath = path.join(autoUpdater.app.baseCachePath, updaterCacheDirName, 'pending')
// fs.emptyDir(updatePendingPath)
// // 更新前,删除本地安装包 ↑
const message = {
error: 'update error',
checking: 'updating...',
updateAva: 'fetch new version and downloading...',
updateNotAva: 'do not to update'
}
// 本地开发环境改变app-update.yml地址
// if (process.env.NODE_ENV === 'development' && !isMac) {
// autoUpdater.updateConfigPath = path.join(__dirname, 'win-unpacked/resources/app-update.yml')
// }
// 设置服务器更新地址
autoUpdater.setFeedURL({
provider: 'generic',
url: updateUrl
})
autoUpdater.on('error', function (err) {
console.log('autoUpdater error', err)
sendUpdateMessage(message.error)
})
autoUpdater.on('checking-for-update', function () {
console.log('autoUpdater checking-for-update')
sendUpdateMessage(message.checking)
})
// 准备更新,打开进度条读取页面,关闭其他页面
autoUpdater.on('update-available', function (info) {
console.log('autoUpdater update-available')
sendUpdateMessage(message.updateAva)
})
autoUpdater.on('update-not-available', function (info) {
console.log('autoUpdater update-not-available')
sendUpdateMessage(message.updateNotAva)
})
// 更新下载进度
autoUpdater.on('download-progress', function (progressObj) {
console.log('autoUpdater download-progress')
win.webContents.send('download-progress', parseInt(progressObj.percent))
})
// 更新完成,重启应用
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
ipcMain.on('isUpdateNow', (e, arg) => {
// some code here to handle event
autoUpdater.quitAndInstall()
})
win.webContents.send('isUpdateNow')
})
ipcMain.on('checkForUpdate', () => {
// 执行自动更新检查
console.log('autoUpdater checkForUpdates')
autoUpdater.checkForUpdates()
})
// 通过main进程发送事件给renderer进程提示更新信息
function sendUpdateMessage (text) {
console.log('autoUpdater sendUpdateMessage')
win.webContents.send('message', text)
}
console.log('auto update inited')
}
export default updateHandle

View File

@ -26,7 +26,7 @@ apiInit().then((api) => {
render: h => h(App) render: h => h(App)
}).$mount('#app') }).$mount('#app')
view.register(app) view.init(app)
}) })
// fix vue-router NavigationDuplicated // fix vue-router NavigationDuplicated

View File

@ -30,6 +30,8 @@ export function apiInit () {
return apiObj return apiObj
}) })
} }
ipcRenderer.send('checkForUpdate')
return new Promise(resolve => { return new Promise(resolve => {
resolve(apiObj) resolve(apiObj)
}) })

View File

@ -13,7 +13,7 @@ function register (app) {
console.error('view on error', message) console.error('view on error', message)
const key = message.key const key = message.key
if (key === 'server') { if (key === 'server') {
handleServerStartError(message.error, app) handleServerStartError(message, message.error, app)
} }
}) })
api.on('error', (event, message) => { api.on('error', (event, message) => {
@ -21,8 +21,8 @@ function register (app) {
}) })
} }
function handleServerStartError (err, app) { function handleServerStartError (message, err, app) {
if (err.message && err.message.indexOf('listen EADDRINUSE') >= 0) { if (message.value === 'EADDRINUSE') {
app.$confirm({ app.$confirm({
title: '端口被占用,代理服务启动失败', title: '端口被占用,代理服务启动失败',
content: '是否要杀掉占用进程?', content: '是否要杀掉占用进程?',

View File

@ -1,5 +1,7 @@
import './status' import './status'
import register from './event' import register from './event'
export default { export default {
register init (app) {
register(app)
}
} }

View File

@ -128,6 +128,7 @@ export default {
btn.loading = true btn.loading = true
try { try {
const ret = await api(param) const ret = await api(param)
console.log('this status', this.status)
return ret return ret
} catch (err) { } catch (err) {
console.log('api invoke error:', err) console.log('api invoke error:', err)

View File

@ -1,5 +1,11 @@
const path = require('path') const path = require('path')
module.exports = { module.exports = {
pages: {
index: {
entry: 'src/main.js',
title: 'Dev-Sidecar-给开发者的边车辅助工具'
}
},
configureWebpack: config => { configureWebpack: config => {
const configNew = { const configNew = {
module: { module: {
@ -41,6 +47,10 @@ module.exports = {
allowElevation: true, allowElevation: true,
allowToChangeInstallationDirectory: true allowToChangeInstallationDirectory: true
}, },
publish: {
provider: 'generic',
url: ''
},
compression: 'maximum' compression: 'maximum'
}, },
chainWebpackMainProcess (config) { chainWebpackMainProcess (config) {