adon-config
Advanced tools
Comparing version 1.2.3 to 2.0.0
114
index.js
"use strict"; | ||
const _ = require('lodash'); | ||
const loaded_paths = []; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
let fs = require('fs'), | ||
path = require('path'), | ||
_ = require('lodash'), | ||
coffee_script = require('coffee-script'), | ||
self, | ||
singleton; | ||
let config = {}; | ||
function loadConfigFile(file_path, type) { | ||
function loadConfigFile(target_path, config_path) { | ||
try { | ||
if (fs.statSync(file_path).isFile()) { | ||
if (type === 'json') { | ||
self.config = _.defaultsDeep(JSON.parse(fs.readFileSync(file_path, 'utf8')), self.config); | ||
} else { | ||
self.config = _.defaultsDeep(require(path.resolve(file_path)), self.config); | ||
let result = fs.readdirSync(target_path); | ||
for (let i = 0; i < result.length; i++) { | ||
let filename = path.resolve(target_path + '/' + result[i]); | ||
if (fs.statSync(filename).isDirectory()) { | ||
let new_config_path = config_path ? config_path + '.' + result[i] : result[i]; | ||
loadConfigFile(filename, new_config_path); | ||
} else if (fs.statSync(filename).isFile()) { | ||
let new_config = {}; | ||
let target_config_path = ''; | ||
if (config_path) { | ||
target_config_path = config_path + '.' + result[i].split('.')[0] | ||
} else { | ||
target_config_path = result[i].split('.')[0]; | ||
} | ||
if (getFileType(filename) === 'json') { | ||
_.set(new_config, target_config_path, loadJsonFile(filename)); | ||
} else { | ||
_.set(new_config, target_config_path, importJavascriptFile(filename)); | ||
} | ||
config = _.defaultsDeep(new_config, config); | ||
} | ||
@@ -24,47 +40,41 @@ } | ||
class ConfigManager { | ||
constructor() { | ||
self = this; | ||
self.config = {}; | ||
} | ||
function getFileType(file_path) { | ||
const filename_breakdown = file_path.split('.'); | ||
return filename_breakdown[filename_breakdown.length - 1]; | ||
} | ||
get (path) { | ||
return _.get(self.config, path, undefined); | ||
} | ||
function loadJsonFile(file_path) { | ||
return JSON.parse(fs.readFileSync(file_path, 'utf8')); | ||
} | ||
has (path) { | ||
return _.has(self.config, path); | ||
} | ||
function importJavascriptFile(file_path) { | ||
return require(path.resolve(file_path)); | ||
} | ||
/** | ||
* Loads a configuration from an object or a configuration directory | ||
* @param source - the object instance of a configuration or the directory which follows the standard configuration structure | ||
*/ | ||
load (source) { | ||
try { | ||
if (typeof source === 'object') { | ||
self.config = source; | ||
} else { | ||
let config_path = path.resolve(source + '/config'); | ||
if (fs.statSync(config_path).isDirectory()) { | ||
let base_path = path.isAbsolute(config_path) ? config_path + '/' : config_path, | ||
default_path = base_path + 'default', | ||
environment = process.env.NODE_ENV || 'development', | ||
environment_path = base_path + 'env/' + environment.toLowerCase(); | ||
module.exports = { | ||
get: () => { | ||
return config; | ||
}, | ||
load: () => { | ||
let config_path = (path.resolve('.') + '/config') | ||
config_path = path.resolve(config_path); //correct it based on OS | ||
loadConfigFile(default_path + '.json'); | ||
loadConfigFile(default_path + '.js'); | ||
loadConfigFile(environment_path + '.json'); | ||
loadConfigFile(environment_path + '.js'); | ||
} | ||
} | ||
} catch (err) { | ||
//do nothing | ||
if (config && loaded_paths.indexOf(config_path) >= 0) { | ||
return config; | ||
} | ||
console.log('ADON-CONFIG: Loading ' + config_path); | ||
if (fs.statSync(config_path).isDirectory()) { | ||
const default_path = path.resolve(config_path + '/common'); | ||
const environment = process.env.NODE_ENV || 'development'; | ||
const environment_path = path.resolve(config_path + '/env/' + environment.toLowerCase()); | ||
loadConfigFile(default_path); | ||
loadConfigFile(environment_path); | ||
return config; | ||
} else { | ||
throw new Error('ADONCONFIG_CONFIG_ROOTFOLDER_NOT_FOUND'); | ||
} | ||
} | ||
} | ||
module.exports = singleton ? singleton : singleton = new ConfigManager(); | ||
//codes below are for test purposes only | ||
//module.exports.load(__dirname); | ||
//console.dir(self.config); | ||
} |
{ | ||
"name": "adon-config", | ||
"version": "1.2.3", | ||
"version": "2.0.0", | ||
"description": "This module is used for managing system configurations in cascading order.", | ||
@@ -20,3 +20,2 @@ "keywords": [ | ||
"dependencies": { | ||
"coffee-script": "^1.10.0", | ||
"lodash": "^4.11.1" | ||
@@ -23,0 +22,0 @@ }, |
# adon-config | ||
This module is used for managing system configurations in cascading order. | ||
This module is used for managing system configurations in cascading order. It is able to load recursively load all the files in the folder and form the exact structure in the config object. | ||
assuming you have a javascript config file with the following item | ||
assuming you have a javascript config file with the following item in config/common | ||
```js | ||
@@ -16,3 +16,3 @@ modules.export = { | ||
```js | ||
let config = require('adon-config'); | ||
let config = require('adon-config').load(); //this should be one of the first line of code in your index file | ||
@@ -25,7 +25,6 @@ config.load(__dirname); | ||
* Allows developers to manage configuration files in a common structure | ||
* Support a mix of common config file types (Javascript, JSON, Coffeescript) | ||
* Support a mix of common config file types (Javascript, JSON) | ||
* Allows overriding of configurations data on different module levels | ||
* Allows special configurations based on server environment (NODE_ENV) | ||
[NOTE: coffee script is not yet supported] | ||
@@ -39,6 +38,5 @@ ## Installation | ||
## Setup | ||
1. In your node module, create a "config" folder where you want all the config files to exist. | ||
2. create an "env" folder within it that will contain environment specific configurations. | ||
3. inside the config folder create a file named default (javascript, json or coffeescript) | ||
4. (optional) in the "env" folder create configration overrides that you want based on the NODE_ENV value | ||
1. In your node project, create a "config/common" folder where you want all the config files to exist. | ||
2. (optional) create an "config/env" folder within as well to contain environment specific configurations. | ||
3. inside these config folder create any files (javascript or json) | ||
@@ -48,9 +46,34 @@ ```bash | ||
|-config | ||
| |-common | ||
| | |-server.js | ||
| | |-database.js | ||
| |-env | ||
| | |-development.js | ||
| | |-production.js | ||
| |-default.js | ||
| | |-development | ||
| | | |-server.js | ||
| | | |-test.js | ||
| | |-production | ||
| | | |-server.js | ||
|-index.js | ||
``` | ||
in the sample above, we have 2 configurations that will be common across all environments. The config object will contain the attributes or elements named 'server' and 'database' based on the config files. | ||
```$js | ||
let config = require('adon-config').get() | ||
console.log(config.database.source); //outputs the source value from common/database | ||
``` | ||
The env (environment) overrides common configurations and will rely on the systm's NODE_ENV value. in the sample above, any duplicate values in config.server (based from common/server.js) will be overridden by the environment version. any unique configuration that exists in an environment only exists for that environment (i.e. development/test.js) | ||
## Functions | ||
###Load | ||
loads the config folder from the current project directory then returns the latest config object (this is the first code you must trigger to initialize this module) | ||
```$js | ||
let config = require('adon-config').load() | ||
``` | ||
just returns the latest config object and do nothing else (used for succeeding requests where loading of configurations are no longer needed) | ||
```$js | ||
let config = require('adon-config').get() | ||
``` | ||
## Dependencies | ||
@@ -57,0 +80,0 @@ adon-config relies on the following node modules |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
41503
1
66
85
4
- Removedcoffee-script@^1.10.0
- Removedcoffee-script@1.12.7(transitive)