forked from rotki/rotki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
180 lines (151 loc) · 5.3 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
const electron = require('electron');
const app = electron.app;
const ipcMain = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const fs = require('fs');
/*************************************************************
* window management
*************************************************************/
let mainWindow = null;
function disableAnimationsForTest() {
if (process.env.ROTKEHLCHEN_ENVIRONMENT === 'test') {
let webContents = mainWindow.webContents;
webContents.once('dom-ready', () => {
let code = `
$(document.body).addClass('no-animations');
window.jQuery.fx.off = true;
let pluginDefaults = window.jconfirm.pluginDefaults;
pluginDefaults.animation = 'none';
pluginDefaults.closeAnimation = 'none';
pluginDefaults.backgroundDismissAnimation = 'none';
pluginDefaults.animationSpeed = 0;
pluginDefaults.animationBounce = 0;
`;
mainWindow.webContents.executeJavaScript(code, true)
.then(value => console.log(value));
});
}
}
function setupInspectMenu() {
if (process.env.ROTKEHLCHEN_ENVIRONMENT === 'development') {
let rightClickPosition = null;
const menu = new electron.Menu();
const menuItem = new electron.MenuItem({
label: 'Inspect Element',
click: () => {
mainWindow.inspectElement(rightClickPosition.x, rightClickPosition.y)
}
});
menu.append(menuItem);
mainWindow.webContents.on('context-menu', (e, params) => {
e.preventDefault();
rightClickPosition = {x: params.x, y: params.y};
menu.popup(mainWindow)
}, false);
}
}
const createWindow = () => {
mainWindow = new BrowserWindow({width: 800, height: 600});
disableAnimationsForTest();
mainWindow.loadURL(require('url').format({
pathname: path.join(__dirname, 'ui', 'index.html'),
protocol: 'file:',
slashes: true
}));
// uncomment for the final app to have dev tools opened
// mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
setupInspectMenu();
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
/*************************************************************
* py process
*************************************************************/
const PY_DIST_FOLDER = 'rotkehlchen_py_dist';
let pyProc = null;
let pyPort = null;
const guessPackaged = () => {
const fullPath = path.join(__dirname, PY_DIST_FOLDER);
return fs.existsSync(fullPath);
};
const selectPort = () => {
// TODO: Perhaps find free port and return it here?
pyPort = 4242;
return pyPort;
};
var pyproc_fail_notifier = null;
// Listen for ack messages from renderer process
ipcMain.on('ack', (event, arg) => {
// when ack is received stop the pyproc fail notifier
clearInterval(pyproc_fail_notifier);
});
function log_and_quit(msg) {
console.log(msg);
app.quit();
}
const createPyProc = () => {
let port = '' + selectPort();
if (guessPackaged()) {
let dist_dir = path.join(__dirname, PY_DIST_FOLDER);
let files = fs.readdirSync(dist_dir);
if (files.length === 0) {
log_and_quit('No files found in the dist directory');
} else if (files.length !== 1) {
log_and_quit('Found more than one file in the dist directory');
}
let executable = files[0];
if (!executable.startsWith('rotkehlchen-')) {
log_and_quit('Unexpected executable name "' + executable + '" in the dist directory');
}
executable = path.join(dist_dir, executable);
pyProc = require('child_process').execFile(executable, ["--zerorpc-port", port]);
} else {
let args = ["-m", "rotkehlchen", "--zerorpc-port", port];
if (process.env.ROTKEHLCHEN_ENVIRONMENT === 'test') {
let tempPath = path.join(app.getPath('temp'), 'rotkehlchen');
if (!fs.existsSync(tempPath)) {
fs.mkdirSync(tempPath)
}
args.push('--data-dir', tempPath)
}
pyProc = require('child_process').spawn('python', args);
}
pyProc.on('error', (err) => {
console.error(err);
console.log('Failed to start python subprocess.');
});
pyProc.on('exit', function (code, signal) {
console.log("python subprocess killed with signal " + signal + " and code " + code);
if (code !== 0) {
// Notify main window every 2 seconds until it acks the notification
pyproc_fail_notifier = setInterval(function () {
mainWindow.webContents.send('failed', 'failed');
}, 2000);
}
});
if (pyProc != null) {
console.log('child process success on port ' + port);
}
console.log("CREATED PYPROCESS");
};
const exitPyProc = () => {
console.log("KILLING PYPROCESS");
pyProc.kill();
pyProc = null;
pyPort = null;
};
app.on('ready', createPyProc);
app.on('will-quit', exitPyProc);