-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
315 lines (277 loc) · 8.68 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Built-in modules
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const util = require('util');
// Electron modules
const { app, BrowserWindow, ipcMain } = require('electron');
// Extra modules
const getPort = require('get-port');
const isDevMode = require('electron-is-dev');
const { get } = require('axios');
// Set path to error log
const errorLogPath = path.join(
process.env.APPDATA,
'MKVToolNix Batch Tool',
'error.log'
);
// Prepare file for writing logs
const errorLogFile = fs.createWriteStream(errorLogPath, { flags: 'a' });
// Update console.error so stdout is saved to log file
console.error = (...args) => {
const now = new Date();
const message = util.format(...args);
// Ensure message configuration is same as Flask's
const timestamp = now.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
}).replace(',', '');
const formattedMessage = `${timestamp} - MKVToolNix Batch Tool - ERROR - ${message}\n`;
// Write stdout to error.log file
errorLogFile.write(formattedMessage);
process.stdout.write(formattedMessage);
};
/**
* @description - Shuts down Electron & Flask.
* @param {number} port - Port that Flask server is running on.
*/
const shutdown = (port) => {
get(`http://localhost:${port}/quit`).then(app.quit).catch(app.quit);
};
/**
* @namespace BrowserWindow
* @description - Electron browser windows.
* @tutorial - https://www.electronjs.org/docs/api/browser-window
*/
const browserWindows = {};
/**
* @description - Creates main window.
* @param {number} port - Port that Flask server is running on.
*
* @memberof BrowserWindow
*/
const createMainWindow = (port) => {
const { loadingWindow, mainWindow } = browserWindows;
/**
* @description - Function to use custom JavaSCript in the DOM.
* @param {string} command - JavaScript to execute in DOM.
* @param {function} callback - Callback to execute here once complete.
* @returns {promise}
*/
const executeOnWindow = (command, callback) => {
return mainWindow.webContents
.executeJavaScript(command)
.then(callback)
.catch(console.error);
};
/**
* If in developer mode, show a loading window while
* the app and developer server compile.
*/
if (isDevMode) {
mainWindow.loadURL('http://localhost:3000');
mainWindow.hide();
/**
* Opening devTools, must be done before dom-ready
* to avoid occasional error from the webContents
* object being destroyed.
*/
mainWindow.webContents.openDevTools({ mode: 'undocked' });
/**
* Hide loading window and show main window
* once the main window is ready.
*/
mainWindow.webContents.on('did-finish-load', () => {
/**
* Checks page for errors that may have occurred
* during the hot-loading process.
*/
const isPageLoaded = `
var isBodyFull = document.body.innerHTML !== "";
var isHeadFull = document.head.innerHTML !== "";
var isLoadSuccess = isBodyFull && isHeadFull;
isLoadSuccess || Boolean(location.reload());
`;
/**
* @description Updates windows if page is loaded
* @param {*} isLoaded
*/
const handleLoad = (isLoaded) => {
if (isLoaded) {
/**
* Keep show() & hide() in this order to prevent
* unresponsive behavior during page load.
*/
mainWindow.show();
loadingWindow.destroy();
}
};
/**
* Checks if the page has been populated with
* React project. if so, shows the main page.
*/
executeOnWindow(isPageLoaded, handleLoad);
});
} else mainWindow.loadFile(path.join(__dirname, 'build/index.html'));
/**
* If using in production, the built version of the
* React project will be used instead of localhost.
*/
/**
* @description - Controls the opacity of title bar on focus/blur.
* @param {number} value - Opacity to set for title bar.
*/
const setTitleOpacity = (value) => `
if(document.readyState === 'complete') {
const titleBar = document.getElementById('electron-window-title-text');
const titleButtons = document.getElementById('electron-window-title-buttons');
if(titleBar) titleBar.style.opacity = ${value};
if(titleButtons) titleButtons.style.opacity = ${value};
}
`;
mainWindow.on('focus', () => executeOnWindow(setTitleOpacity(1)));
mainWindow.on('blur', () => executeOnWindow(setTitleOpacity(0.5)));
/**
* Listen and respond to ipcRenderer events on the frontend.
* @see `src\utils\services.js`
*/
ipcMain.on('app-maximize', () => mainWindow.maximize());
ipcMain.on('app-minimize', () => mainWindow.minimize());
ipcMain.on('app-quit', () => shutdown(port));
ipcMain.on('app-unmaximize', () => mainWindow.unmaximize());
ipcMain.on('get-port-number', (event) => {
event.returnValue = port;
});
ipcMain.on('app-restart', (_event, options) => {
// Determine if debug or regular app is used
const appPath = options.detached
? 'app.debug/app.debug.exe'
: 'app/app.exe';
// Determines if .py or .exe is used
const script = isDevMode
? 'python app.py'
: `start ./resources/${appPath}`;
// Quit Flask, then restart in desired mode
get(`http://localhost:${port}/quit`)
.then(() => {
spawn(`${script} ${port}`, options);
if (options.detached) {
mainWindow.webContents.openDevTools({ mode: 'undocked' });
} else {
mainWindow.webContents.closeDevTools();
}
})
.catch(console.error);
});
};
/**
* @description - Creates loading window to show while build is created.
* @memberof BrowserWindow
*/
const createLoadingWindow = () => {
return new Promise((resolve, reject) => {
const { loadingWindow } = browserWindows;
const loaderConfig = {
react: 'utilities/loaders/react/index.html',
redux: 'utilities/loaders/redux/index.html'
};
try {
loadingWindow.loadFile(path.join(__dirname, loaderConfig.react));
loadingWindow.webContents.on('did-finish-load', () => {
resolve(loadingWindow.show());
});
} catch (error) {
reject(console.error(error));
}
});
};
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.whenReady().then(async () => {
/**
* Method to set port in range of 3001-3999,
* based on availability.
*/
const port = await getPort({
port: getPort.makeRange(3001, 3999)
});
/**
* Assigns the main browser window on the
* browserWindows object.
*/
browserWindows.mainWindow = new BrowserWindow({
frame: false,
height: 370,
maxHeight: 370,
minHeight: 370,
width: 675,
minWidth: 675,
maxWidth: 675,
webPreferences: {
contextIsolation: false,
enableRemoteModule: true,
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
});
/**
* If not using in production, use the loading window
* and run Flask in shell.
*/
if (isDevMode) {
(browserWindows.loadingWindow = new BrowserWindow({ frame: false }));
createLoadingWindow().then(() => createMainWindow(port));
spawn(`python app.py ${port}`, {
detached: true,
shell: true,
stdio: 'inherit'
});
} else {
/**
* If using in production, use the main window
* and run bundled app.exe file.
*/
createMainWindow(port);
spawn(`start ./resources/app/app.exe ${port}`, {
detached: false,
shell: true,
stdio: 'pipe'
});
}
app.on('activate', () => {
/**
* On macOS 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 (BrowserWindow.getAllWindows().length === 0) createMainWindow(port);
});
/**
* Ensures that only a single instance of the app
* can run, this correlates with the "name" property
* used in `package.json`.
*/
const initialInstance = app.requestSingleInstanceLock();
if (!initialInstance) app.quit();
else {
app.on('second-instance', () => {
if (browserWindows.mainWindow?.isMinimized()) { browserWindows.mainWindow?.restore(); }
browserWindows.mainWindow?.focus();
});
}
/**
* Quit when all windows are closed, except on macOS. There, it's common
* for applications and their menu bar to stay active until the user quits
* explicitly with Cmd + Q.
*/
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') shutdown(port);
});
});