-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
356 lines (314 loc) · 13.4 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
LIBRARIES = {
FS: require("fs"), // This library allows you to interact with local files on the machine.
Colors: require("colors"), // This library allows you to colorize the console outputs.
ChildProcess: require("child_process"), // This library allows you to execute terminal code on the local station.
ReadLine: require("readline"), // This library allows you to query the user in the terminal.
SocketIO: require("socket.io"), // This library allows you to design a socket server.
Path: require("path") // This library allows you to create paths without having to worry about the computer OS.
};
class Launcher {
Demo_mode = false;
constructor() {
const SELF = this;
if(process.argv[2] != undefined){
if(process.argv[2] == "demo"){
SELF.Demo_mode = true;
}
}
SELF.GitClientURL = "https://github.com/HeyHeyChicken/NOVA-Client.git";
SELF.GitServerURL = "https://github.com/HeyHeyChicken/NOVA-Server.git";
SELF.ClientPath = LIBRARIES.Path.join(__dirname, "/src/client");
SELF.ServerPath = LIBRARIES.Path.join(__dirname, "/src/server");
SELF.Settings = JSON.parse(LIBRARIES.FS.readFileSync(__dirname + "/settings.json", "utf8"));
SELF.SocketServer = null;
if(SELF.Demo_mode){
SELF.Settings.LaunchClientOnStart = true;
SELF.Settings.LaunchServerOnStart = true;
SELF.Settings.Debug = false;
}
console.log("######################################");
console.log("## ##");
console.log("## Welcome to NOVA's launcher ! ##");
console.log("## ##");
console.log("######################################");
SELF.CheckLicense();
SELF.InitialiseSocketServer();
SELF.CheckLaunchClientOnStartSettings(function(){
SELF.CheckLaunchServerOnStartSettings(function(){
SELF.Launch(SELF.Settings.LaunchServerOnStart, SELF.ServerPath, SELF.GitServerURL, "NOVA - Server", function(){
SELF.InstallPackages(SELF.Settings.LaunchServerOnStart, SELF.ServerPath, "NOVA - Server", function () {
SELF.Launch(SELF.Settings.LaunchClientOnStart, SELF.ClientPath, SELF.GitClientURL, "NOVA - Client", function(){
SELF.InstallPackages(SELF.Settings.LaunchClientOnStart, SELF.ClientPath, "NOVA - Client", function () {
if(SELF.Settings.LaunchServerOnStart === true){
SELF.Log("Starting the server ...", "green");
SELF.Terminal("node index.js", SELF.ServerPath);
}
if(SELF.Settings.LaunchClientOnStart === true){
SELF.Log("Starting the client ...", "green");
SELF.Terminal("node index.js", SELF.ClientPath);
}
});
});
});
});
});
});
}
// This function initializes the socket server connecting the launcher to the NOVA server.
InitialiseSocketServer(){
const SELF = this;
this.SocketServer = LIBRARIES.SocketIO();
this.SocketServer.on("connection", function(socket){ // A server has just connected to the launcher.
// If the server requests the launcher to display text in the console.
socket.on("log", function(_text, _color, _header){
SELF.Log(_text, _color, _header);
});
// If the server asks the launcher to restart.
socket.on("reboot_server", function(){
SELF.Log("Rebooting the server...", "green");
socket.emit("reboot");
setTimeout(function(){
if(SELF.Settings.LaunchServerOnStart === true){
SELF.Terminal("node index.js", SELF.ServerPath);
}
}, 1000);
});
// If the client asks the launcher to restart.
socket.on("reboot_client", function(){
SELF.Log("Rebooting the client...", "green");
socket.emit("reboot");
setTimeout(function(){
if(SELF.Settings.LaunchClientOnStart === true){
SELF.Terminal("node index.js", SELF.ClientPath);
}
}, 1000);
});
});
this.SocketServer.listen(8082);
}
// This function installs the required packages to run the application.
InstallPackages(_settings, _path, _name, _callback){
const SELF = this;
if(_settings === true) {
SELF.Terminal("npm install", _path, function (_error_code, _messages) {
if (_error_code === 0) {
SELF.Log("Your \"" + _name + "\" app's packages are installed.", "green");
if (_callback !== undefined) {
_callback();
}
} else {
console.log("npm install error : " + _error_code);
}
});
}
else{
if (_callback !== undefined) {
_callback();
}
}
}
// This function launches an instance of NOVA.
Launch(_settings, _path, _git, _name, _callback){
const SELF = this;
if(_settings === true){
if (!LIBRARIES.FS.existsSync(_path)) {
SELF.Log("It seems that you don't have \"" + _name + "\" installed, we are downloading it.", "green");
SELF.Terminal("git --version", null, function(_error_code, _messages){
if(_error_code === 0){
SELF.Terminal("git clone " + _git + " \"" + _path + "\"", null, function(_error_code, _messages){
if(_error_code === 0){
SELF.Log("The download went well.", "green");
if(_callback !== undefined){
_callback();
}
}
});
}
});
}
else{
const UP_TO_DATE = "Your \"" + _name + "\" app seems to be up to date.";
const NEW_VERSION = "A new version of \"" + _name + "\" is available.";
if(SELF.Settings.UpdateAtBoot === true){
SELF.CheckUpdate(_path, function(_updateAvailable){
if(_updateAvailable === true){
SELF.Log(UP_TO_DATE, "green");
if(_callback !== undefined){
_callback();
}
}
else{
SELF.Log(NEW_VERSION, "red");
SELF.Log("Because \"UpdateAtBoot\" is set to \"true\", we are starting the update.", "green");
SELF.Update(_path, function(){
if(_callback !== undefined){
_callback();
}
});
}
});
}
else{
SELF.CheckUpdate(_path, function(_updateAvailable){
if(_updateAvailable === true){
SELF.Log(UP_TO_DATE, "green");
}
else{
SELF.Log(NEW_VERSION, "red");
}
});
if(_callback !== undefined){
_callback();
}
}
}
}
else{
if(_callback !== undefined){
_callback();
}
}
}
// This function asks the user whether or not to start a client when starting the launcher.
CheckLaunchClientOnStartSettings(_callback){
const SELF = this;
if(SELF.Settings.LaunchClientOnStart === null){
SELF.AskQuestion("Do you want the launcher to auto start a client ? (Y/n)", function(_answer){
if(_answer.toLowerCase() === "y" || _answer.toLowerCase() === "yes" || _answer === ""){
SELF.Settings.LaunchClientOnStart = true;
}
else{
SELF.Settings.LaunchClientOnStart = false;
}
LIBRARIES.FS.writeFileSync(__dirname + "/settings.json", JSON.stringify(SELF.Settings, null, 4), "utf8");
_callback();
});
}
else{
_callback();
}
}
// This function asks the user whether or not to start a server when starting the launcher.
CheckLaunchServerOnStartSettings(_callback){
const SELF = this;
if(SELF.Settings.LaunchServerOnStart === null){
SELF.AskQuestion("Do you want the launcher to auto start a server ? (Y/n)", function(_answer){
if(_answer.toLowerCase() === "y" || _answer.toLowerCase() === "yes" || _answer === ""){
SELF.Settings.LaunchServerOnStart = true;
}
else{
SELF.Settings.LaunchServerOnStart = false;
}
LIBRARIES.FS.writeFileSync(__dirname + "/settings.json", JSON.stringify(SELF.Settings, null, 4), "utf8");
_callback();
});
}
else{
_callback();
}
}
// This function updates NOVA.
Update(_path, _callback){
const SELF = this;
SELF.Log("Updating...", "green");
SELF.Terminal("git pull", _path, function(_error_code, _messages){
if(_error_code === 0){
SELF.Log("The update went well.", "green");
if(_callback !== undefined){
_callback();
}
}
else{
console.log("git pull : " + _error_code);
}
});
}
// This function checks for updates.
CheckUpdate(_path, _callback){
const SELF = this;
SELF.Terminal("git fetch origin", _path, function(_error_code, _messages){
if(_error_code === 0){
SELF.Terminal("git status", _path, function(_error_code, _messages){
if(_error_code === 0){
if(_messages.includes("Your branch is up to date with 'origin/master'.")){
if(_callback !== undefined){
_callback(true);
}
}
else {
_callback(false);
}
}
});
}
});
}
// This function executes terminal commands on the local computer.
Terminal(_command, _path, _callback){
const SELF = this;
const MESSAGES = [];
const EXECUTION = LIBRARIES.ChildProcess.exec(_command, { cwd: _path });
if(SELF.Settings.Debug === true){
SELF.Log("Command : " + _command);
}
EXECUTION.stdout.on("data", (_data) => {
_data = _data.split("\n");
for(let i = 0; i < _data.length; i++){
if(_data[i].length > 0){
MESSAGES.push(_data[i]);
if(SELF.Settings.Debug === true){
SELF.Log(_data[i]);
}
}
}
});
EXECUTION.stderr.on("data", (_data) => {
_data = _data.split("\n");
for(let i = 0; i < _data.length; i++){
if(_data[i].length > 0){
MESSAGES.push(_data[i]);
if(SELF.Settings.Debug === true){
SELF.Log(_data[i]);
}
}
}
});
EXECUTION.on("close", (_error_code) => {
if(_callback !== undefined){
_callback(_error_code, MESSAGES);
}
});
}
// This function asks the user a question from the command prompt.
AskQuestion(_question, _callback) {
this.Log(_question, "green");
const RL = LIBRARIES.ReadLine.createInterface({
input: process.stdin
});
return new Promise(resolve => RL.question(_question, function(_answer) {
RL.close();
if(_callback !== undefined){
_callback(_answer);
}
}));
}
// This function checks the license.
CheckLicense() {
if(this.Settings.LicenseKey !== "non-commercial-and-evaluation"){
this.Log("Your license key is invalid.", "red");
process.exit();
}
}
// This function replaces the "console.log" function.
Log(_text, _color = "white", _header = "NOVA LAUNCHER"){
if(_text.length > 0){
if(LIBRARIES.Colors[_color] !== undefined){
console.log("[" + (LIBRARIES.Colors[_color](_header)) + "] " + _text);
}
else{
console.log(LIBRARIES.Colors.red("The color \"" + _color + "\" does not exist in the \"colors\" package."));
}
}
}
}
const LAUNCHER = new Launcher();