From d7c5fd2b00291c8c7516fbaf3c0db7041fc6c6d0 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sun, 30 Jul 2017 00:45:48 +0530 Subject: [PATCH] * Added verbose messages * Updated docs V 0.1.4 --- README.md | 24 ++++++++++++++++++++++-- index.js | 6 ++++++ package.json | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5cfb409..750524c 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,19 @@ hari@hari-VirtualBox:~app$ env NODE_ENV=production node xyz.js #### Set any configuration variable using Environtment variable. By setting an Environtment variable '=', 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: * `TC_port` will set config.port - * `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 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 + diff --git a/index.js b/index.js index beae74e..1795798 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ var finalConfig = {}; var configDirLookupPath = [ 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 ){ 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]; if( fs.existsSync(v) ){ configDir = v; + log( 'Found config dir: ' + configDir ); break; } } @@ -42,10 +45,12 @@ function processSpecial( str ){ } function loadConfig( name ){ + log('Loading config ' + name ); var out = {}; try{ out = require( configDir + '/' + name ); } catch(e){ + log( 'Error loading config ' + name, e ); 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; diff --git a/package.json b/package.json index 7061655..a2bf9f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "main": "index.js", "scripts": {