dull-config
Advanced tools
Comparing version 0.1.1 to 0.2.1
23
index.js
@@ -10,3 +10,3 @@ 'use strict'; | ||
const _ = require('lodash'); | ||
const makeError = require ('make-error') | ||
const makeError = require ('make-error'); | ||
@@ -107,2 +107,21 @@ | ||
}, | ||
/** | ||
* loads a config object that will merge with the currentStore | ||
* useful for testing, local dev setups, and old school deployments. | ||
* @param fullPath a full path to the file. a relative path won't work | ||
* @returns {boolean} If the file loaded it returns true. If not it returns false | ||
*/ | ||
loadLocal(fullPath){ | ||
if(fullPath && typeof fullPath != 'string'){ | ||
throw new TypeError('loadLocal requires a string') | ||
} | ||
try{ | ||
let configModule = require(fullPath) | ||
configStore = _.merge({},configStore,configModule) | ||
return true | ||
}catch(e){ | ||
return false; | ||
} | ||
} | ||
@@ -112,2 +131,2 @@ | ||
module.exports = config | ||
module.exports = config; |
{ | ||
"name": "dull-config", | ||
"version": "0.1.1", | ||
"version": "0.2.1", | ||
"description": "simple dull and boring config", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,3 +21,3 @@ [![API Doc](https://doclets.io/toddgeist/dull-config/master.svg)](https://doclets.io/toddgeist/dull-config/master) | ||
That's really it. Its not even that much code 90 loc with docs :-) Not including dependancies. | ||
That's really it. Its not even that much code 125 loc with docs :-) Not including dependancies. | ||
@@ -114,2 +114,22 @@ ### Installation | ||
### Local Config Overrides | ||
Sometimes its nice to be able to load a local config file, that overrides settings for this particular local machine. This is useful for testing or old school deployments when you are running on an actual server somewhere. dull-config as a loadLocal() method to let you do this. | ||
Its important to note that in this case you need to specify the full path to the local config file. This is so it can be conditionally included if it exists, and ignored if it doesn't. | ||
**Important!** It's good practice to **NOT** check local config into your source control. If you want to remind your self of what goes in there, then check in an example. i.e. local.config.example.js | ||
_config/local.js_ | ||
```js | ||
{ | ||
api: local.endpoint.com | ||
} | ||
``` | ||
then in your setup after you've loaded everything else. | ||
```js | ||
config.loadLocal(__dirname + '/config/local.js') | ||
``` | ||
### Secrets | ||
@@ -119,3 +139,2 @@ | ||
_config/db.js_ | ||
@@ -130,2 +149,3 @@ ```js | ||
_index.js_ | ||
```js | ||
@@ -132,0 +152,0 @@ const config = require('config') |
@@ -1,2 +0,2 @@ | ||
const expect = require('expect.js') | ||
const expect = require('expect.js'); | ||
const config = require('../'); | ||
@@ -6,3 +6,3 @@ | ||
const DB_USER_NAME = 'my db user name'; | ||
const DB_PASSWORD = 'my db password' | ||
const DB_PASSWORD = 'my db password'; | ||
@@ -14,3 +14,3 @@ | ||
process.env.NODE_ENV=env | ||
process.env.NODE_ENV=env; | ||
config.load({ | ||
@@ -31,3 +31,3 @@ email : { to : 'todd@geistinteractive.com',mistake : undefined }, | ||
process.env.SECRET = SECRET; | ||
process.env.DB_USER_NAME = DB_USER_NAME | ||
process.env.DB_USER_NAME = DB_USER_NAME; | ||
process.env.DB_PASSWORD = DB_PASSWORD | ||
@@ -41,3 +41,3 @@ | ||
before((done)=>{ | ||
loadConfigForTest() | ||
loadConfigForTest(); | ||
console.log(config.get('email')); | ||
@@ -59,4 +59,4 @@ done() | ||
it('should get the full object with null or "" path' , function( done ) { | ||
expect(config.get('')).to.be.an('object') | ||
expect(config.get()).to.be.an('object') | ||
expect(config.get('')).to.be.an('object'); | ||
expect(config.get()).to.be.an('object'); | ||
done() | ||
@@ -90,3 +90,3 @@ }); | ||
before((done)=>{ | ||
loadConfigForTest() | ||
loadConfigForTest('development'); | ||
done() | ||
@@ -119,3 +119,3 @@ }); | ||
it('should work when obj has no default or env ' , function( done ) { | ||
expect(config.get('email.to')).to.equal('todd@geistinteractive.com') | ||
expect(config.get('email.to')).to.equal('todd@geistinteractive.com'); | ||
done() | ||
@@ -125,3 +125,3 @@ }); | ||
it('should get a default setting' , function( done ) { | ||
expect(config.get('db.url')).to.equal('production') | ||
expect(config.get('db.url')).to.equal('production'); | ||
done() | ||
@@ -131,14 +131,52 @@ }); | ||
it('should get a default setting when not over written' , function( done ) { | ||
expect(config.get('db.userName')).to.equal(DB_USER_NAME) | ||
expect(config.get('db.userName')).to.equal(DB_USER_NAME); | ||
done() | ||
}); | ||
it('should get a very deep overwritten value' , function( done ) { | ||
expect(config.get('auth.make.up.super.deep.setting')).to.equal('iamdeep-production') | ||
expect(config.get('auth.make.up.super.deep.setting')).to.equal('iamdeep-production'); | ||
done() | ||
}); | ||
it('should get a very deep NOT overwritten value' , function( done ) { | ||
expect(config.get('auth.make.up.super.deep.setting2')).to.equal('iamalsodeep') | ||
expect(config.get('auth.make.up.super.deep.setting2')).to.equal('iamalsodeep'); | ||
done() | ||
}) | ||
}); | ||
describe('localConfig', function () { | ||
before((done)=>{ | ||
loadConfigForTest(); | ||
done() | ||
}); | ||
it('should not throw, if its not there' , function( done ) { | ||
const shouldNotThrow = ()=>{ | ||
config.loadLocal('../examples/NOTHERE') | ||
}; | ||
expect(shouldNotThrow).not.to.throwError(); | ||
done() | ||
}); | ||
it('should throw if an object is passed' , function( done ) { | ||
const shouldThrow = () => { | ||
config.loadLocal(require('../examples/local')) | ||
}; | ||
expect(shouldThrow).to.throwError(); | ||
done() | ||
}); | ||
it('should return false if the file is not there' , function( done ) { | ||
expect(config.loadLocal(__dirname + '/../examples/nothere')).to.be(false); | ||
done() | ||
}); | ||
it('should override a previously loaded config' , function( done ) { | ||
expect( | ||
config.loadLocal(__dirname + '/../examples/local') | ||
).to.be(true); | ||
expect(config.get('db.url')).to.be('local'); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14911
10
272
166
6