From 430e10e6b62a87c219b0028ae13df1109f1092f3 Mon Sep 17 00:00:00 2001 From: shockrah Date: Fri, 1 Jan 2021 17:14:00 -0800 Subject: [PATCH] Moved config loading to its own proper module --- freechat-client/js/config.js | 41 ++++++++++++++++++++++++++++++++++++ freechat-client/main.js | 18 ++++++---------- 2 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 freechat-client/js/config.js diff --git a/freechat-client/js/config.js b/freechat-client/js/config.js new file mode 100644 index 0000000..61f8baa --- /dev/null +++ b/freechat-client/js/config.js @@ -0,0 +1,41 @@ +const { ArgumentParser } = require('argparse') +const fs = require('fs') + +/** + * @param {ArgumentParset} parser + * @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) + + // now we'll just blindly construct the config object from here on out + let ret = { path: path } + for(const key in file_json.keys) { + if(key == 'path') { continue } // don't overwrite the old path + else { + ret[key] = file_json[key] + } + } + return ret +} + +/** + * Loads the parse with the various arguments that the program takes + * @param {ArgumentParser} parser + */ +function load_parser() { + const parser = new ArgumentParser({ + description: 'Freechat Electron Client' + }) + parser.add_argument('-c', '--config', {help: 'Specify config path'}) + return parser +} + +exports.load_parser = load_parser +exports.get_config = get_config diff --git a/freechat-client/main.js b/freechat-client/main.js index 4dad965..594596f 100644 --- a/freechat-client/main.js +++ b/freechat-client/main.js @@ -1,20 +1,18 @@ const { ArgumentParser } = require('argparse') const { app, BrowserWindow } = require('electron') const path = require('path') -const fs = require('fs') -let win; -let config; +const cfg = require('./js/config.js') + + +let win +const config = cfg.get_config(cfg.load_parser()) +console.log('Config: ', config) -const parser = new ArgumentParser({ - description: 'Freechat Electron Client' -}) -parser.add_argument('-c', '--config', {help: 'Specify config path'}) -console.log(parser.parse_args()) function createWin() { - let win = new BrowserWindow({ + win = new BrowserWindow({ width: 800, height: 600, minWidth: 640, @@ -35,8 +33,6 @@ function createWin() { }); } -function load_config(config_path) { -} app.on('ready', createWin);