From 60aee1dc07eb1fb2bbfba5e42d783881b17e1a52 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 6 Jan 2021 15:22:27 -0800 Subject: [PATCH] Loading configs asynchronously now as well as with better error logging for the user --- freechat-client/make | 2 +- freechat-client/src/config.js | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/freechat-client/make b/freechat-client/make index a690299..d1355ca 100755 --- a/freechat-client/make +++ b/freechat-client/make @@ -3,7 +3,7 @@ # Script to help build and run this fucking thing because apparantly npm sucks for # projects that use cli arguments -[[ -z "$1" ]] && echo Options: run - make && exit 0 +[[ -z "$1" ]] && echo Options: run \| build && exit 0 run() { ./node_modules/electron/dist/electron main.js $@ diff --git a/freechat-client/src/config.js b/freechat-client/src/config.js index 776be17..67974a6 100644 --- a/freechat-client/src/config.js +++ b/freechat-client/src/config.js @@ -6,22 +6,29 @@ const fs = require('fs') * @returns {Config} { path } */ function get_config(parser) { + const a = parser.parse_args() const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json` // we'll just assume that the config is there as per the installation process const path = (a['config']) ? a['config'] : defaultcfg - const file_contents = fs.readFileSync(path) - const file_json = JSON.parse(file_contents) + let ret = { path: path } + + fs.readFile(path, (err, data) => { + if(err) { + console.log('No valid config found') + process.exit() + } + const file_json = JSON.parse(data) + for(const key of Object.keys(file_json)) { + if(key == 'path') { continue } // don't overwrite the old path + else { + ret[key] = file_json[key] + } + } + }) // now we'll just blindly construct the config object from here on out - let ret = { path: path } - for(const key of Object.keys(file_json)) { - if(key == 'path') { continue } // don't overwrite the old path - else { - ret[key] = file_json[key] - } - } return ret }