-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
174 lines (157 loc) · 5.13 KB
/
main.js
File metadata and controls
174 lines (157 loc) · 5.13 KB
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
const { app, BrowserWindow, dialog, shell, nativeImage } = require('electron');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const https = require('https');
const { autoUpdater } = require('electron-updater');
const log = require('electron-log');
const { ipcMain } = require('electron');
const AdmZip = require('adm-zip');
const packageJson = require(path.join(__dirname, 'package.json'));
const { spawn } = require('child_process');
const { microbotDir, openLocation } = require(path.join(
__dirname,
'libs',
'dir-module.js'
));
process.on('uncaughtException', (error) => {
log.error('Uncaught Exception:', error);
});
let mainWindow = null;
// Ensure the .microbot directory exists
if (!fs.existsSync(microbotDir)) {
fs.mkdirSync(microbotDir);
}
async function loadLibraries() {
log.info('Loading libraries...');
const ipcHandlersPath = path.join(__dirname, 'libs', 'ipc-handlers.js');
try {
log.info('Requiring ipc-handlers...');
const handler = require(ipcHandlersPath);
const deps = {
AdmZip: AdmZip,
axios: axios,
ipcMain: ipcMain,
microbotDir: microbotDir,
packageJson: packageJson,
path: path,
log: log,
spawn: spawn,
dialog: dialog,
shell: shell,
projectDir: __dirname,
fs: fs,
app: app,
mainWindow: mainWindow
};
try {
const mockAuthHandler = require(path.join(
__dirname,
'libs',
'mock-auth.js'
));
if (typeof mockAuthHandler === 'function') {
mockAuthHandler({ ipcMain, log });
} else {
log.error('mock-auth does not export a function');
}
} catch (authError) {
log.error('Error requiring mock-auth:', authError);
}
if (typeof handler === 'function') {
await handler(deps);
} else {
log.error('ipcHandlers does not export a function');
}
log.info('Done requiring ipcHandlers...');
} catch (error) {
log.error('Error requiring ipcHandlers:', error);
}
}
async function createWindow() {
// Create the main window, but don't show it yet
mainWindow = new BrowserWindow({
width: 1280,
height: process.env.DEBUG !== 'true' ? 750 : 800,
show: false, // Don't show the main window immediately
title: 'Microbot Launcher',
autoHideMenuBar: process.env.DEBUG !== 'true',
icon: path.join(__dirname, 'images/microbot_transparent.ico'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: true,
webviewTag: true
},
titleBarStyle: process.env.DEBUG !== 'true' ? 'hidden' : '',
frame: process.env.DEBUG === 'true'
});
if (process.platform === 'darwin') {
mainWindow.setWindowButtonVisibility(false);
}
try {
const extraHandlers = require(path.join(
__dirname,
'libs',
'extra-ipc-handlers.js'
));
if (typeof extraHandlers === 'function') {
await extraHandlers(app, ipcMain, mainWindow, log, openLocation);
} else {
log.error('extra-ipc-handlers does not export a function');
}
} catch (e) {
log.error('Failed to load extra-ipc-handlers:', e);
}
await mainWindow.loadFile(path.join(__dirname, 'index.html'));
}
autoUpdater.autoDownload = false;
autoUpdater.disableWebInstaller = true;
autoUpdater.on('update-available', (info) => {
dialog
.showMessageBox({
type: 'info',
title: 'Update available',
detail: `Would you like to download version ${info.version} and install it now?`,
message: `New version of launcher available!`,
buttons: ['Install', 'Later'],
cancelId: 1,
defaultId: 0,
noLink: false
})
.then((result) => {
if (result.response === 0) {
dialog.showMessageBox({
type: 'info',
title: 'Downloading',
message: `Downloading version ${info.version} of the launcher...`
});
autoUpdater.downloadUpdate();
}
});
});
autoUpdater.on('update-downloaded', () => {
dialog
.showMessageBox({
title: 'Install Updates',
message:
'Updates downloaded. The application will now quit and install the updates.'
})
.then(() => {
autoUpdater.quitAndInstall();
});
});
app.whenReady().then(async () => {
log.info('App starting...');
await loadLibraries();
await createWindow();
if (process.env.DEBUG !== 'true') {
mainWindow.show();
autoUpdater.checkForUpdates();
} else {
mainWindow.show();
}
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});