From 071043a70e73ee0f764a32f8aafc8d1e1139e686 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Thu, 16 Feb 2017 10:12:17 +0100 Subject: [PATCH] create app config.json in iguana folder --- main.js | 50 +++++++++--------------------------- routes/shepherd.js | 63 +++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 66 deletions(-) diff --git a/main.js b/main.js index 8aa06f2..7ca659a 100644 --- a/main.js +++ b/main.js @@ -3,22 +3,20 @@ const electron = require('electron'), app = electron.app, - BrowserWindow = electron.BrowserWindow; -var express = require('express'), - bodyParser = require('body-parser'); -const path = require('path'), + BrowserWindow = electron.BrowserWindow, + path = require('path'), url = require('url'), os = require('os'), spawn = require('child_process').spawn, exec = require('child_process').exec, - fixPath = require('fix-path'); -var fs = require('fs'), + fixPath = require('fix-path'); +var express = require('express'), + bodyParser = require('body-parser'), + fs = require('fs'), fsnode = require('fs'), fs = require('fs-extra'), mkdirp = require('mkdirp'), - pm2 = require('pm2'); - -var iguanaAppPort = 17777; + pm2 = require('pm2'); Promise = require('bluebird'); @@ -28,30 +26,6 @@ if (os.platform() === 'linux') { process.env.ELECTRON_RUN_AS_NODE = true; console.log(process.env); } -if (os.platform() === 'darwin') { - fixPath(); - var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'), - iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; -} - -if (os.platform() === 'linux') { - var iguanaBin = path.join(__dirname, '../assets/bin/linux64/iguana'), - iguanaDir = process.env.HOME + '/.iguana'; -} - -if (os.platform() === 'win32') { - var iguanaDir = process.env.APPDATA + '/iguana'; - iguanaDir = path.normalize(iguanaDir); -} - -var appSettings = { - "edexGuiOnly": true, - "iguanaGuiOnly": false, - "manualIguanaStart": false, - "skipBasiliskNetworkCheck": false -}; - -var appConfig = appSettings; // preload.js const _setImmediate = setImmediate, @@ -73,7 +47,7 @@ process.once('loaded', () => { var shepherd = require('./routes/shepherd'), guiapp = express(); -shepherd.saveLocalAppConf(appSettings); +var appConfig = shepherd.loadLocalConfig(); guiapp.use(bodyParser.json()); // support json encoded bodies guiapp.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies @@ -87,8 +61,8 @@ guiapp.use('/gui', express.static(guipath)); guiapp.use('/shepherd', shepherd); -var rungui = guiapp.listen(iguanaAppPort, function () { - console.log('guiapp listening on port ' + iguanaAppPort + '!'); +var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { + console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!'); }) module.exports = guiapp; @@ -214,9 +188,9 @@ function createWindow (status) { // load our index.html (i.e. easyDEX GUI) if (appConfig.edexGuiOnly) { - mainWindow.loadURL('http://127.0.0.1:' + iguanaAppPort + '/gui/EasyDEX-GUI/'); + mainWindow.loadURL('http://127.0.0.1:' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/'); } else { - mainWindow.loadURL('http://127.0.0.1:' + iguanaAppPort + '/gui/main.html'); + mainWindow.loadURL('http://127.0.0.1:' + appConfig.iguanaAppPort + '/gui/main.html'); } // DEVTOOLS - only for dev purposes - ca333 diff --git a/routes/shepherd.js b/routes/shepherd.js index 9c411a1..487ab7c 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -56,6 +56,15 @@ if (os.platform() === 'win32') { iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); } +shepherd.appConfig = { + "edexGuiOnly": true, + "iguanaGuiOnly": false, + "manualIguanaStart": false, + "skipBasiliskNetworkCheck": false, + "iguanaAppPort": 17777, + "iguanaCorePort": 7778 +}; + console.log(iguanaDir); console.log(iguanaBin); @@ -65,10 +74,22 @@ shepherd.get('/', function(req, res, next) { }); shepherd.get('/appconf', function(req, res, next) { - var obj = JSON.parse(fs.readFileSync(iguanaDir + '/config.json', 'utf8')); + var obj = shepherd.loadLocalConfig(); res.send(obj); }); +shepherd.loadLocalConfig = function() { + if (fs.existsSync(iguanaDir + '/config.json')) { + var localAppConfig = fs.readFileSync(iguanaDir + '/config.json', 'utf8'); + console.log('app config set from local file'); + return JSON.parse(localAppConfig); + } else { + console.log('local config file is not found!'); + shepherd.saveLocalAppConf(shepherd.appConfig); + return shepherd.appConfig; + } +}; + shepherd.post('/herd', function(req, res) { console.log('======= req.body ======='); //console.log(req); @@ -274,27 +295,11 @@ function slayer(flock) { } shepherd.saveLocalAppConf = function(appSettings) { - var appConfFileName = iguanaDir + '/conf.json'; - console.log(iguanaDir); - - var CheckFileExists = function() { - return new Promise(function(resolve, reject) { - var result = 'Check app conf.json file exists is done' - - fs.ensureFile(appConfFileName, function(err) { - console.log(err); // => null - }); - - setTimeout(function() { - console.log(result); - resolve(result); - }, 2000); - }); - } + var appConfFileName = iguanaDir + '/config.json'; var FixFilePermissions = function() { return new Promise(function(resolve, reject) { - var result = 'conf.json file permissions updated to Read/Write'; + var result = 'config.json file permissions updated to Read/Write'; fsnode.chmodSync(appConfFileName, '0666'); @@ -307,9 +312,14 @@ shepherd.saveLocalAppConf = function(appSettings) { var FsWrite = function() { return new Promise(function(resolve, reject) { - var result = 'write file is done' - - fs.writeFile(appConfFileName, JSON.stringify(appSettings), 'utf8', function(err) { + var result = 'config.json write file is done' + + fs.writeFile(appConfFileName, + JSON.stringify(appSettings) + .replace(/,/g, ',\n') // format json in human readable form + .replace(/:/g, ': ') + .replace('{', '{\n') + .replace('}', '\n}'), 'utf8', function(err) { if (err) return console.log(err); }); @@ -317,17 +327,14 @@ shepherd.saveLocalAppConf = function(appSettings) { fsnode.chmodSync(appConfFileName, '0666'); setTimeout(function() { console.log(result); - console.log('app conf.json file is copied successfully at: ' + iguanaConfsDir); + console.log('app conf.json file is created successfully at: ' + iguanaConfsDir); resolve(result); }, 2000); }); } - CheckFileExists() - .then(function(result) { - return FixFilePermissions(); - }) - .then(FsWrite); + FsWrite() + .then(FixFilePermissions()); // not really required now } function setConf(flock) {