From 06109151feffa4e0103377775c4de5a5b4f773ae Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 7 Dec 2016 14:30:28 +0530 Subject: [PATCH] Initial commit --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- index.js | 39 +++++++++++++++++++++++++ package.json | 24 +++++++++++++++ 3 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md index ea8812a..7bb7b8d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,81 @@ # tconfig -A simple transparent config file loader for Nodejs applications. -any config field can be overriden using environmen variables +A simple and transparent config file loader for Nodejs applications. + + +## install +```bash +npm i tconfig +``` +## Usage +```javascript +const conig = require('tconfig'); + +console.log( config ); // your config object +``` + +## Examples + +```javascript +// file: xyz.js + +/* +▾ app/ + ▾ config/ + default.js // port = 3000 + production.js // port = 4000 + xyz.js + package.json +*/ + + +console.log( require('tconfig') ); +``` + +#### Simple case +```bash +hari@hari-VirtualBox:~app$ node xyz.js +{ port: 3000 } + +``` + +#### Override using NODE_ENV +values from config/production overides the default one + +```bash +hari@hari-VirtualBox:~app$ env NODE_ENV=production node xyz.js +{ port: 5000 } +``` + +#### Override using Environtment Variables. +Default prefix is `TC_` + +TC_port will set config.port + +TC_a.b.c will set config.a.b.c +```bash +hari@hari-VirtualBox:~app$ env NODE_ENV=production TC_db.mysql.user=root node xyz.js +{ port: 5000, db: { mysql: { user: 'root' } } } +``` + +#### Override using Environtment Variables. Custom Prefix +Prefix can be changed using Environtment variable +```bash +export TC_PREFIX=MYAPP_ +export MYAPP_PORT=8080 +hari@hari-VirtualBox:~app$ env MYAPP_db.mysql.user=root node xyz.js +{ port: 8080, db: { mysql: { user: 'root' } } } + +``` + +#### use custom config directory +config directory can be changed using Environtment variable +```bash +hari@hari-VirtualBox:~app$ env CONFIG_DIR=../config TC_PREFIX=MYAPP_ NODE_ENV=production MYAPP_db.mysql.user=root node xyz.js +{ dir: '../config', db: { mysql: { user: 'root' } } } + +``` + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..f6dc17c --- /dev/null +++ b/index.js @@ -0,0 +1,39 @@ + +var path = require('path'); +var configDIR = process.env.CONFIG_DIR || path.resolve( path.join( require.main.paths[0], '..', 'config' ) ); +var env = ( process.env.NODE_ENV || 'development' ).toLowerCase(); +var envPrefix = process.env.TC_PREFIX || 'TC_'; +var envRegex = new RegExp( '^' + envPrefix + '(.*)' ); +var finalConfig = {}; + +function loadConfig( name ){ + var out = {}; + try{ + out = require( configDIR + '/' + name ); + } catch(e){ + out = {}; + } + return out; +} + +/* Implementation of lodash.set function */ +function setProp( object, keys, val ){ + keys = Array.isArray( keys )? keys : keys.split('.'); + if( keys.length>1 ){ + object[keys[0]] = object[keys[0]] || {}; + return setProp( object[keys[0]], keys.slice(1), val ); + } + object[keys[0]] = val; +} + +Object.assign( finalConfig, loadConfig('default'), loadConfig( env ) ); + +var envList = Object.keys( process.env ).filter( function(v){ + var match = v.match( envRegex ); + if( match ){ + setProp( finalConfig, match[1].toLowerCase(), process.env[v] ); + } +}); + + +module.exports = finalConfig; diff --git a/package.json b/package.json new file mode 100644 index 0000000..94ecf41 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "tconfig", + "version": "0.0.1", + "description": "A simple transparent config file loader for Nodejs applications. any config field can be overriden using environmen variables", + "main": "index.js", + "scripts": { + "test": "mocha -R spec" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/harish2704/tconfig.git" + }, + "keywords": [ + "config", + "env", + "override" + ], + "author": "Harish.K ", + "license": "MIT", + "bugs": { + "url": "https://github.com/harish2704/tconfig/issues" + }, + "homepage": "https://github.com/harish2704/tconfig#readme" +}