-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
187 lines (157 loc) · 5.44 KB
/
index.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
var util = require('util');
global.logs = '';
global.console.log = function(x){
global.logs += JSON.stringify(x) + '\n';
process.stdout.write(util.format.apply(util, arguments));
};
var fs = require('fs');
var gui = require('nw.gui');
var path = require('path');
var nwPath = process.execPath;
var nwDir = path.dirname(nwPath);
var spawn = require('child_process').spawn;
var $ = function (selector) {
return document.querySelector(selector);
}
if (process.platform == "darwin") {
var menu = new gui.Menu({ type: 'menubar' });
menu.createMacBuiltin && menu.createMacBuiltin(window.document.title);
gui.Window.get().menu = menu;
}
initApp();
var liteServer;
function startNode(serverPath) {
var nodePath = 'node';
if (process.env.NODE_ENV === 'dev' && /^win/.test(process.platform)) nodePath = global.__dirname + '\\node\\win32\\node.exe';
// if (process.env.NODE_ENV === 'dev' && /^darwin/.test(process.platform)) nodePath = global.__dirname + '/node/osx64/node';
if (process.env.NODE_ENV != 'dev' && /^win/.test(process.platform)) {
nodePath = nwDir + '\\node.exe';
}
if (process.env.NODE_ENV != 'dev' && process.platform === 'darwin') {
var folderDir = require(global.__dirname + '/config').path;
nodePath = folderDir + '/node';
}
console.log("nodePath is " + nodePath);
liteServer = spawn(nodePath, [serverPath]);
liteServer.stdout.on('data', function(data) {
console.log(data.toString());
});
liteServer.stderr.on('data', function(data) {
console.log(data.toString());
});
liteServer.on('close', function(status) {
liteServer = null;
console.log(`Terminal MCS Lite server: ${status}`);
});
}
function startMCSLiteService() {
return new Promise(function(resolve, reject) {
if (liteServer || (liteServer && !liteServer.killed)) {
return reject('MCS Lite service is still running');
}
if (process.platform === 'darwin') {
if (process.env.NODE_ENV === 'dev') {
startNode('./server');
} else {
let folderDir;
try {
folderDir = require(global.__dirname + '/config').path;
startNode(folderDir + '/mcs-lite-app/server');
} catch (e) {
$("#app").innerHTML = "<p>Please click \"setup\" file to setup your env and reopen this mcs-lite-app.app.<p>";
}
}
}
if (/^win/.test(process.platform)) {
const folderDir = nwDir + '\\mcs-lite-app';
startNode(folderDir + '\\server');
}
console.log('MCS lite service is started.');
return resolve('MCS lite service is started.');
});
}
function stopMCSLiteService() {
return new Promise(function(resolve, reject) {
if (liteServer) {
liteServer.on('close', function(status) {
liteServer = null;
console.log(`Terminal MCS Lite server: ${status}`);
return resolve('MCS Lite service is stopped.');
});
liteServer.kill();
setTimeout(function() {
return reject('Stop request failed.');
}, 2000);
} else return reject('MCS Lite service is not running.');
});
}
global.startMCSLiteService = startMCSLiteService;
global.stopMCSLiteService = stopMCSLiteService;
function initApp() {
var adminServer;
var $admin;
setTimeout(function() {
if (process.platform == "darwin") {
if (process.env.NODE_ENV === 'dev') {
adminServer = require('./adminServer/index');
$admin = require('./configs/admin');
} else {
var folderDir = require(global.__dirname + '/config').path + '/mcs-lite-app';
adminServer = require(folderDir + '/adminServer');
$admin = require(folderDir + '/configs/admin');
}
}
if (/^win/.test(process.platform)) {
var folderDir = nwDir + '\\mcs-lite-app';
adminServer = require(folderDir + '\\adminServer\\index');
$admin = require(folderDir + '\\configs\\admin');
}
adminServer.listen($admin.port);
var win = gui.Window.get();
var tray = new gui.Tray({
icon: process.platform === 'darwin'
? 'icon_tray@2x.png'
: 'icon_tray_windows.png',
tooltip: 'MCS Lite',
});
var trayMenu = new gui.Menu();
var showMenuItem = new gui.MenuItem({
type: 'normal',
click: function() {
win.show();
win.setShowInTaskbar(true);
},
label: 'Show Admin Window',
});
var quitMenuItem = new gui.MenuItem({
type: 'normal',
click: function() {
quitApp();
},
label: 'Quit MCS Lite',
});
trayMenu.append(showMenuItem);
trayMenu.append(quitMenuItem);
tray.menu = trayMenu;
win.show();
function quitApp() {
stopMCSLiteService();
tray.remove();
tray = null;
win.close(true);
}
win.on('close', function(event) {
win.hide();
win.setShowInTaskbar(false);
if (event === 'quit') {
quitApp();
}
});
if (process.env.NODE_ENV === 'dev') {
document.body.innerHTML += '<iframe frameborder="0" src="http://' + $admin.host + ':' + $admin.port + '/login' + '" style="width: 100%; height: 580px; overflow: auto;" nwdisable nwfaketop>';
// document.body.innerHTML += '<iframe frameborder="0" src="' + $admin.webClient.redirect.dev + '" style="width: 100%; height: 580px; overflow: auto;" nwdisable nwfaketop>';
} else {
document.body.innerHTML += '<iframe frameborder="0" src="http://' + $admin.host + ':' + $admin.port + '/login' + '" style="width: 100%; height: 580px; overflow: auto;" nwdisable nwfaketop>';
}
}, 500);
}