mirror of
https://github.com/harish2704/tconfig.git
synced 2026-09-10 15:11:08 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7c5fd2b00 | ||
|
|
4a10b8149b | ||
|
|
58a7593f7e | ||
|
|
23b899df60 |
@@ -48,16 +48,23 @@ 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
|
||||||
{ port: 5000, db: { mysql: { user: 'root' } } }
|
{ port: 4000, db: { mysql: { user: 'root' } } }
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Use custom env prefix.
|
##### Use custom env prefix.
|
||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,14 @@ var envPrefix = process.env.TC_PREFIX || 'TC_';
|
|||||||
var envRegex = new RegExp( '^' + envPrefix + '(.*)' );
|
var envRegex = new RegExp( '^' + envPrefix + '(.*)' );
|
||||||
var finalConfig = {};
|
var finalConfig = {};
|
||||||
var configDirLookupPath = [
|
var configDirLookupPath = [
|
||||||
path.resolve( path.join( require.main.paths[0], '..', 'config' ) ),
|
|
||||||
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 ){
|
||||||
|
configDirLookupPath.unshift( path.resolve( path.join( require.main.paths[0], '..', 'config' ) ) );
|
||||||
|
}
|
||||||
if( process.env.CONFIG_DIR ){
|
if( process.env.CONFIG_DIR ){
|
||||||
configDirLookupPath.unshift( process.env.CONFIG_DIR );
|
configDirLookupPath.unshift( process.env.CONFIG_DIR );
|
||||||
}
|
}
|
||||||
@@ -18,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,13 +34,23 @@ if( !configDir ){
|
|||||||
'\n Please set CONFIG_DIR env variable');
|
'\n Please set CONFIG_DIR env variable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function processSpecial( str ){
|
||||||
|
var out;
|
||||||
|
try {
|
||||||
|
out = JSON.parse(str);
|
||||||
|
} catch (e) {
|
||||||
|
out = str;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -66,6 +82,9 @@ function setProp( object, keys, val ){
|
|||||||
object[keys[0]] = object[keys[0]] || {};
|
object[keys[0]] = object[keys[0]] || {};
|
||||||
return setProp( object[keys[0]], keys.slice(1), val );
|
return setProp( object[keys[0]], keys.slice(1), val );
|
||||||
}
|
}
|
||||||
|
if( val instanceof Object ){
|
||||||
|
return assignDeep( object[keys[0]], val );
|
||||||
|
}
|
||||||
object[keys[0]] = val;
|
object[keys[0]] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,9 +93,10 @@ assignDeep( finalConfig, loadConfig('default'), loadConfig( env ) );
|
|||||||
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], process.env[v] );
|
setProp( finalConfig, match[1], processSpecial( process.env[v] ) );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
log('Final config ', JSON.stringify(finalConfig, null, 2 ) );
|
||||||
|
|
||||||
module.exports = finalConfig;
|
module.exports = finalConfig;
|
||||||
|
|||||||
+1
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tconfig",
|
"name": "tconfig",
|
||||||
"version": "0.1.2",
|
"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": {
|
||||||
@@ -22,6 +22,5 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/harish2704/tconfig#readme",
|
"homepage": "https://github.com/harish2704/tconfig#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lodash.defaultsdeep": "^4.6.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user