mirror of
https://github.com/harish2704/tconfig.git
synced 2026-09-10 15:11:08 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38fd5d04aa | ||
|
|
c8aae33840 |
@@ -38,37 +38,42 @@ hari@hari-VirtualBox:~app$ node xyz.js
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Override using NODE_ENV
|
#### Read additional config based on NODE_ENV variable.
|
||||||
values from config/production overides the default one
|
If we specify NODE_ENV=xyz then, config/xyz.js will overide the values from default.js. **Overriding is a deep merge**
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
hari@hari-VirtualBox:~app$ env NODE_ENV=production node xyz.js
|
hari@hari-VirtualBox:~app$ env NODE_ENV=production node xyz.js
|
||||||
{ port: 5000 }
|
{ port: 4000 }
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Override using Environtment Variables.
|
#### Set any configuration variable using Environtment variable.
|
||||||
|
By setting an Environtment variable '<Prefix><key>=<value>', we can set config[key] as value.
|
||||||
Default prefix is `TC_`
|
Default prefix is `TC_`
|
||||||
|
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
|
|
||||||
```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
|
||||||
{ port: 5000, db: { mysql: { user: 'root' } } }
|
{ port: 5000, db: { mysql: { user: 'root' } } }
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Override using Environtment Variables. Custom Prefix
|
##### Use custom env prefix.
|
||||||
Prefix can be changed using Environtment variable
|
|
||||||
|
default prefix can be changed using Environtment variable by setting `TC_PREFIX` Environtment variable
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export TC_PREFIX=MYAPP_
|
export TC_PREFIX=MYAPP_
|
||||||
export MYAPP_PORT=8080
|
export MYAPP_port=8080
|
||||||
hari@hari-VirtualBox:~app$ env MYAPP_db.mysql.user=root node xyz.js
|
hari@hari-VirtualBox:~app$ env MYAPP_db.mysql.user=root node xyz.js
|
||||||
{ port: 8080, db: { mysql: { user: 'root' } } }
|
{ port: 8080, db: { mysql: { user: 'root' } } }
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### use custom config directory
|
#### use custom config directory
|
||||||
config directory can be changed using Environtment variable
|
config directory can be changed using Environtment variable by setting `CONFIG_DIR` Environtment variable
|
||||||
```bash
|
```bash
|
||||||
hari@hari-VirtualBox:~app$ env CONFIG_DIR=../config TC_PREFIX=MYAPP_ NODE_ENV=production MYAPP_db.mysql.user=root node xyz.js
|
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' } } }
|
{ dir: '../config', db: { mysql: { user: 'root' } } }
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
{
|
{
|
||||||
"port":3000
|
"port":3000,
|
||||||
|
"pool":{
|
||||||
|
"min":2,
|
||||||
|
"max":10
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
|
|
||||||
console.log( require('tconfig'));
|
console.log( require('../../'));
|
||||||
|
|||||||
@@ -40,6 +40,25 @@ function loadConfig( name ){
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function assignDeep( src, dest1, dest2 ){
|
||||||
|
var key, val;
|
||||||
|
for( key in dest1 ){
|
||||||
|
val = dest1[ key ];
|
||||||
|
if( val.constructor.name === 'Object' ){
|
||||||
|
if( ( !src.hasOwnProperty(key) ) || ( src[ key ].constructor.name !== 'Object' ) ){
|
||||||
|
src[ key ] = {};
|
||||||
|
}
|
||||||
|
assignDeep( src[ key ], val );
|
||||||
|
} else {
|
||||||
|
src[ key ] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( dest2 ){
|
||||||
|
assignDeep( src, dest2 );
|
||||||
|
}
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
/* Implementation of lodash.set function */
|
/* Implementation of lodash.set function */
|
||||||
function setProp( object, keys, val ){
|
function setProp( object, keys, val ){
|
||||||
keys = Array.isArray( keys )? keys : keys.split('.');
|
keys = Array.isArray( keys )? keys : keys.split('.');
|
||||||
@@ -50,12 +69,12 @@ function setProp( object, keys, val ){
|
|||||||
object[keys[0]] = val;
|
object[keys[0]] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign( finalConfig, loadConfig('default'), loadConfig( env ) );
|
assignDeep( finalConfig, loadConfig('default'), loadConfig( env ) );
|
||||||
|
|
||||||
var envList = Object.keys( process.env ).filter( function(v){
|
Object.keys( process.env ).filter( function(v){
|
||||||
var match = v.match( envRegex );
|
var match = v.match( envRegex );
|
||||||
if( match ){
|
if( match ){
|
||||||
setProp( finalConfig, match[1].toLowerCase(), process.env[v] );
|
setProp( finalConfig, match[1], process.env[v] );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tconfig",
|
"name": "tconfig",
|
||||||
"version": "0.1.1",
|
"version": "0.1.2",
|
||||||
"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": {
|
||||||
@@ -20,5 +20,8 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/harish2704/tconfig/issues"
|
"url": "https://github.com/harish2704/tconfig/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/harish2704/tconfig#readme"
|
"homepage": "https://github.com/harish2704/tconfig#readme",
|
||||||
|
"dependencies": {
|
||||||
|
"lodash.defaultsdeep": "^4.6.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user