diff options
Diffstat (limited to '')
-rw-r--r-- | server/src/managers/config.js | 240 | ||||
-rw-r--r-- | server/src/managers/index.js | 6 | ||||
-rw-r--r-- | server/src/serverLib/CommandManager.js (renamed from server/src/managers/commands.js) | 5 | ||||
-rw-r--r-- | server/src/serverLib/ImportsManager.js (renamed from server/src/managers/imports-manager.js) | 29 | ||||
-rw-r--r-- | server/src/serverLib/StatsManager.js (renamed from server/src/managers/stats.js) | 10 |
5 files changed, 8 insertions, 282 deletions
diff --git a/server/src/managers/config.js b/server/src/managers/config.js deleted file mode 100644 index 7b49aa8..0000000 --- a/server/src/managers/config.js +++ /dev/null @@ -1,240 +0,0 @@ -/** - * Server configuration manager, handling loading, creation, parsing and saving - * of the main config.json file - * - * Version: v2.0.0 - * Developer: Marzavec ( https://github.com/marzavec ) - * License: WTFPL ( http://www.wtfpl.net/txt/copying/ ) - * - */ - -const stripIndents = require('common-tags').stripIndents; -const dateFormat = require('dateformat'); -const chalk = require('chalk'); -const fse = require('fs-extra'); -const prompt = require('prompt'); -const path = require('path'); -const deSync = require('deasync'); - -// For hashing the admin's password into a trip. -const crypto = require('crypto'); - -const hash = (password) => { - let sha = crypto.createHash('sha256'); - sha.update(password); - return sha.digest('base64').substr(0, 6); -}; - -class ConfigManager { - /** - * Create a `ConfigManager` instance for (re)loading classes and config - * - * @param {Object} core reference to the global core object - * @param {String} base executing directory name; __dirname - * @param {Object} dynamicImports dynamic import engine reference - */ - constructor (core, base, dynamicImports) { - this._core = core; - this._base = base; - - this._configPath = path.resolve(base, 'config/config.json'); - - this._dynamicImports = dynamicImports; - } - - /** - * Pulls both core config questions along with any optional config questions, - * used in building the initial config.json or re-building it. - * - * @param {Object} currentConfig an object containing current server settings, if any - * @param {Object} optionalConfigs optional (non-core) module config - */ - getQuestions (currentConfig, optionalConfigs) { - let salt = null; // this is so it can be accessed from adminTrip. - - // core server setup questions - const questions = { - properties: { - tripSalt: { - type: 'string', - required: !currentConfig.tripSalt, - default: currentConfig.tripSalt, - hidden: true, - replace: '*', - before: value => { - salt = value; - return salt; - } - }, - adminName: { - pattern: /^"?[a-zA-Z0-9_]+"?$/, - type: 'string', - message: 'Nicks can only contain letters, numbers and underscores', - required: !currentConfig.adminName, - default: currentConfig.adminName, - before: value => value.replace(/"/g, '') - }, - adminTrip: { - type: 'string', - required: !currentConfig.adminTrip, - default: currentConfig.adminTrip, - hidden: true, - replace: '*', - description: 'adminPass', - before: value => hash(value + salt) - }, - websocketPort: { - type: 'number', - required: !currentConfig.websocketPort, - default: currentConfig.websocketPort || 6060 - } - } - }; - - // non-core server setup questions, for future plugin support - Object.keys(optionalConfigs).forEach(configName => { - const config = optionalConfigs[configName]; - const question = config.getQuestion(currentConfig, configName); - - if (!question) { - return; - } - - question.description = (question.description || configName) + ' (Optional)'; - questions.properties[configName] = question; - }); - - return questions; - } - - /** - * `load` function overload, only blocking - * - */ - loadSync () { - let conf = {}; - conf = this.load(); - - // websocketport is the last core config question, wait until it's been populated - while(conf === null || typeof conf.websocketPort === 'undefined') { - deSync.sleep(100); - } - - return conf; - } - - /** - * (Re)builds the config.json (main server config), or loads the config into mem - * if rebuilding, process will exit- this is to allow a process manager to take over - * - * @param {Boolean} reconfiguring set to true by `scripts/configure.js`, will exit if true - */ - load (reconfiguring = false) { - if (reconfiguring || !fse.existsSync(this._configPath)) { - // gotta have that sexy console - console.log(stripIndents` - ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')} - ${chalk.gray('--------------(') + chalk.white(' HackChat Setup Wizard v1.0 ') + chalk.gray(')--------------')} - ${chalk.magenta('°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø')} - - For advanced setup, see the documentation at: - ${chalk.green('https://github.com/hack-chat/main/tree/master/documentation')} - - ${chalk.white('Note:')} ${chalk.green('npm/yarn run config')} will re-run this utility. - - You will now be asked for the following: - - ${chalk.magenta(' Salt')}, the salt for username trip - - ${chalk.magenta('Admin Name')}, the initial admin username - - ${chalk.magenta('Admin Pass')}, the initial admin password - - ${chalk.magenta(' Port')}, the port for the websocket - \u200b - `); - - let currentConfig = this._config || {}; - if (reconfiguring && fse.existsSync(this._configPath)) { - this._backup(); - currentConfig = fse.readJSONSync(this._configPath); - } - - prompt.get(this.getQuestions(currentConfig, this._dynamicImports.optionalConfigs), (err, res) => { - if (typeof res.mods === 'undefined') { - res.mods = []; - } - - if (err) { - console.error(err); - process.exit(666); // SPOOKY! - } - - try { - fse.outputJsonSync(this._configPath, res); - } catch (e) { - console.error(`Couldn't write config to ${this._configPath}\n${e.stack}`); - if (!reconfiguring) { - process.exit(666); // SPOOKY! - } - } - - console.log('Config generated! You may now start the server normally.') - - process.exit(reconfiguring ? 0 : 42); - }); - - return null; - } - - this._config = fse.readJSONSync(this._configPath); - - return this._config; - } - - /** - * Creates backup of current config into _configPath - * - */ - _backup () { - const backupPath = `${this._configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`; - fse.copySync(this._configPath, backupPath); - - return backupPath; - } - - /** - * First makes a backup of the current `config.json`, then writes current config - * to disk - * - */ - save () { - const backupPath = this._backup(); - - if (!fse.existsSync(this._configPath)){ - fse.mkdirSync(this._configPath); - } - - try { - fse.writeJSONSync(this._configPath, this._config); - fse.removeSync(backupPath); - - return true; - } catch (err) { - console.log(`Failed to save config file: ${err}`); - - return false; - } - } - - /** - * Updates current config[`key`] with `value` then writes changes to disk - * - * @param {*} key arbitrary configuration key - * @param {*} value new value to change `key` to - */ - set (key, value) { - const realKey = `${key}`; - this._config[realKey] = value; - - this.save(); - } -} - -module.exports = ConfigManager; diff --git a/server/src/managers/index.js b/server/src/managers/index.js deleted file mode 100644 index 2fac8fb..0000000 --- a/server/src/managers/index.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - CommandManager: require('./commands'), - Config: require('./config'), - ImportsManager: require('./imports-manager'), - Stats: require('./stats') -}; diff --git a/server/src/managers/commands.js b/server/src/serverLib/CommandManager.js index 434a16e..71c8884 100644 --- a/server/src/managers/commands.js +++ b/server/src/serverLib/CommandManager.js @@ -8,7 +8,6 @@ */ const path = require('path'); -const chalk = require('chalk'); const didYouMean = require('didyoumean2').default; class CommandManager { @@ -33,7 +32,7 @@ class CommandManager { const core = this.core; - const commandImports = core.managers.dynamicImports.getImport('src/commands'); + const commandImports = core.dynamicImports.getImport('src/commands'); let cmdErrors = ''; Object.keys(commandImports).forEach(file => { let command = commandImports[file]; @@ -61,7 +60,7 @@ class CommandManager { } if (!command.category) { - let base = path.join(this.core.managers.dynamicImports.base, 'commands'); + let base = path.join(this.core.dynamicImports.base, 'commands'); let category = 'Uncategorized'; if (file.indexOf(path.sep) > -1) { diff --git a/server/src/managers/imports-manager.js b/server/src/serverLib/ImportsManager.js index d8b2144..a049d5c 100644 --- a/server/src/managers/imports-manager.js +++ b/server/src/serverLib/ImportsManager.js @@ -14,24 +14,12 @@ class ImportsManager { /** * Create a `ImportsManager` instance for (re)loading classes and config * - * @param {Object} core reference to the global core object * @param {String} base executing directory name; __dirname */ - constructor (core, base) { - this._core = core; + constructor (base) { this._base = base; this._imports = {}; - this._optionalConfigs = {}; - } - - /** - * Pull core reference - * - * @type {Object} readonly - */ - get core () { - return this._core; } /** @@ -44,15 +32,6 @@ class ImportsManager { } /** - * Pull optional (none-core) config options - * - * @type {Object} - */ - get optionalConfigs () { - return Object.assign({}, this._optionalConfigs); - } - - /** * Initialize this class and start loading target directories * */ @@ -89,12 +68,6 @@ class ImportsManager { return errorText; } - if (imported.configs) { - imported.configs.forEach(config => { - this._optionalConfigs[config.name] = config; - }); - } - if (!this._imports[dirName]) { this._imports[dirName] = {}; } diff --git a/server/src/managers/stats.js b/server/src/serverLib/StatsManager.js index 20f1ae3..4ec7ddf 100644 --- a/server/src/managers/stats.js +++ b/server/src/serverLib/StatsManager.js @@ -7,13 +7,13 @@ * */ -class Stats { +class StatsManager { /** * Create a stats instance. * */ constructor () { - this._stats = {}; + this.data = {}; } /** @@ -22,7 +22,7 @@ class Stats { * @param {String} key Reference to the arbitrary store name */ get (key) { - return this._stats[key]; + return this.data[key]; } /** @@ -32,7 +32,7 @@ class Stats { * @param {Number} value New value for `key` */ set (key, value) { - this._stats[key] = value; + this.data[key] = value; } /** @@ -56,4 +56,4 @@ class Stats { } } -module.exports = Stats; +module.exports = StatsManager; |