-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
89 lines (75 loc) · 2.19 KB
/
main.js
File metadata and controls
89 lines (75 loc) · 2.19 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
const { app, BrowserWindow, ipcMain } = require('electron');
const psTree = require('ps-tree');
const kill = require('tree-kill');
// Get PID of the current process
const parentPid = process.pid;
// Get all child processes of the current process
function getChildProcesses(parentPid, callback) {
psTree(parentPid, (err, children) => {
if (err) {
callback(err, null);
} else {
callback(null, children);
}
});
}
function createWindow() {
// Create the browser window.
let win = new BrowserWindow ({
width: 1000,
height: 650,
minWidth: 1000,
minHeight: 650,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// devTools: true,
},
});
// Load index.html
win.loadFile('index.html');
ipcMain.on('get-app-version', (event) => {
event.sender.send('app-version', app.getVersion());
});
win.on('close', (event) => {
// Prevent the window from closing immediately
event.preventDefault();
// Function to terminate child processes and perform cleanup
function cleanupAndClose() {
getChildProcesses(parentPid, (err, children) => {
if (err) {
// Destroy the window if there's an error
win.destroy();
} else {
let childProcesses = children.filter(child => child.COMMAND === 'python.exe');
if (process.platform === 'darwin') {
childProcesses = children
}
let killPromises = childProcesses.map(process => {
return new Promise((resolve, reject) => {
kill(process.PID, 'SIGTERM', (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
// Wait for all promises to resolve
Promise.all(killPromises).then(() => {
// Destroy the window
win.destroy();
}).catch(error => {
// Destroy the window
win.destroy();
});
}
});
}
// Call the cleanup function
cleanupAndClose();
});
// win.openDevTools();
}
app.whenReady().then(createWindow);