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.9 to 0.1.0

derp.js

2

package.json
{
"name": "configya",
"version": "0.0.9",
"version": "0.1.0",
"description": "Config files that defer to env settings.",

@@ -5,0 +5,0 @@ "main": "src/configya.js",

@@ -7,3 +7,3 @@ # configya

* environment variables
* optional configuration
* optional configuration
* defaults hash

@@ -30,2 +30,14 @@

### Key Prefixing
`configya` lets you specify a prefix for your environment variables to be removed when your configuration is created. For example, if you have an environment variable `LK_RABBIT_BROKER_PORT` set to 5672, and call configya with the `prefix` option `var cfg = require( 'configya' )({prefix:'lk'});`, the resulting configuration object will be:
```javascript
{
rabbit: {
broker: {
port: "5672"
}
}
}
```
### Original Keys

@@ -38,3 +50,3 @@ The original keys are technically still stored on the object based on their source.

**Note**: These are really here for diagnostic/backwards compatibility. You shouldn't use/rely on them in your code.
## Usage

@@ -46,10 +58,20 @@

//with an environment prefix
var cfg = require( 'configya' )({prefix: 'lk'});
//with a config file as well
var cfg = require( 'configya' )( './path/to/configuration.json' );
var cfg = require( 'configya' )({file: './path/to/configuration.json'});
//with a defaults hash
var cfg = require( 'configya' )( { RABBIT_BROKER_PORT: 5672 } );
var cfg = require( 'configya' )({
defaults: { RABBIT_BROKER_PORT: 5672 }
});
//with defaults and a config (order of args doesn't matter)
var cfg = require( 'configya' )( { rabbit: { broker: { port: 5672 } } }, './path/to/configuration.json' );
//with defaults and a config
var cfg = require( 'configya' )({
defaults:{
rabbit: { broker: { port: 5672 } }
},
file: './path/to/configuration.json'
});

@@ -60,3 +82,19 @@ var port = cfg.rabbit.broker.port; // etc.

## Backwards Compatibility
The previous version of `configya` accepted multiple arguments of either a string for the file path or an object literal for defaults.
```javascript
//without a config file or defaults (using only environment)
var cfg = require( 'configya' )();
//with a config file as well
var cfg = require( 'configya' )( './path/to/configuration.json' );
//with a defaults hash
var cfg = require( 'configya' )( { RABBIT_BROKER_PORT: 5672 } );
//with defaults and a config (order of args doesn't matter)
var cfg = require( 'configya' )( { rabbit: { broker: { port: 5672 } } }, './path/to/configuration.json' );
var port = cfg.rabbit.broker.port; // etc.
```
The original version of `configya` used a `get` method to retrieve configuration values with the ability to specify a default/fallback if the key were missing. This is technically still supported, but we think the new approach (nested keys) is nicer. Here's an example of the original API:

@@ -67,5 +105,5 @@

// get the value from the config file, if an
// get the value from the config file, if an
// environment variable is present the environment
// variable ALWAYS trumps the file setting unless
// variable ALWAYS trumps the file setting unless
// you have deploy-type=DEV in your env settings

@@ -75,2 +113,2 @@ config.get( 'key' );

config.get( 'key', defaultValue );
```
```

@@ -12,3 +12,3 @@ var fs = require( 'fs' );

} else {
target[ key ] = ( ( original == undefined ) || overwrite ) ? val : original;
target[ key ] = ( ( original == undefined ) || overwrite ) ? val : original;
}

@@ -30,13 +30,22 @@ } );

function parseIntoTarget( source, target, cache ) {
var preRgx = /^[_]*/,
postRgx = /[_]*$/;
function parseIntoTarget( source, target, cache, prefix ) {
var preRgx = /^[_]*/;
var prefixRgx = new RegExp("^"+prefix+"_","i");
var postRgx = /[_]*$/;
_.each( source, function( val, key ) {
var k = key.toLowerCase(),
scrubbed = key.replace( preRgx, '' ).replace( postRgx, '' ),
paths = scrubbed.split( '_' );
key = key.replace(prefixRgx,'');
var k = key.toLowerCase();
var scrubbed = key.replace( preRgx, '' ).replace( postRgx, '' );
var paths = scrubbed.split( '_' );
if(prefixRgx.test(paths[0])){
paths.shift();
}
target[ key ] = val;
target[ k ] = val;
ensurePath( target, val, paths );
if( cache ) {
if( cache ) {
target[ cache ][ key ] = val;

@@ -61,12 +70,10 @@ target[ cache ][ k ] = val;

module.exports = function() {
var args = Array.prototype.slice.call( arguments );
var file = _.where( args, _.isString )[ 0 ];
var hash = _.where( args, _.isObject )[ 0 ] || {};
var defaults = { __defaults__: {} };
function buildConfig(options){
var defaultsHash = { __defaults__: {} };
var fileHash = { __file__: {} };
var envHash = { __env__: {} };
var json = file ? readConfig( file ) : {};
var json = options.file ? readConfig( options.file ) : {};
var preferCfgFile = process.env[ 'deploy-type' ] === 'DEV';
var config = {

@@ -79,13 +86,30 @@ __env__: {},

};
parseIntoTarget( process.env, envHash, '__env__' );
parseIntoTarget( hash, defaults, '__defaults__' );
parseIntoTarget( process.env, envHash, '__env__', options.prefix );
parseIntoTarget( options.defaults || {}, defaultsHash, '__defaults__' );
parseIntoTarget( json, fileHash, '__file__' );
var fileOp = preferCfgFile ? 'merge' : 'defaults';
_.merge( config, envHash );
deepMerge( config, fileHash, preferCfgFile );
deepMerge( config, defaults );
deepMerge( config, defaultsHash );
return config;
};
}
module.exports = function() {
var args = Array.prototype.slice.call( arguments );
var options = {};
var newApi = args.length === 1
&& _.isObject( options = args[0] )
&& ( options.file || options.defaults );
if ( !newApi ){
options = {
file: _.where( args, _.isString )[0],
defaults: _.where( args, _.isObject )[0] || {}
}
}
return buildConfig(options);
};
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