-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit for issue #21 - add basic framework into the library. Se…
…e below for further details. This has been added into the library, but is unreferenced within the main code. The following changes have been made: + library/config/config.js: This is a new API for accessing configurations. Regardless of storage medium, having a wrapper for the configurations means that we can switch between a file-based system, or a database system, and only the functions need to be modified. The API currently consists of the following exposed functions: + **appPath()** - returns the application absolute path. + **absolutePath(relativePath)** - joins the given path with the application path to create an absolute path/file + **userConfigFile(gid)** - returns an absolute path for a given guild config file. No filesystem checks are made. + **serializeConfigObjToString(configString)** - wrapper only, no implementation. + **parseConfigStringToObj(configObj)** - wrapper only, no implementation. + **exportConfig(config)** - wrapper only, no implementation. *This may change in future, proposing writeConfig.* + **importConfig(config)** - wrapper only, no implementation. *This may change, proposing readConfig.* library/index.js: + Added the new config library to expose the API functions. config.json: + Added three new fields to store the path and filename prefix of the user-defined config files (should a file-based system be decided) - currently the global config is stored as a json within the filesystem, so development will assume that the user-defined config files will also be stored in the filesystem. package.json & package-lock.json: + Incremented the version number. New API module for accessing config files. (cherry picked from commit 22f3721)
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* @Author: BanderDragon | ||
* @Date: 2020-08-25 21:10:12 | ||
* @Last Modified by: BanderDragon | ||
* @Last Modified time: 2020-08-26 00:01:53 | ||
*/ | ||
|
||
/* | ||
* library/config/config.js | ||
* ======================== | ||
* A library of functions related to the configuration of the bot. | ||
* | ||
*/ | ||
|
||
const logger = require('winston'); | ||
const config = require('../../config.json'); | ||
|
||
var fs = require('fs'); | ||
var userConfigPath = config.userConfigurationPath; | ||
const userConfigExt = config.userConfigurationExt; | ||
|
||
var path = require('path'); | ||
|
||
module.exports = { | ||
|
||
/** | ||
* Returns the applications absolute path | ||
*/ | ||
appPath: function() { | ||
return path.dirname(require.main.filename); | ||
}, | ||
|
||
/** | ||
* returns the full path, prefixed with the application root directory | ||
* @param {string} relativePath | ||
*/ | ||
absolutePath: function(relativePath) { | ||
// Check if a ~ has been specified (this is not required, but clearly signifies the path is relative) | ||
var firstChar = relativePath.charAt(0); | ||
if(firstChar == "~") { | ||
// Remove the ~ | ||
relativePath = relativePath.substring(1); | ||
} | ||
// Normalise the path, so that we can run on any OS | ||
relativePath = path.normalize(relativePath); | ||
|
||
// Return the full path, prefixed with the application root directory | ||
return path.join(this.appPath(), relativePath); | ||
}, | ||
|
||
/** | ||
* Returns the full path and filename of the user configuration file for the given guild. | ||
* - The file is not created, so it may not yet exist. | ||
* @param {string} gid | ||
*/ | ||
userConfigFile: function(gid) { | ||
var fullPath = ""; | ||
if(gid) { | ||
var filename = config.userConfigurationFilePrefix + gid + config.userConfigurationExtension; | ||
var relativePathAndFile = path.join(config.userConfigurationPath, filename); | ||
fullPath = this.absolutePath(relativePathAndFile); | ||
} else { | ||
throw "userConfigFile: A null guild Id was passed." | ||
} | ||
|
||
return fullPath; | ||
}, | ||
|
||
/* | ||
* The following are wrappers for storing and loading the | ||
* user-defined config files - the user-defined config files allow | ||
* configuration to be customised on a per-guild basis. | ||
* | ||
* These functions wrap the config save/load functionality so that | ||
* the storage medium can be changed at a later date without requiring | ||
* massive code changes. If the API remains static, we can switch from | ||
* a file storage system to a database with relative ease. | ||
*/ | ||
serializeConfigObjToString: function(gid, configString, client) { | ||
|
||
}, | ||
|
||
parseConfigStringToObj: function(gid, configString, client) { | ||
var returnObj; | ||
try { | ||
returnObj = JSON.parse(configString); | ||
} catch (e) { | ||
logger.error(`Unable to parse config string - exception ${e.name}`) | ||
} | ||
}, | ||
|
||
exportConfig: function (client) { | ||
|
||
}, | ||
|
||
importConfig: function (client) { | ||
|
||
}, | ||
|
||
exportGuildConfig: function (gid, client) { | ||
|
||
}, | ||
|
||
importGuildConfig: function (gid, client) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters