diff --git a/freechat-client/pages/index.html b/freechat-client/pages/index.html index 0a5bb66..03acba9 100644 --- a/freechat-client/pages/index.html +++ b/freechat-client/pages/index.html @@ -129,10 +129,9 @@ $('#add-admin-json').click(async () => await add_server_to_config() ) try { - let config = await ipcRenderer.invoke('config-request') - await auth.init(config) + let config = await auth.init() - for(const server_cfg of config['servers']) { + for(const server_cfg of config.data['servers']) { const remote = server_cfg['server'] const user = server_cfg['user'] diff --git a/freechat-client/src/config.js b/freechat-client/src/config.js index 15f89a8..b337995 100644 --- a/freechat-client/src/config.js +++ b/freechat-client/src/config.js @@ -1,37 +1,51 @@ -const { ipcRenderer } = require('electron') const { ArgumentParser } = require('argparse') -const fs = require('fs') +const fs = require('fs').promises +const syncFs = require('fs') + +class Config { + /** + * + * @param {String} path path to config on startup + * @param {Object} data Contains actual config data + */ + constructor(path, data) { + this.path = path + this.data = data + } + + // TODO: this function basically + async write_disk() { + const str = JSON.stringify(this.data, null, 4) + await fs.writeFile(this.path, str) + } +} +exports.Config = Config /** * Gets the config data off disk - * Because we're calling this from node's process we can actuall us the 'fs' module here * @param {ArgumentParset} parser - * @returns {Config} { path } + * @returns {Config} */ -exports.get_config = function(parser) { +exports.from_cli_parser = function(parser) { - const a = parser.parse_args() - const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json` + const args = parser.parse_args() + const defaultpath = `${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 - let ret = { path: path } + // We'll just assume that the config is there as per the installation process + const path = (args['config']) ? args['config'] : defaultpath + let ret = new Config(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] - } + // Allow errors from here to bubble up instead of handling them + + fs.readFile(path).then(file => { + const json = JSON.parse(file) + + for(const key of Object.keys(json)) { + ret.data[key] = json[key] } }) + .catch(err => console.error(err)) - // now we'll just blindly construct the config object from here on out return ret } @@ -57,15 +71,4 @@ exports.update_file = function(config, path='default') { }) } -/** - * Loads the parse with the various arguments that the program takes - * @param {ArgumentParser} parser - */ -exports.load_parser = function() { - const parser = new ArgumentParser({ - description: 'Freechat Electron Client' - }) - parser.add_argument('-c', '--config', {help: 'Specify config path'}) - return parser -} diff --git a/freechat-client/src/settings.js b/freechat-client/src/settings.js index 50184ac..d0cb46d 100644 --- a/freechat-client/src/settings.js +++ b/freechat-client/src/settings.js @@ -6,13 +6,13 @@ exports.add_server_to_config = async function() { $('#admin-json-err').text('') try { const data = JSON.parse($('#admin-json').val()) - const config = await ipcRenderer.invoke('config-request') + let config = await ipcRenderer.invoke('config-request') if(!config['servers']) { config['servers'] = [] } config['servers'].push(data) - await ipcRenderer.invoke('config-update', config, config['path']) + await ipcRenderer.invoke('config-update', config) try { await auth.login(