/
main.js
102 lines (90 loc) · 2.49 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
const log = require('./libs/logger');
const config = require('./config/config');
const { fork, execFile, execFileSync, spawn } = require('child_process');
const path = require('path');
const fse = require('fs-extra');
const url = require('url');
const { app, BrowserWindow } = require('electron');
const utils = require('./libs/utils');
log.setLogFileName('main', config.logPath, true);
log.scheduleClean(24 * 60 * 60 * 1000);
log.writeLog('******** process start, pid:' + process.pid + ' ********', 'info');
// if (environment.production) {
// enableProdMode();
// if(window){
// window.console.log=function(){};
// }
// }
let pidServer;
let bTerminate=false;
app.on('ready', async ()=>{
await utils.sleep(2000);
createWindow();
});
app.on('window-all-closed', () => {
// darwin = MacOS
log.writeLog('electron window-all-closed', 'info');
if (process.platform !== 'darwin') {
bTerminate = true;
app.quit();
pidServer.kill('SIGINT', );
}
});
app.on('activate', async () => {
if (win === null) {
await utils.sleep(3000);
createWindow();
}
});
function createWindow() {
// Create the browser window.
log.writeLog('electron create borwser window', 'info');
win = new BrowserWindow({
width: 1024,
height: 800,
maximizable: true,
autoHideMenuBar: true,
resizable: true,
});
try{
win.loadURL('http://localhost:1688');
log.writeLog('electron load url', 'info');
// win.loadURL(url.format({
// pathname: path.join(__dirname, 'public/index.html'),
// protocol: 'file:',
// slashes: true
// }));
}catch(err){
log.writeLog(err.message, 'error');
// console.log(err.message);
}
// Open DevTools.
// win.webContents.openDevTools()
// When Window Close.
win.on('closed', () => {
log.writeLog('electron window-closed', 'info');
win = null;
});
}
const cwd = process.cwd();
function launchServer() {
let serverPath = path.resolve(cwd, config.serverPath);
if (!fse.existsSync(serverPath)) {
log.writeLog('Server (' + serverPath + ') Not found', 'error');
} else {
try {
// let pidServer = spawn(process.execPath, [serverPath], {cwd: cwd});
pidServer = fork(serverPath, {cwd: cwd});
} catch (err) {
log.writeLog(err.message);
}
log.writeLog('Server launched', 'info');
pidServer.on('exit', (code, signal) => {
if(!bTerminate){
log.writeLog('Server exit and try to restart', 'warn');
launchServer();
}
});
}
}
launchServer();