building a foundation and breaking prisma

Co-authored-by: Spacelord <Spacelord09@users.noreply.github.com>
This commit is contained in:
2023-04-30 22:04:13 +02:00
commit 18a4393765
18 changed files with 2717 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
import fs from 'node:fs';
import _ from 'lodash';
export type configObject = Record<any, any>
/**
* This class is responsible to save/edit config files.
*
* @export
* @class config
* @typedef {config}
*/
export default class config {
#configPath: string;
//global = {[key: string] : string}
global: configObject
/**
* Creates an instance of config.
*
* @constructor
* @param {string} configPath Path to config file.
* @param {object} configPreset Default config object with default values.
*/
constructor(configPath: string, configPreset: object) {
this.#configPath = configPath;
this.global = configPreset;
try {
// Read config
const data = fs.readFileSync(this.#configPath, 'utf8');
// Extend config with missing parameters from configPreset.
this.global = _.defaultsDeep(JSON.parse(data), this.global);
// Save config.
this.save_config();
} catch (err) {
console.error('Could not read config file at ' + this.#configPath + ' due to: ' + err);
}
}
/**
* Saves the jsonified config object to the config file.
*/
save_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);
return;
}
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/config.js';
const config = new configHandler(__path + '/config.json', default_config);
console.log('Base Config:');
console.log(config.global);
console.log('Add some new key to config and call save_config.');
config.global.NewKey = 'ThisIsANewKey!'
config.save_config()
console.log('Complete Config:');
console.log(config.global);
*/