-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.js
217 lines (206 loc) · 7.63 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
import {app, BrowserWindow, dialog} from "electron";
import getPort from "get-port";
import decompress from "decompress";
import child_process from "child_process";
import requestPromise from "minimal-request-promise";
import path from "path";
import fs from "fs";
import translations from "./translations/i18n.js";
let i18n;
let mainWindow = null;
let loading = null;
let serverProcess = null;
let allowClose = false;
const jreFolder = 'jdk-17+20';
function error_log(exception) {
fs.appendFile('error.log', exception.stack + "\n", (err) => {
if (err) throw err;
});
}
try {
const gotTheLock = app.requestSingleInstanceLock();
const showApplication = function (appUrl) {
mainWindow = new BrowserWindow({
title: i18n.__('application-name')
, show: false
, width: 1200
, height: 800
, frame: true
});
mainWindow.setMenu(null);
mainWindow.loadURL(appUrl);
mainWindow.once('ready-to-show', () => {
loading.hide();
mainWindow.show();
});
mainWindow.on('closed', function () {
mainWindow = null;
app.quit();
});
mainWindow.on('close', function (e) {
if (serverProcess && !allowClose) {
dialog.showMessageBox(this, {
type: 'question'
, buttons: [i18n.__('yes'), i18n.__('no')]
, title: i18n.__('confirm')
, message: i18n.__("are-you-sure-you-want-to-quit")
}).then(result => {
if (result.response === 0) {
allowClose = true;
app.quit();
}
});
e.preventDefault();
}
});
};
const awaitStartUp = function (appUrl, callback) {
requestPromise.get(appUrl).then(function (response) {
callback();
}, function (response) {
setTimeout(function () {
awaitStartUp(appUrl, callback);
}, 200);
});
};
const focusSecondInstance = function () {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
})
};
const getJavaFile = function () {
var files = fs.readdirSync(app.getAppPath() + "/java/");
var filename = null;
for (var i in files) {
if (path.extname(files[i]) === ".jar") {
filename = path.basename(files[i]);
break;
}
}
if (!filename) {
throw new Error("There is no JAR file in ./java/ !");
}
return filename;
};
const showStartUpErrorMessage = function () {
setTimeout(function () {
dialog.showMessageBox(null, {
type: 'error'
, buttons: [i18n.__('ok')]
, title: i18n.__("java-runtime-not-available")
, message: i18n.__("java-runtime-not-available-long")
});
}, 200);
}
const spawnServerProcess = function (port) {
var filename = getJavaFile();
const platform = process.platform;
if (platform === 'win32') {
return child_process.spawn(jreFolder + path.sep + 'bin' + path.sep + 'java', ['-jar', '-Dvaadin.productionMode=true', '-Dserver.port=' + port, filename, '--logging.file=application.log'], {
cwd: app.getAppPath() + path.sep + 'java' + path.sep
}).on('error', function (code, signal) {
'+ path.sep +'
showStartUpErrorMessage();
});
} else if (platform === 'darwin') {
child_process.exec('chmod +X ' + app.getAppPath() + '/java/' + jreFolder + '/Contents/Home/bin/' + 'java');
if (!app.getAppPath().startsWith("/Applications/")) {
dialog.showMessageBox(null, {
type: 'error'
, buttons: [i18n.__('ok')]
, title: i18n.__('wrong-directory')
, message: i18n.__('wrong-directory-long')
});
app.quit();
return null;
}
return child_process.spawn(jreFolder + '/Contents/Home/bin/java', ['-jar', '-Dvaadin.productionMode=true', '-Dserver.port=' + port, filename, '--logging.file=application.log'], {
cwd: app.getAppPath() + '/java/'
}).on('error', function (code, signal) {
showStartUpErrorMessage();
});
} else if (platform === 'linux') {
return child_process.spawn(jreFolder + '/bin/java', ['-jar', '-Dvaadin.productionMode=true', '-Dserver.port=' + port, filename, '--logging.file=application.log'], {
cwd: app.getAppPath() + '/java/'
}).on('error', function (code, signal) {
showStartUpErrorMessage();
});
} else {
throw new Error("Platform not supported");
}
};
const showLoadingScreen = function () {
loading = new BrowserWindow({
show: true
, frame: false
, width: 500
, height: 280
});
loading.loadURL('file://' + app.getAppPath() + '/loading.html');
};
const beginStartUp = function () {
(async () => {
try {
const port = await getPort();
serverProcess = spawnServerProcess(port);
var appUrl = "http://localhost:" + port;
awaitStartUp(appUrl, function () {
showApplication(appUrl);
});
} catch (e) {
error_log(e);
}
})();
}
if (!gotTheLock) {
app.quit()
} else {
focusSecondInstance();
app.on('window-all-closed', function () {
app.quit();
});
app.on('ready', function () {
i18n = new translations();
try {
showLoadingScreen();
const platform = process.platform;
const jrePath = app.getAppPath() + path.sep + 'java' + path.sep + jreFolder;
const compressedJreFilePath = app.getAppPath() + path.sep + 'java' + path.sep;
const extractionTargetPath = app.getAppPath() + path.sep + 'java' + path.sep;
let zipFileName;
if (platform === 'win32') {
zipFileName = 'jre_windows.zip';
} else if (platform === 'darwin') {
zipFileName = 'jre_mac.tar.gz';
} else if (platform === 'linux') {
zipFileName = 'jre_linux.tar.gz';
}
if (zipFileName) {
if (!fs.existsSync(jrePath)) {
decompress(compressedJreFilePath + zipFileName, extractionTargetPath).then(files => {
// remove compressed jre once unpacked
fs.unlinkSync(compressedJreFilePath + zipFileName);
beginStartUp();
});
} else {
beginStartUp();
}
} else {
throw new Error("Platform not supported");
}
} catch (e) {
error_log(e);
}
});
app.on('will-quit', function () {
serverProcess.kill('SIGINT');
});
}
} catch (e) {
error_log(e);
}