forked from iffy/electron-updater-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
202 lines (179 loc) · 5.83 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// This is free and unencumbered software released into the public domain.
// See LICENSE for details
const {app, BrowserWindow, Menu, protocol, ipcMain} = require('electron');
const log = require('electron-log');
const {autoUpdater} = require("electron-updater");
//-------------------------------------------------------------------
// Logging
//
// THIS SECTION IS NOT REQUIRED
//
// This logging setup is not required for auto-updates to work,
// but it sure makes debugging easier :)
//-------------------------------------------------------------------
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
//-------------------------------------------------------------------
// Define the menu
//
// THIS SECTION IS NOT REQUIRED
//-------------------------------------------------------------------
let template = []
if (process.platform === 'darwin') {
// OS X
const name = app.getName();
template.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click() { app.quit(); }
},
]
})
}
//-------------------------------------------------------------------
// Open a window that displays the version
//
// THIS SECTION IS NOT REQUIRED
//
// This isn't required for auto-updates to work, but it's easier
// for the app to show a window than to have to click "About" to see
// that updates are working.
//-------------------------------------------------------------------
let mainWindow;
function sendStatusToWindow(text) {
log.info(text);
mainWindow.webContents.send('message', text);
}
function createDefaultWindow() {
mainWindow = new BrowserWindow({
//解决electron5.x 及以上版本中默认没法在electron渲染进程引入nodejs模块
webPreferences: {
nodeIntegration: true
}
});
mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.loadURL(`file://${__dirname}/version.html#v${app.getVersion()}`);
return mainWindow;
}
app.on('ready', function() {
// Create the Menu
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
createDefaultWindow();
//尝试更新
updateHandle();
});
app.on('window-all-closed', () => {
app.quit();
});
//
// CHOOSE one of the following options for Auto updates
//
//-------------------------------------------------------------------
// Auto updates - Option 1 - Simplest version
//
// This will immediately download an update, then install when the
// app quits.
//-------------------------------------------------------------------
app.on('ready', function() {
autoUpdater.checkForUpdatesAndNotify();
});
//-------------------------------------------------------------------
// Auto updates - Option 2 - More control
//
// For details about these events, see the Wiki:
// https://github.com/electron-userland/electron-builder/wiki/Auto-Update#events
//
// The app doesn't need to listen to any events except `update-downloaded`
//
// Uncomment any of the below events to listen for them. Also,
// look in the previous section to see them being used.
//-------------------------------------------------------------------
// app.on('ready', function() {
// autoUpdater.checkForUpdates();
// });
// autoUpdater.on('checking-for-update', () => {
// })
// autoUpdater.on('update-available', (info) => {
// })
// autoUpdater.on('update-not-available', (info) => {
// })
// autoUpdater.on('error', (err) => {
// })
// autoUpdater.on('download-progress', (progressObj) => {
// })
// autoUpdater.on('update-downloaded', (info) => {
// autoUpdater.quitAndInstall();
// })
//检测更新
let uploadUrl = "http://192.168.1.29:8080/download/history2/";
function updateHandle()
{
let message = {
error: '检查更新出错',
checking: '正在检查更新……',
updateAva: '检测到新版本,正在下载……',
updateNotAva: '现在使用的就是最新版本,不用更新',
};
//避免自动下载强制更新
autoUpdater.autoDownload = false;
autoUpdater.setFeedURL(uploadUrl);
autoUpdater.on('error', function (error) {
sendUpdateMessage(message.error)
});
autoUpdater.on('checking-for-update', function () {
console.log("checking-for-update");
sendUpdateMessage(message.checking)
});
autoUpdater.on('update-available', function (info) {
console.log("update-available");
sendUpdateMessage(message.updateAva)
//检测到新版本后监听渲染进程传来是否开始下载消息
ipcMain.on("startDownload", ()=>{
//手动下载更新
autoUpdater.downloadUpdate().then(res => {
sendUpdateMessage("下载完成");
});
})
mainWindow.webContents.send("startDownload", info);
});
autoUpdater.on('update-not-available', function (info) {
console.log("update-not-available");
sendUpdateMessage(message.updateNotAva)
});
// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
console.log("download-progress");
mainWindow.webContents.send('downloadProgress', progressObj)
})
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
ipcMain.on('isUpdateNow', (e, arg) =>{
console.log(arguments);
console.log("开始更新isUpdateNow");
//some code here to handle event
autoUpdater.quitAndInstall();
});
console.log("update-downloaded");
mainWindow.webContents.send('isUpdateNow')
});
ipcMain.on("checkForUpdate",()=>{
console.log("checkForUpdate");
//执行自动更新检查
autoUpdater.checkForUpdates();
})
}
// 通过main进程发送事件给renderer进程,提示更新信息
function sendUpdateMessage(text) {
mainWindow.webContents.send('message', text)
}