Browse Source

* Added verbose messages

* Updated docs
V 0.1.4
master v0.1.4
Harish.K 8 years ago
parent
commit
d7c5fd2b00
  1. 24
      README.md
  2. 6
      index.js
  3. 2
      package.json

24
README.md

@ -48,12 +48,19 @@ hari@hari-VirtualBox:~app$ env NODE_ENV=production node xyz.js
#### Set any configuration variable using Environtment variable. #### Set any configuration variable using Environtment variable.
By setting an Environtment variable '<Prefix><key>=<value>', we can set config[key] as value. By setting an Environtment variable '<Prefix><key>=<value>', we can set config[key] as value.
Default prefix is `TC_`
** First we will try to parse value as json. It it is failed value will be treated as string **
** If a values if parsable as JSON object, then it will be deep merged with default configuration **
Default prefix is `TC_` ( Can be changed by setting TC_PREFIX` env variable `)
For eg: For eg:
* `TC_port` will set config.port * `TC_port` will set config.port
* `TC_a.b.c` will set config.a.b.c * `TC_a.b.c` will set config.a.b.c
* `TC_a='{"a":{"b":{"c": 123546 }}}'` will also set `config.a.b.c`
* `TC_port=8000` will set `{ port: 8000 }` By default, number will parsed as json number.
* `TC_port='"8000"'` will set `{ port: '8000' }` ( **now port is a string** because double quoted value will be parsed as string in json )
```bash ```bash
hari@hari-VirtualBox:~app$ env NODE_ENV=production TC_db.mysql.user=root node xyz.js hari@hari-VirtualBox:~app$ env NODE_ENV=production TC_db.mysql.user=root node xyz.js
@ -80,6 +87,19 @@ hari@hari-VirtualBox:~app$ env CONFIG_DIR=../config TC_PREFIX=MYAPP_ NODE_ENV=pr
``` ```
#### printing debug messages
set `DEBUG` environment variable to `tconfig`
```bash
export DEBUG=tconfig
# OR
export DEBUG='*'
```
#### Config directory search path
* Directory pointed by `CONFIG_DIR` environment variable
* < directory or main script >/config
* < current working directory >/config

6
index.js

@ -9,6 +9,8 @@ var finalConfig = {};
var configDirLookupPath = [ var configDirLookupPath = [
path.join( process.env.PWD, 'config' ) path.join( process.env.PWD, 'config' )
]; ];
var beVerbose = process.env.DEBUG && 'tconfig'.match( process.env.DEBUG.replace( /\*/g, '.*' ) );
var log = beVerbose ? console.log.bind( console, 'tconfig:: ' ) : function(){};
if( require.main ){ if( require.main ){
configDirLookupPath.unshift( path.resolve( path.join( require.main.paths[0], '..', 'config' ) ) ); configDirLookupPath.unshift( path.resolve( path.join( require.main.paths[0], '..', 'config' ) ) );
@ -21,6 +23,7 @@ for (var i = 0, l = configDirLookupPath.length; i < l; i ++) {
var v = configDirLookupPath[i]; var v = configDirLookupPath[i];
if( fs.existsSync(v) ){ if( fs.existsSync(v) ){
configDir = v; configDir = v;
log( 'Found config dir: ' + configDir );
break; break;
} }
} }
@ -42,10 +45,12 @@ function processSpecial( str ){
} }
function loadConfig( name ){ function loadConfig( name ){
log('Loading config ' + name );
var out = {}; var out = {};
try{ try{
out = require( configDir + '/' + name ); out = require( configDir + '/' + name );
} catch(e){ } catch(e){
log( 'Error loading config ' + name, e );
out = {}; out = {};
} }
return out; return out;
@ -92,5 +97,6 @@ Object.keys( process.env ).filter( function(v){
} }
}); });
log('Final config ', JSON.stringify(finalConfig, null, 2 ) );
module.exports = finalConfig; module.exports = finalConfig;

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "tconfig", "name": "tconfig",
"version": "0.1.3",
"version": "0.1.4",
"description": "A simple transparent config file loader for Nodejs applications. any config field can be overriden using environmen variables", "description": "A simple transparent config file loader for Nodejs applications. any config field can be overriden using environmen variables",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

Loading…
Cancel
Save