2016-04-05 14:35:11 -04:00
|
|
|
"use strict";
|
2016-03-27 20:40:07 +02:00
|
|
|
|
2016-03-30 12:20:46 +02:00
|
|
|
//load modules
|
2016-04-05 14:35:11 -04:00
|
|
|
const walk = require("walk");
|
|
|
|
const fs = require("fs");
|
|
|
|
const Server = require(__dirname + "/server.js");
|
|
|
|
const spawn = require("child_process").spawn;
|
|
|
|
const electron = require("electron");
|
|
|
|
const defaultModules = require(__dirname + "/../modules/default/defaultmodules.js");
|
|
|
|
const path = require("path");
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
// Config
|
|
|
|
var config = {};
|
2016-03-27 20:40:07 +02:00
|
|
|
// Module to control application life.
|
|
|
|
const app = electron.app;
|
|
|
|
// Module to create native browser window.
|
|
|
|
const BrowserWindow = electron.BrowserWindow;
|
|
|
|
|
2016-03-30 16:25:24 +02:00
|
|
|
var nodeHelpers = [];
|
|
|
|
|
2016-03-27 20:40:07 +02:00
|
|
|
// Keep a global reference of the window object, if you don't, the window will
|
|
|
|
// be closed automatically when the JavaScript object is garbage collected.
|
|
|
|
let mainWindow;
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
function createWindow() {
|
2016-03-30 12:20:46 +02:00
|
|
|
// Create the browser window.
|
2016-03-31 12:44:00 +02:00
|
|
|
mainWindow = new BrowserWindow({width: 800, height: 600, fullscreen: true, autoHideMenuBar: true, webPreferences: {nodeIntegration: false}});
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
// and load the index.html of the app.
|
|
|
|
//mainWindow.loadURL('file://' + __dirname + '../../index.html');
|
2016-04-05 14:35:11 -04:00
|
|
|
mainWindow.loadURL("http://localhost:" + config.port);
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
// Open the DevTools.
|
|
|
|
//mainWindow.webContents.openDevTools();
|
|
|
|
|
|
|
|
// Emitted when the window is closed.
|
2016-04-05 14:35:11 -04:00
|
|
|
mainWindow.on("closed", function() {
|
2016-03-30 12:20:46 +02:00
|
|
|
// Dereference the window object, usually you would store windows
|
|
|
|
// in an array if your app supports multi windows, this is the time
|
|
|
|
// when you should delete the corresponding element.
|
|
|
|
mainWindow = null;
|
|
|
|
});
|
2016-03-27 20:40:07 +02:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
function loadConfig(callback) {
|
2016-03-30 12:20:46 +02:00
|
|
|
console.log("Loading config ...");
|
2016-04-05 14:35:11 -04:00
|
|
|
var defaults = require(__dirname + "/defaults.js");
|
|
|
|
var configFilename = __dirname + "/../config/config.js";
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
try {
|
2016-04-05 14:35:11 -04:00
|
|
|
fs.accessSync(configFilename, fs.R_OK);
|
|
|
|
var c = require(configFilename);
|
2016-03-30 12:20:46 +02:00
|
|
|
var config = Object.assign(defaults, c);
|
|
|
|
callback(config);
|
|
|
|
} catch (e) {
|
2016-04-05 14:35:11 -04:00
|
|
|
callback(defaults);
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:35:29 +02:00
|
|
|
function loadModule(module) {
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
var elements = module.split("/");
|
2016-04-01 17:35:29 +02:00
|
|
|
var moduleName = elements[elements.length - 1];
|
2016-04-05 14:35:11 -04:00
|
|
|
var moduleFolder = __dirname + "/../modules/" + module;
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-01 17:35:29 +02:00
|
|
|
if (defaultModules.indexOf(moduleName) !== -1) {
|
2016-04-05 14:35:11 -04:00
|
|
|
moduleFolder = __dirname + "/../modules/default/" + module;
|
2016-04-01 17:35:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
var helperPath = moduleFolder + "/node_helper.js";
|
2016-04-01 17:35:29 +02:00
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
var loadModule = true;
|
2016-03-30 12:20:46 +02:00
|
|
|
try {
|
2016-04-05 14:35:11 -04:00
|
|
|
fs.accessSync(helperPath, fs.R_OK);
|
2016-03-30 12:20:46 +02:00
|
|
|
} catch (e) {
|
2016-03-30 14:49:37 +02:00
|
|
|
loadModule = false;
|
2016-03-30 12:20:46 +02:00
|
|
|
console.log("No helper found for module: " + moduleName + ".");
|
|
|
|
}
|
2016-03-30 14:49:37 +02:00
|
|
|
|
|
|
|
if (loadModule) {
|
|
|
|
var Module = require(helperPath);
|
|
|
|
var m = new Module();
|
2016-04-05 14:35:11 -04:00
|
|
|
m.setName(moduleName);
|
|
|
|
m.setPath(path.resolve(moduleFolder));
|
|
|
|
nodeHelpers.push(m);
|
2016-03-30 14:49:37 +02:00
|
|
|
}
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadModules(modules) {
|
|
|
|
console.log("Loading module helpers ...");
|
|
|
|
|
|
|
|
for (var m in modules) {
|
|
|
|
loadModule(modules[m]);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("All module helpers loaded.");
|
|
|
|
}
|
|
|
|
|
|
|
|
loadConfig(function(c) {
|
|
|
|
config = c;
|
|
|
|
|
|
|
|
var modules = [];
|
|
|
|
|
|
|
|
for (var m in config.modules) {
|
|
|
|
var module = config.modules[m];
|
|
|
|
if (modules.indexOf(module.module) === -1) {
|
|
|
|
modules.push(module.module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadModules(modules);
|
2016-03-30 16:25:24 +02:00
|
|
|
|
2016-04-05 14:22:34 +02:00
|
|
|
var server = new Server(config, function(app, io) {
|
2016-04-05 14:35:11 -04:00
|
|
|
console.log("Server started ...");
|
2016-03-30 16:25:24 +02:00
|
|
|
|
|
|
|
for (var h in nodeHelpers) {
|
|
|
|
var nodeHelper = nodeHelpers[h];
|
2016-04-05 14:22:34 +02:00
|
|
|
nodeHelper.setExpressApp(app);
|
2016-03-30 16:25:24 +02:00
|
|
|
nodeHelper.setSocketIO(io);
|
|
|
|
nodeHelper.start();
|
|
|
|
}
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
console.log("Sockets connected & modules started ...");
|
2016-03-30 16:25:24 +02:00
|
|
|
|
|
|
|
});
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
2016-03-27 22:22:04 +02:00
|
|
|
|
2016-03-27 20:40:07 +02:00
|
|
|
// This method will be called when Electron has finished
|
|
|
|
// initialization and is ready to create browser windows.
|
2016-04-05 14:35:11 -04:00
|
|
|
app.on("ready", function() {
|
|
|
|
console.log("Launching application.");
|
2016-03-30 16:25:24 +02:00
|
|
|
createWindow();
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
2016-03-27 20:40:07 +02:00
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
2016-04-05 14:35:11 -04:00
|
|
|
app.on("window-all-closed", function() {
|
2016-03-30 12:20:46 +02:00
|
|
|
// On OS X it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
2016-04-05 14:35:11 -04:00
|
|
|
if (process.platform !== "darwin") {
|
2016-03-30 12:20:46 +02:00
|
|
|
app.quit();
|
|
|
|
}
|
2016-03-27 20:40:07 +02:00
|
|
|
});
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
app.on("activate", function() {
|
2016-03-30 12:20:46 +02:00
|
|
|
// On OS X it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow();
|
|
|
|
}
|
2016-03-27 20:40:07 +02:00
|
|
|
});
|