adon-config
Advanced tools
Comparing version 1.2.2 to 1.2.3
114
index.js
"use strict"; | ||
const _ = require('lodash'); | ||
const loaded_paths = []; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
let config = {}; | ||
let fs = require('fs'), | ||
path = require('path'), | ||
_ = require('lodash'), | ||
coffee_script = require('coffee-script'), | ||
self, | ||
singleton; | ||
function loadConfigFile(target_path, config_path) { | ||
function loadConfigFile(file_path, type) { | ||
try { | ||
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); | ||
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); | ||
} | ||
@@ -40,41 +24,47 @@ } | ||
function getFileType(file_path) { | ||
const filename_breakdown = file_path.split('.'); | ||
return filename_breakdown[filename_breakdown.length - 1]; | ||
} | ||
class ConfigManager { | ||
constructor() { | ||
self = this; | ||
self.config = {}; | ||
} | ||
function loadJsonFile(file_path) { | ||
return JSON.parse(fs.readFileSync(file_path, 'utf8')); | ||
} | ||
get (path) { | ||
return _.get(self.config, path, undefined); | ||
} | ||
function importJavascriptFile(file_path) { | ||
return require(path.resolve(file_path)); | ||
} | ||
has (path) { | ||
return _.has(self.config, path); | ||
} | ||
module.exports = { | ||
get: () => { | ||
return config; | ||
}, | ||
load: () => { | ||
let config_path = (path.resolve('.') + '/config') | ||
config_path = path.resolve(config_path); //correct it based on OS | ||
/** | ||
* 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(); | ||
if (config && loaded_paths.indexOf(config_path) >= 0) { | ||
return config; | ||
loadConfigFile(default_path + '.json'); | ||
loadConfigFile(default_path + '.js'); | ||
loadConfigFile(environment_path + '.json'); | ||
loadConfigFile(environment_path + '.js'); | ||
} | ||
} | ||
} catch (err) { | ||
//do nothing | ||
} | ||
} | ||
} | ||
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.2", | ||
"version": "1.2.3", | ||
"description": "This module is used for managing system configurations in cascading order.", | ||
@@ -20,2 +20,3 @@ "keywords": [ | ||
"dependencies": { | ||
"coffee-script": "^1.10.0", | ||
"lodash": "^4.11.1" | ||
@@ -22,0 +23,0 @@ }, |
# adon-config | ||
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. | ||
This module is used for managing system configurations in cascading order. | ||
assuming you have a javascript config file with the following item in config/common | ||
assuming you have a javascript config file with the following item | ||
```js | ||
@@ -16,3 +16,3 @@ modules.export = { | ||
```js | ||
let config = require('adon-config').load(); //this should be one of the first line of code in your index file | ||
let config = require('adon-config'); | ||
@@ -25,6 +25,7 @@ config.load(__dirname); | ||
* Allows developers to manage configuration files in a common structure | ||
* Support a mix of common config file types (Javascript, JSON) | ||
* Support a mix of common config file types (Javascript, JSON, Coffeescript) | ||
* 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] | ||
@@ -38,5 +39,6 @@ ## Installation | ||
## Setup | ||
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) | ||
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 | ||
@@ -46,34 +48,9 @@ ```bash | ||
|-config | ||
| |-common | ||
| | |-server.js | ||
| | |-database.js | ||
| |-env | ||
| | |-development | ||
| | | |-server.js | ||
| | | |-test.js | ||
| | |-production | ||
| | | |-server.js | ||
| | |-development.js | ||
| | |-production.js | ||
| |-default.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 | ||
@@ -80,0 +57,0 @@ adon-config relies on the following node modules |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39898
2
60
62
+ Addedcoffee-script@^1.10.0
+ Addedcoffee-script@1.12.7(transitive)