mirror of https://github.com/harish2704/tconfig
3 changed files with 143 additions and 2 deletions
@ -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' } } } |
|||
|
|||
``` |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
@ -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; |
@ -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 <harish2704@gmail.com>", |
|||
"license": "MIT", |
|||
"bugs": { |
|||
"url": "https://github.com/harish2704/tconfig/issues" |
|||
}, |
|||
"homepage": "https://github.com/harish2704/tconfig#readme" |
|||
} |
Loading…
Reference in new issue