Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

configya

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

configya - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

.npmignore

10

package.json
{
"name": "configya",
"version": "0.0.3",
"description": "Config files that defer to env settings. Weeeeeee.",
"version": "0.0.4",
"description": "Config files that defer to env settings.",
"main": "src/configya.js",

@@ -10,3 +10,3 @@ "scripts": {

"author": "LeanKit",
"license": "MIT",
"license": "MIT License - http://opensource.org/licenses/MIT",
"devDependencies": {

@@ -16,3 +16,7 @@ "gulp": "~3.5.6",

"should": "~3.2.0-beta1"
},
"dependencies": {
"gnosis": "^0.3.0",
"lodash": "^2.4.1"
}
}
# configya
Stupid simple configuration.
##What & How
`configya` reads your environment variables as well as an optional configuration file (you provide the path, in that case), and returns a configuration object to you.
###Environment Variables
`configya` will parse your environment variables into an object hierarchy if you use underscores to delimit them. For example, if you have an environment variable called `RABBIT_BROKER_IP` set to "127.0.0.1", and another one called `RABBIT_BROKER_PORT` (set to 5672), they will be parsed to this representation:
{
rabbit: {
broker: {
ip: "127.0.0.1",
port: "5672"
}
}
}
Notice that the environment variables are transformed to lower case as well.
By default, configya will prefer to use your environment variables. If you provide a config file as well, it will still prefer environment variables unless you add this to your environment variables: `deploy-type=DEV`. With `deploy-type` set to DEV, `configya` will use values from your config file, *if they exist*, before an environment variable.
## Use
## Usage
```javascript
var config = require( 'configya' )( './path/config.json' );
//load configya without a config file (using only environment)
var cfg = require('configya')();
// get the value from the config file,
// if an environment variable is presen
// the environment variable ALWAYS trumps the file setting
config.get( 'key' );
//load configta with a config file as well
var cfg = require('configya')('./path/to/configuration.json');
config.get( 'key', defaultValue );
```
var port = cfg.rabbit.broker.port; // etc.
For the oddball edge case(s), the environment variables are also available on `configya` in an un-transformed state:
// This isn't how you want to get at your config data....
var port = cfg.__env__.RABBIT_BROKER_PORT;
## Backwards Compatibility
The original version of `configya` (v0.0.3) used a `get` method to retrieve configuration values. This is technically still supported, though we recommend using the approach described above. Here's a usage example based on the older API:
var config = require( 'configya' )( './path/config.json' );
// get the value from the config file, if an
// environment variable is present the environment
// variable ALWAYS trumps the file setting unless
// you have deploy-type=DEV in your env settings
config.get( 'key' );
config.get( 'key', defaultValue );
require( 'should' );
describe( 'when loading config that doesn\'t exist', function() {
var cfg = require( '../src/configya.js' )( './spec/non-existant.json' );
it( 'should return nothing', function() {
( cfg.get( 'thing' ) == undefined ).should.be.true;
describe( 'when using deprecated API (calling get())', function() {
describe( 'when loading config that doesn\'t exist', function() {
var cfg = require( '../src/configya.js' )( './spec/non-existant.json' );
it( 'should return nothing', function() {
( cfg.get( 'thing' ) === undefined )
.should.be.true;
} );
} );
} );
describe( 'when loading valid config without environment variables', function() {
var cfg = require( '../src/configya.js' )( './spec/test.json' );
describe( 'when loading valid config without environment variables', function() {
var cfg = require( '../src/configya.js' )( './spec/test.json' );
it( 'missing key should be missing', function() {
( cfg.get( 'thing' ) == undefined ).should.be.true;
} );
it( 'missing key should be missing', function() {
( cfg.get( 'thing' ) === undefined )
.should.be.true;
} );
it( 'missing key should return default', function() {
cfg.get( 'thing', ':(' ).should.equal( ':(' );
it( 'missing key should return default', function() {
cfg.get( 'thing', ':(' )
.should.equal( ':(' );
} );
it( 'valid key should return value', function() {
cfg.get( 'test-key' )
.should.equal( 'hulloo' );
} );
} );
it( 'valid key should return value', function() {
cfg.get( 'test-key' ).should.equal( 'hulloo' );
describe( 'when loading valid config with environment variables', function() {
var cfg;
before( function() {
process.env[ 'missing-from-config' ] = 'env';
process.env[ 'override-me' ] = 'OVERRIDE!';
cfg = require( '../src/configya.js' )( './spec/test.json' );
} );
describe( 'with no deploy-type environment var set', function() {
it( 'missing key should be missing', function() {
( cfg.get( 'thing' ) === undefined )
.should.be.true;
} );
it( 'missing key should return default', function() {
cfg.get( 'thing', ':(' )
.should.equal( ':(' );
} );
it( 'environment key should return value', function() {
cfg.get( 'missing-from-config' )
.should.equal( 'env' );
} );
it( 'environment key should override config key', function() {
cfg.get( 'override-me' )
.should.equal( 'OVERRIDE!' );
} );
} );
describe( 'with deploy-type set to DEV for testing', function() {
var cfg;
before( function() {
process.env[ 'deploy-type' ] = 'DEV';
cfg = require( '../src/configya.js' )( './spec/test.json' );
} );
it( 'missing key should be missing', function() {
( cfg.get( 'thing' ) === undefined )
.should.be.true;
} );
it( 'missing key should return default', function() {
cfg.get( 'thing', ':(' )
.should.equal( ':(' );
} );
it( 'environment key should return value', function() {
cfg.get( 'missing-from-config' )
.should.equal( 'env' );
} );
it( 'config key should override environment key', function() {
cfg.get( 'override-me' )
.should.equal( "you will see this only when you have deploy-type set to 'DEV'" );
} );
} );
after( function() {
process.env[ 'missing-from-config' ] = '';
process.env[ 'test-key' ] = '';
process.env[ 'deploy-type' ] = '';
} );
} );
} );
describe( 'when loading valid config with environment variables', function() {
var cfg = require( '../src/configya.js' )( './spec/test.json' );
process.env[ 'missing-from-config' ] = 'env';
process.env[ 'override-me' ] = 'OVERRIDE!';
describe( 'when accessing configuration data directly (new API)', function() {
describe( 'when loading config that doesn\'t exist', function() {
var cfg = require( '../src/configya.js' )( './spec/non-existant.json' );
it( 'missing key should be missing', function() {
( cfg.get( 'thing' ) == undefined ).should.be.true;
it( 'should return nothing', function() {
( cfg.thing === undefined )
.should.be.true;
} );
} );
describe( 'when loading valid config without environment variables', function() {
var cfg = require( '../src/configya.js' )( './spec/test.json' );
it( 'missing key should return default', function() {
cfg.get( 'thing', ':(' ).should.equal( ':(' );
} );
it( 'missing key should be missing', function() {
( cfg.thing === undefined )
.should.be.true;
} );
it( 'environment key should return value', function() {
cfg.get( 'missing-from-config' ).should.equal( 'env' );
it( 'valid key should return value', function() {
cfg[ 'test-key' ]
.should.equal( 'hulloo' );
} );
describe( 'when loading valid config with environment variables', function() {
var cfg;
before( function() {
process.env[ 'missing-from-config' ] = 'env';
process.env[ 'override-me' ] = 'OVERRIDE!';
cfg = require( '../src/configya.js' )( './spec/test.json' );
} );
describe( 'with no deploy-type environment var set', function() {
it( 'missing key should be missing', function() {
( cfg.thing === undefined )
.should.be.true;
} );
it( 'environment key should return value', function() {
cfg[ 'missing-from-config' ]
.should.equal( 'env' );
} );
it( 'environment key should override config key', function() {
cfg[ 'override-me' ]
.should.equal( 'OVERRIDE!' );
} );
} );
} );
} );
describe( 'with deploy-type set to DEV for testing', function() {
var cfg;
it( 'environment key should override config key', function() {
cfg.get( 'override-me' ).should.equal( 'OVERRIDE!' );
before( function() {
process.env[ 'deploy-type' ] = 'DEV';
cfg = require( '../src/configya.js' )( './spec/test.json' );
} );
it( 'missing key should be missing', function() {
( cfg.thing === undefined )
.should.be.true;
} );
it( 'environment key should return value', function() {
cfg[ 'missing-from-config' ]
.should.equal( 'env' );
} );
it( 'config key should override environment key', function() {
cfg[ 'override-me' ]
.should.equal( "you will see this only when you have deploy-type set to 'DEV'" );
} );
} );
after( function() {
process.env[ 'missing-from-config' ] = '';
process.env[ 'test-key' ] = '';
process.env[ 'deploy-type' ] = '';
} );
} );
{
"test-key": "hulloo",
"override-me": "you will never see this"
"override-me": "you will see this only when you have deploy-type set to 'DEV'"
}

@@ -1,39 +0,67 @@

var fs = require( 'fs' ),
path = require( 'path' );
var fs = require( 'fs' );
var path = require( 'path' );
var gnosis = require( 'gnosis' )();
var _ = require( 'lodash' );
var merge = function() {
var args = Array.prototype.slice.call( arguments );
var obj = {};
while( args.length > 0 ) {
var x = args.shift();
for( key in x ) {
var val = x [ key ];
if( val ) {
obj[ key ] = val;
function ensurePath( target, val, paths ) {
var key = paths.shift();
if ( paths.length === 0 ) {
target[ key ] = val;
} else {
target[ key ] = target[ key ] || {};
ensurePath( target[ key ], val, paths );
}
}
function parseEnvVarsIntoConfig( config ) {
var undRegx = /^[_]+/;
gnosis.traverse( process.env, function( instance, key, val, meta, root ) {
var k = key.toLowerCase();
config.__env__[ key ] = val;
var paths = undRegx.test( key ) ? [ k ] : k.split( "_" );
ensurePath( config, val, paths );
} );
}
function parseFileIntoConfig( config, pathToCfg, options ) {
var fullPath = path.resolve( pathToCfg );
if ( fs.existsSync( fullPath ) ) {
try {
var raw = fs.readFileSync( fullPath );
var json = JSON.parse( raw );
if ( options.preferCfgFile ) {
_.merge( config, json );
} else {
_.defaults( config, json );
}
} catch ( err ) {
console.log( 'error parsing configuration at "', fullPath, '"', err );
}
}
return obj;
};
}
module.exports = function( configFile ) {
var preferCfgFile = process.env[ 'deploy-type' ] === 'DEV';
var config = {
__env__: {},
// Deprecated, but still here for backwards compat ONLY
get: function( key, defaultValue ) {
var envVal = process.env[ key ];
return envVal || this[ key ] || defaultValue;
var val = this.__env__[ key ];
if ( preferCfgFile || !val ) {
val = this[ key ] || val;
}
return val || defaultValue;
}
};
if( configFile ) {
var fullPath = path.resolve( configFile );
if( fs.existsSync( fullPath ) ) {
try {
var raw = fs.readFileSync( fullPath );
var json = JSON.parse( raw );
config = merge( config, json );
} catch ( err ) {
console.log( 'error parsing configuration at "', fullPath, '"', err );
}
}
parseEnvVarsIntoConfig( config );
if ( configFile ) {
parseFileIntoConfig( config, configFile, {
preferCfgFile: preferCfgFile
} );
}
return config;
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc