-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
238 lines (201 loc) · 8.54 KB
/
index.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type { CapacitorElectronConfig } from '@capacitor-community/electron';
import { getCapacitorElectronConfig, setupElectronDeepLinking } from '@capacitor-community/electron';
import type { MenuItemConstructorOptions } from 'electron';
import { app, MenuItem, ipcMain, dialog } from 'electron';
import electronIsDev from 'electron-is-dev';
import unhandled from 'electron-unhandled';
import { autoUpdater } from 'electron-updater';
import fs from 'fs-extra';
import { ElectronCapacitorApp, setupContentSecurityPolicy, setupReloadWatcher } from './setup';
import { captureException } from '@sentry/node';
const gotTheLock = app.requestSingleInstanceLock();
// Graceful handling of unhandled errors.
unhandled({
logger: (e) => {
console.error(e);
captureException(e);
console.log("there is an error occurs")
},
showDialog: false,
reportButton: (error) => {
console.log('Report Button Initialized');
captureException(error);
}
});
let isQuiting = false;
let mainWindow = null;
let isDownloaded = false;
// Define our menu templates (these are optional)
const trayMenuTemplate: (MenuItemConstructorOptions | MenuItem)[] = [
new MenuItem({
label: 'Open', click: function () {
myCapacitorApp.getMainWindow().show();
}
}),
new MenuItem({
label: 'Quit App', click: function () {
isQuiting = true;
// myCapacitorApp.getMainWindow().close();
app.quit();
}
})
];
const appMenuBarMenuTemplate: (MenuItemConstructorOptions | MenuItem)[] = [
{ role: process.platform === 'darwin' ? 'appMenu' : 'fileMenu' },
{ role: 'viewMenu' },
];
// Get Config options from capacitor.config
const capacitorFileConfig: CapacitorElectronConfig = getCapacitorElectronConfig();
// Initialize our app. You can pass menu templates into the app here.
// const myCapacitorApp = new ElectronCapacitorApp(capacitorFileConfig);
const myCapacitorApp = new ElectronCapacitorApp(capacitorFileConfig, trayMenuTemplate, appMenuBarMenuTemplate);
// If deeplinking is enabled then we will set it up here.
if (capacitorFileConfig.electron?.deepLinkingEnabled) {
setupElectronDeepLinking(myCapacitorApp, {
customProtocol: capacitorFileConfig.electron.deepLinkingCustomProtocol ?? 'mycapacitorapp',
});
}
// If we are in Dev mode, use the file watcher components.
if (electronIsDev) {
setupReloadWatcher(myCapacitorApp);
}
// Run Application
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
} else {
mainWindow.show();
}
mainWindow.focus();
if (isQuiting) {
// mainWindow.close();
app.quit();
}
}
});
// Wait for electron app to be ready.
app.whenReady().then(async () => {
mainWindow = await myCapacitorApp.init();
})
/*
app.on('ready', () => {
updateApp = require('update-electron-app');
updateApp({
updateInterval: '5 minute',
notifyUser: true
});
});
*/
autoUpdater.autoDownload = true;
setInterval(() => {
autoUpdater.checkForUpdates()
}, 3600000)
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart / Reinicie. / Перезапуск', 'Later / Después / Позже'],
title: 'Giga Meter Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: `A new version of UNICEF's Giga Meter has been downloaded. Restart the application to apply the updates.\n\nUna nueva version de la aplicación Giga Meter de UNICEF ha sido descargada. Reinicie la aplicación para aplicar los cambios.\n\nНовая версия приложения Giga Meter загружена . Перезапустите приложение, чтобы применить обновления.`
};
/*
if (isDownloaded === false) {
dialog.showMessageBox(dialogOpts).then((returnValue) => {
isDownloaded = true;
if (returnValue.response === 0) autoUpdater.quitAndInstall(true, true)
})
}
*/
if (!isDownloaded) {
isDownloaded = true;
try {
// autoUpdater.quitAndInstall(true, true)
//for auto update comment the below codes, and uncomment the above line of code
const dialogOpts = {
type: 'info',
buttons: ['Restart / Reinicie. / Перезапуск', 'Later / Después / Позже'],
title: 'Giga Meter Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: `A new version of UNICEF's Giga Meter has been downloaded. Restart the application to apply the updates.\n\nUna nueva version de la aplicación Giga Meter de UNICEF ha sido descargada. Reinicie la aplicación para aplicar los cambios.\n\nНовая версия приложения Giga Meter загружена . Перезапустите приложение, чтобы применить обновления.`
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall(false, true)
})
//throw new Error("opps there is unexpected error")
} catch (error) {
console.error('Error during update installation:', error);
captureException(error);
const dialogOpts = {
type: 'info',
buttons: ['Restart / Reinicie. / Перезапуск', 'Later / Después / Позже'],
title: 'Giga Meter Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: `A new version of UNICEF's Giga Meter has been downloaded. Restart the application to apply the updates.\n\nUna nueva version de la aplicación Giga Meter de UNICEF ha sido descargada. Reinicie la aplicación para aplicar los cambios.\n\nНовая версия приложения Giga Meter загружена . Перезапустите приложение, чтобы применить обновления.`
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall(false, true)
})
}
}
});
autoUpdater.on('error', (error) => {
console.error('Update Error:', error);
captureException(error);
});
/*
autoUpdater.on('error', (error) => {
console.error('Update Error:', error);
const dialogOpts = {
type: 'info',
buttons: ['Restart / Reinicie / Перезапустить', 'Later / Después / Позже'],
title: 'PCDC Update',
message: `A new version of PCDC has been downloaded. Restart the application to apply the updates.\n\nUna nueva version de PCDC ha sido descargada. Reinicie la aplicación para aplicar los cambios.\n\nБыла загружена новая версия PCDC. Перезапустите приложение, чтобы применить обновления.`
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall(false, true)
})
});
*/
// Security - Set Content-Security-Policy based on whether or not we are in dev mode.
// setupContentSecurityPolicy(myCapacitorApp.getCustomURLScheme());
// Initialize our app, build windows, and load content.
// await myCapacitorApp.init();
// Check for updates if we are in a packaged app.
// autoUpdater.checkForUpdatesAndNotify();
}
if (mainWindow) {
mainWindow.on('close', (event) => {
if (!isQuiting) {
event.preventDefault();
mainWindow.hide();
return false;
} else {
app.quit();
}
});
}
// Handle when all of our windows are close (platforms have their own expectations).
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
// When the dock icon is clicked.
app.on('activate', async function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (myCapacitorApp.getMainWindow().isDestroyed()) {
await myCapacitorApp.init();
}
});
// Place all ipc or other electron api calls and custom functionality under this line
ipcMain.addListener('closeFromUi', (ev) => {
myCapacitorApp.getMainWindow().hide();
});