diff --git a/src/assets/configHandler.ts b/src/assets/configHandler.ts index 3663f35..4cfb8b0 100644 --- a/src/assets/configHandler.ts +++ b/src/assets/configHandler.ts @@ -1,7 +1,7 @@ import fs from 'node:fs'; import _ from 'lodash'; -export type configObject = Record +export type configObject = Record; /** * This class is responsible to save/edit config files. @@ -13,7 +13,7 @@ export type configObject = Record export default class config { #configPath: string; //global = {[key: string] : string} - global: configObject + global: configObject; /** * Creates an instance of config. @@ -35,7 +35,15 @@ export default class config { // Save config. this.save_config(); } catch (err) { - console.error('Could not read config file at ' + this.#configPath + ' due to: ' + err); + // If file does not exist, create it. + if (err.code === 'ENOENT') { + console.log(`Config file does not exist. Creating it at ${this.#configPath} now.`); + this.save_config(); + return; + } + console.error(`Could not read config file at ${this.#configPath} due to: ${err}`); + // Exit process. + process.exit(1); } } @@ -46,30 +54,28 @@ export default class config { try { fs.writeFileSync(this.#configPath, JSON.stringify(this.global, null, 8)); } catch (err) { - console.error('Could not write config file at ' + this.#configPath + ' due to: ' + err); + console.error(`Could not write config file at ${this.#configPath} due to: ${err}`); return; } - console.log('Successfully written config file to ' + this.#configPath); + console.log(`Successfully written config file to ${this.#configPath}`); } } -// BUG: If file does'nt exist -> fail. -// TODO: Check for SyntaxError on fileread and ask if the user wants to continue -> overwrite everything. This behavior is currently standard. - /* **** Example **** -const default_config = { - token: 'your-token-goes-here', - clientId: '', - devserverID: '', - devmode: true -}; +import configHandler from './assets/configHandler.js'; +// Create a new config instance. +export const config = new ConfigHandler(__path + '/config.json', { + db_connection_string: 'mysql://USER:PASSWORD@HOST:3306/DATABASE', + http_listen_address: '127.0.0.1', + http_port: 3000, + sentry_dsn: 'https://ID@sentry.example.com/PROJECTID', + debug: false +}); -import configHandler from './assets/config.js'; -const config = new configHandler(__path + '/config.json', default_config); console.log('Base Config:'); console.log(config.global);