From aef5953b8a456c55d47830231c350d0194b19dac Mon Sep 17 00:00:00 2001 From: pbca26 Date: Thu, 12 Oct 2017 18:14:03 +0300 Subject: [PATCH] settings load on init fix --- main.js | 4 +- routes/shepherd.js | 342 ++++++++++++++++++++------------------------- 2 files changed, 157 insertions(+), 189 deletions(-) diff --git a/main.js b/main.js index 7020e5a..4ef9bab 100644 --- a/main.js +++ b/main.js @@ -29,6 +29,7 @@ if (osPlatform === 'linux') { // GUI APP settings and starting gui on address http://120.0.0.1:17777 var shepherd = require('./routes/shepherd'); var guiapp = express(); +var appConfig = shepherd.loadLocalConfig(); // load app config const nativeCoindList = shepherd.scanNativeCoindBins(); shepherd.setVar('nativeCoindList', nativeCoindList); @@ -77,7 +78,6 @@ shepherd.log(`platform: ${osPlatform}`); shepherd.log(`os_release: ${os.release()}`); shepherd.log(`os_type: ${os.type()}`); -var appConfig = shepherd.loadLocalConfig(); // load app config appConfig['daemonOutput'] = false; // shadow setting let __defaultAppSettings = require('./routes/appConfig.js').config; @@ -305,7 +305,7 @@ function createAppSettingsWindow() { // initialise window appSettingsWindow = new BrowserWindow({ // dirty hack to prevent main window flash on quit width: 750, - height: !appConfig.experimentalFeatures ? 570 : 700, + height: 570, frame: false, icon: agamaIcon, show: false, diff --git a/routes/shepherd.js b/routes/shepherd.js index 2b1f55e..36a7a81 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -25,7 +25,7 @@ const CoinKey = require('coinkey'); const bitcoinJS = require('bitcoinjs-lib'); const coinSelect = require('coinselect'); const fixPath = require('fix-path'); -var crypto = require('crypto'); +const crypto = require('crypto'); var ps = require('ps-node'); var setconf = require('../private/setconf.js'); @@ -209,8 +209,157 @@ shepherd.appConfig = _appConfig.config; if (os.platform() === 'darwin') { fixPath(); - var agamaDir = `${process.env.HOME}/Library/Application Support/agama`, - agamaTestDir = `${process.env.HOME}/Library/Application Support/agama/test`, + var agamaDir = `${process.env.HOME}/Library/Application Support/Agama`; +} + +if (os.platform() === 'linux') { + var agamaDir = `${process.env.HOME}/.agama`; +} + +if (os.platform() === 'win32') { + var agamaDir = `${process.env.APPDATA}/Agama`; + agamaDir = path.normalize(agamaDir); +} + +shepherd.log = function(msg) { + if (shepherd.appConfig.dev || + shepherd.appConfig.debug) { + console.log(msg); + } + + appRuntimeLog.push({ + time: Date.now(), + msg: msg, + }); +}; + +shepherd.writeLog = function(data) { + const logLocation = `${agamaDir}/shepherd`; + const timeFormatted = new Date(Date.now()).toLocaleString('en-US', { hour12: false }); + + if (shepherd.appConfig.debug) { + if (fs.existsSync(`${logLocation}/agamalog.txt`)) { + fs.appendFile(`${logLocation}/agamalog.txt`, `${timeFormatted} ${data}\r\n`, function(err) { + if (err) { + shepherd.log('error writing log file'); + } + }); + } else { + fs.writeFile(`${logLocation}/agamalog.txt`, `${timeFormatted} ${data}\r\n`, function(err) { + if (err) { + shepherd.log('error writing log file'); + } + }); + } + } +} + +shepherd.loadLocalConfig = function() { + if (fs.existsSync(`${agamaDir}/config.json`)) { + let localAppConfig = fs.readFileSync(`${agamaDir}/config.json`, 'utf8'); + + shepherd.log('app config set from local file'); + shepherd.writeLog('app config set from local file'); + + // find diff between local and hardcoded configs + // append diff to local config + const compareJSON = function(obj1, obj2) { + let result = {}; + + for (let i in obj1) { + if (!obj2.hasOwnProperty(i)) { + result[i] = obj1[i]; + } + } + + return result; + }; + + if (localAppConfig) { + const compareConfigs = compareJSON(shepherd.appConfig, JSON.parse(localAppConfig)); + + if (Object.keys(compareConfigs).length) { + const newConfig = Object.assign(JSON.parse(localAppConfig), compareConfigs); + + shepherd.log('config diff is found, updating local config'); + shepherd.log('config diff:'); + shepherd.log(compareConfigs); + shepherd.writeLog('aconfig diff is found, updating local config'); + shepherd.writeLog('config diff:'); + shepherd.writeLog(compareConfigs); + + shepherd.saveLocalAppConf(newConfig); + return newConfig; + } else { + return JSON.parse(localAppConfig); + } + } else { + return shepherd.appConfig; + } + } else { + shepherd.log('local config file is not found!'); + shepherd.writeLog('local config file is not found!'); + shepherd.saveLocalAppConf(shepherd.appConfig); + + return shepherd.appConfig; + } +}; + +shepherd.saveLocalAppConf = function(appSettings) { + let appConfFileName = `${agamaDir}/config.json`; + + _fs.access(agamaDir, fs.constants.R_OK, function(err) { + if (!err) { + + const FixFilePermissions = function() { + return new Promise(function(resolve, reject) { + const result = 'config.json file permissions updated to Read/Write'; + + fsnode.chmodSync(appConfFileName, '0666'); + + setTimeout(function() { + shepherd.log(result); + shepherd.writeLog(result); + resolve(result); + }, 1000); + }); + } + + const FsWrite = function() { + return new Promise(function(resolve, reject) { + const 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(/{/g, '{\n') + .replace(/}/g, '\n}'), 'utf8', function(err) { + if (err) + return shepherd.log(err); + }); + + fsnode.chmodSync(appConfFileName, '0666'); + setTimeout(function() { + shepherd.log(result); + shepherd.log(`app conf.json file is created successfully at: ${agamaDir}`); + shepherd.writeLog(`app conf.json file is created successfully at: ${agamaDir}`); + resolve(result); + }, 2000); + }); + } + + FsWrite() + .then(FixFilePermissions()); + } + }); +} + +shepherd.appConfig = shepherd.loadLocalConfig(); + +if (os.platform() === 'darwin') { + fixPath(); + var agamaTestDir = `${process.env.HOME}/Library/Application Support/Agama/test`, komododBin = path.join(__dirname, '../assets/bin/osx/komodod'), komodocliBin = path.join(__dirname, '../assets/bin/osx/komodo-cli'), komodoDir = shepherd.appConfig.dataDir.length ? shepherd.appConfig.dataDir : `${process.env.HOME}/Library/Application Support/Komodo`, @@ -220,13 +369,12 @@ if (os.platform() === 'darwin') { zcashParamsDir = `${process.env.HOME}/Library/Application Support/ZcashParams`, chipsBin = path.join(__dirname, '../assets/bin/osx/chipsd'), chipscliBin = path.join(__dirname, '../assets/bin/osx/chips-cli'), - komodoDir = `${process.env.HOME}/Library/Application Support/Chips`, + chipsDir = `${process.env.HOME}/Library/Application Support/Chips`, coindRootDir = path.join(__dirname, '../assets/bin/osx/dex/coind'); } if (os.platform() === 'linux') { - var agamaDir = `${process.env.HOME}/.agama`, - agamaTestDir = `${process.env.HOME}/.agama/test`, + var agamaTestDir = `${process.env.HOME}/.agama/test`, komododBin = path.join(__dirname, '../assets/bin/linux64/komodod'), komodocliBin = path.join(__dirname, '../assets/bin/linux64/komodo-cli'), komodoDir = shepherd.appConfig.dataDir.length ? shepherd.appConfig.dataDir : `${process.env.HOME}/.komodo`, @@ -238,9 +386,7 @@ if (os.platform() === 'linux') { } if (os.platform() === 'win32') { - var agamaDir = `${process.env.APPDATA}/Agama`; - agamaDir = path.normalize(agamaDir); - agamaTestDir = `${process.env.APPDATA}/Agama/test`; + var agamaTestDir = `${process.env.APPDATA}/Agama/test`; agamaTestDir = path.normalize(agamaTestDir); komododBin = path.join(__dirname, '../assets/bin/win64/komodod.exe'), komododBin = path.normalize(komododBin), @@ -2079,18 +2225,6 @@ shepherd.getAppRuntimeLog = function() { }); }; -shepherd.log = function(msg) { - if (shepherd.appConfig.dev || - shepherd.appConfig.debug) { - console.log(msg); - } - - appRuntimeLog.push({ - time: Date.now(), - msg: msg, - }); -}; - shepherd.startSPV = function(coin) { if (coin === 'KMD+REVS+JUMBLR') { shepherd.addElectrumCoin('KMD'); @@ -2828,27 +2962,6 @@ shepherd.readVersionFile = function() { return localVersionFile; } -shepherd.writeLog = function(data) { - const logLocation = `${agamaDir}/shepherd`; - const timeFormatted = new Date(Date.now()).toLocaleString('en-US', { hour12: false }); - - if (shepherd.appConfig.debug) { - if (fs.existsSync(`${logLocation}/agamalog.txt`)) { - fs.appendFile(`${logLocation}/agamalog.txt`, `${timeFormatted} ${data}\r\n`, function(err) { - if (err) { - shepherd.log('error writing log file'); - } - }); - } else { - fs.writeFile(`${logLocation}/agamalog.txt`, `${timeFormatted} ${data}\r\n`, function(err) { - if (err) { - shepherd.log('error writing log file'); - } - }); - } - } -} - shepherd.createAgamaDirs = function() { if (!fs.existsSync(agamaDir)) { fs.mkdirSync(agamaDir); @@ -3867,109 +3980,6 @@ shepherd.post('/appconf/reset', function(req, res, next) { res.end(JSON.stringify(successObj)); }); -shepherd.saveLocalAppConf = function(appSettings) { - let appConfFileName = `${agamaDir}/config.json`; - - _fs.access(agamaDir, fs.constants.R_OK, function(err) { - if (!err) { - - const FixFilePermissions = function() { - return new Promise(function(resolve, reject) { - const result = 'config.json file permissions updated to Read/Write'; - - fsnode.chmodSync(appConfFileName, '0666'); - - setTimeout(function() { - shepherd.log(result); - shepherd.writeLog(result); - resolve(result); - }, 1000); - }); - } - - const FsWrite = function() { - return new Promise(function(resolve, reject) { - const 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(/{/g, '{\n') - .replace(/}/g, '\n}'), 'utf8', function(err) { - if (err) - return shepherd.log(err); - }); - - fsnode.chmodSync(appConfFileName, '0666'); - setTimeout(function() { - shepherd.log(result); - shepherd.log(`app conf.json file is created successfully at: ${agamaDir}`); - shepherd.writeLog(`app conf.json file is created successfully at: ${agamaDir}`); - resolve(result); - }, 2000); - }); - } - - FsWrite() - .then(FixFilePermissions()); - } - }); -} - -shepherd.loadLocalConfig = function() { - if (fs.existsSync(`${agamaDir}/config.json`)) { - let localAppConfig = fs.readFileSync(`${agamaDir}/config.json`, 'utf8'); - - shepherd.log('app config set from local file'); - shepherd.writeLog('app config set from local file'); - - // find diff between local and hardcoded configs - // append diff to local config - const compareJSON = function(obj1, obj2) { - let result = {}; - - for (let i in obj1) { - if (!obj2.hasOwnProperty(i)) { - result[i] = obj1[i]; - } - } - - return result; - }; - - if (localAppConfig) { - const compareConfigs = compareJSON(shepherd.appConfig, JSON.parse(localAppConfig)); - - if (Object.keys(compareConfigs).length) { - const newConfig = Object.assign(JSON.parse(localAppConfig), compareConfigs); - - shepherd.log('config diff is found, updating local config'); - shepherd.log('config diff:'); - shepherd.log(compareConfigs); - shepherd.writeLog('aconfig diff is found, updating local config'); - shepherd.writeLog('config diff:'); - shepherd.writeLog(compareConfigs); - - shepherd.saveLocalAppConf(newConfig); - return newConfig; - } else { - return JSON.parse(localAppConfig); - } - } else { - return shepherd.appConfig; - } - } else { - shepherd.log('local config file is not found!'); - shepherd.writeLog('local config file is not found!'); - shepherd.saveLocalAppConf(shepherd.appConfig); - - return shepherd.appConfig; - } -}; - -shepherd.appConfig = shepherd.loadLocalConfig(); - shepherd.log(`agama dir: ${agamaDir}`); shepherd.log('--------------------------') shepherd.log(`komodo dir: ${komododBin}`); @@ -4770,24 +4780,6 @@ function herder(flock, data, coind) { } shepherd.setConfKMD = function(isChips) { - let komodoDir; - let zcashDir; - - if (os.platform() === 'darwin') { - komodoDir = `${process.env.HOME}/Library/Application Support/Komodo`; - ZcashDir = `${process.env.HOME}/Library/Application Support/Zcash`; - } - - if (os.platform() === 'linux') { - komodoDir = `${process.env.HOME}/.komodo`; - ZcashDir = `${process.env.HOME}/.zcash`; - } - - if (os.platform() === 'win32') { - komodoDir = `${process.env.APPDATA}/Komodo`; - ZcashDir = `${process.env.APPDATA}/Zcash`; - } - // check if kmd conf exists _fs.access(isChips ? `${chipsDir}/chips.conf` : `${komodoDir}/komodo.conf`, fs.constants.R_OK, function(err) { if (err) { @@ -4810,9 +4802,6 @@ shepherd.setConfKMD = function(isChips) { } function setConf(flock, coind) { - let komodoDir; - let chipsDir; - let zcashDir; let nativeCoindDir; let DaemonConfPath; @@ -4820,23 +4809,14 @@ function setConf(flock, coind) { shepherd.writeLog(`setconf ${flock}`); if (os.platform() === 'darwin') { - komodoDir = `${process.env.HOME}/Library/Application Support/Komodo`; - chipsDir = `${process.env.HOME}/Library/Application Support/Chips`; - ZcashDir = `${process.env.HOME}/Library/Application Support/Zcash`; nativeCoindDir = coind ? `${process.env.HOME}/Library/Application Support/${shepherd.nativeCoindList[coind.toLowerCase()].bin}` : null; } if (os.platform() === 'linux') { - komodoDir = `${process.env.HOME}/.komodo`; - chipsDir = `${process.env.HOME}/.chips`; - ZcashDir = `${process.env.HOME}/.zcash`; nativeCoindDir = coind ? `${process.env.HOME}/.${shepherd.nativeCoindList[coind.toLowerCase()].bin.toLowerCase()}` : null; } if (os.platform() === 'win32') { - komodoDir = `${process.env.APPDATA}/Komodo`; - chipsDir = `${process.env.APPDATA}/Chips`; - ZcashDir = `${process.env.APPDATA}/Zcash`; nativeCoindDir = coind ? `${process.env.APPDATA}/${shepherd.nativeCoindList[coind.toLowerCase()].bin}` : null; } @@ -5123,9 +5103,6 @@ function setConf(flock, coind) { } function getConf(flock, coind) { - let komodoDir = ''; - let ZcashDir = ''; - let chipsDir = ''; let DaemonConfPath = ''; let nativeCoindDir; @@ -5138,23 +5115,14 @@ function getConf(flock, coind) { shepherd.writeLog(`getconf flock: ${flock}`); if (os.platform() === 'darwin') { - komodoDir = `${process.env.HOME}/Library/Application Support/Komodo`; - ZcashDir = `${process.env.HOME}/Library/Application Support/Zcash`; - chipsDir = `${process.env.HOME}/Library/Application Support/Chips`; nativeCoindDir = `${process.env.HOME}/Library/Application Support/${shepherd.nativeCoindList[coind.toLowerCase()].bin}`; } if (os.platform() === 'linux') { - komodoDir = `${process.env.HOME}/.komodo`; - ZcashDir = `${process.env.HOME}/.zcash`; - chipsDir = `${process.env.HOME}/.chips`; nativeCoindDir = coind ? `${process.env.HOME}/.${shepherd.nativeCoindList[coind.toLowerCase()].bin.toLowerCase()}` : null; } if (os.platform() === 'win32') { - komodoDir = `${process.env.APPDATA}/Komodo`; - ZcashDir = `${process.env.APPDATA}/Zcash`; - chipsDir = `${process.env.APPDATA}/Chips`; nativeCoindDir = coind ? `${process.env.APPDATA}/${shepherd.nativeCoindList[coind.toLowerCase()].bin}` : null; }