forked from siddu-k/bashmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
105 lines (88 loc) · 2.64 KB
/
Copy pathmain.js
File metadata and controls
105 lines (88 loc) · 2.64 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
const { app, BrowserWindow } = require('electron');
const { spawn } = require('child_process');
const path = require('path');
const http = require('http');
let mainWindow;
let flaskProcess;
const PORT = 5000;
const HOST = '127.0.0.1';
// We must preserve the python path to our bundled app or local python
const pythonCmd = process.env.VIRTUAL_ENV
? path.join(process.env.VIRTUAL_ENV, 'bin', 'python')
: (process.platform === 'win32' ? 'python' : 'python3');
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 900,
minHeight: 600,
title: "DevShell",
autoHideMenuBar: true, // Hide menu bar
webPreferences: {
nodeIntegration: false,
contextIsolation: true
}
});
// We keep trying to load the URL until Flask is ready
loadWhenReady(`http://${HOST}:${PORT}`);
mainWindow.on('closed', function () {
mainWindow = null;
});
}
function startFlaskServer() {
console.log(`Starting Python server... (using ${pythonCmd})`);
flaskProcess = spawn(pythonCmd, ['app.py'], {
cwd: __dirname,
env: {
...process.env,
DEV_SHELL_DATA_DIR: app.getPath('userData'),
}
});
flaskProcess.stdout.on('data', (data) => {
console.log(`Flask: ${data}`);
});
flaskProcess.stderr.on('data', (data) => {
console.error(`Flask Err: ${data}`);
});
flaskProcess.on('close', (code) => {
console.log(`Flask process exited with code ${code}`);
});
}
// Poller to check if Flask is up
function loadWhenReady(url) {
const checkServer = () => {
http.get(url, (res) => {
if (res.statusCode === 200) {
console.log('Server is ready. Loading UI...');
mainWindow.loadURL(url);
} else {
setTimeout(checkServer, 200);
}
}).on('error', (err) => {
console.log('Waiting for server...');
setTimeout(checkServer, 200);
});
};
checkServer();
}
app.on('ready', () => {
startFlaskServer();
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
// Ensure python child process is killed when app exits
app.on('will-quit', () => {
if (flaskProcess) {
flaskProcess.kill();
}
});