@myob-oss/config
Advanced tools
Comparing version 1.0.5 to 1.0.6
{ | ||
which: 'default' | ||
configurationFile: 'default' // Note that this is not strictly JSON but will be parsed correctly. | ||
} |
{ | ||
"which": "development" | ||
"configurationFile": "development" | ||
} |
{ | ||
'which': 'runtime' | ||
'configurationFile': 'runtime' // Note that this is not strictly JSON but will be parsed correctly. | ||
} |
@@ -50,4 +50,4 @@ const merge = require('deepmerge'); | ||
default: | ||
// Do nothing | ||
break; | ||
// Do not update a configuration file | ||
return false; | ||
} | ||
@@ -54,0 +54,0 @@ |
{ | ||
"name": "@myob-oss/config", | ||
"version": "1.0.5", | ||
"description": "A simple, predictable configuration module.", | ||
"version": "1.0.6", | ||
"description": "A simple, slightly opinionated, and predictable configuration module.", | ||
"main": "lib/config-wrapper.js", | ||
@@ -21,15 +21,15 @@ "scripts": { | ||
"dependencies": { | ||
"deepmerge": "^2.0.1", | ||
"json5": "^0.5.1" | ||
"deepmerge": "^2.1.1", | ||
"json5": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.13.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint": "^5.0.1", | ||
"eslint-config-airbnb-base": "^13.0.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^4.0.0", | ||
"gulp-mocha": "^4.3.1", | ||
"gulp-eslint": "^4.0.2", | ||
"gulp-mocha": "^6.0.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^4.0.1", | ||
"should": "^13.1.3" | ||
"mocha": "^5.2.0", | ||
"should": "^13.2.1" | ||
}, | ||
@@ -36,0 +36,0 @@ "engines": { |
# @myob-oss/config | ||
A simple, predictable configuration module for Node.js applications. | ||
A simple, slightly opinionated, and predictable configuration module for Node.js applications. | ||
This uses the environment variable NODE_ENV to determine which configuration file to load. | ||
To install the module in your project use the following command. | ||
The module assumes that all the configuration files in JSON are in the `/config` folder at the top level of the project. The JSON files can have comments in them, as the module uses the [json5](https://www.npmjs.com/package/json5) module to parse the config files. | ||
```bash | ||
npm install @myob-oss/config | ||
``` | ||
To use the module (after you have installed it in your project), you only need to require it, and it exposes the configuration as an Object. | ||
```js | ||
const config = require('@myob-oss/config'); | ||
console.log(config.configurationFile); // prints "runtime" from the example below. | ||
``` | ||
Yes, this is another configuration module. We know there are others out there. We created this configuration module to simplify and unify the way in which we configured our node applications to work with our various environments including our cloud infrastructure, continuous delivery pipelines and running our apps locally. | ||
The benefits we have seen from using this configuration module are: | ||
* A consistent way to enable environmentally-aware configuration across a suite of node applications. | ||
* Easy to use, just require the module and there is your configuration ready to use. | ||
* Doesn't force you to repeat configuration that is consistent across all environments. | ||
* Easy to see the running config file by inspecting the runtime.json file. (Works great for just shelling in a taking a look!) | ||
This configuration module has also been mentioned in the [Under The Hood](https://medium.com/myobunderthehood/why-configuration-matters-25456109041d) series of blog posts. | ||
## How the Configuration Hierarchy Works | ||
This module uses the environment variable `NODE_ENV` to determine which configuration file to load. | ||
The module assumes that all the configuration files are in [JSON](https://www.json.org/) format and in the `/config` folder at the top level of the project. The JSON files can have comments in them, as the module uses the [json5](https://www.npmjs.com/package/json5) module to parse the config files. | ||
The module loads 3 configuration files in the following order (each overwrites the previous if they have the same key): | ||
@@ -20,4 +47,4 @@ | ||
{ | ||
"foo": "bar", | ||
"hello": "there" | ||
"configurationFile": "default", | ||
"disableConsole": false | ||
} | ||
@@ -29,3 +56,3 @@ ``` | ||
{ | ||
"my": "goodness" | ||
"debugLevel": "trace" | ||
} | ||
@@ -37,15 +64,15 @@ ``` | ||
{ | ||
"foo": "baz", | ||
"au": "revoir" | ||
"configurationFile": "runtime", | ||
"validHosts": ["localhost", "example.com"] | ||
} | ||
``` | ||
The resulting config will be: | ||
The resulting configuration will be: | ||
```json | ||
{ | ||
"foo": "baz", | ||
"hello": "there", | ||
"au": "revoir", | ||
"my": "goodness" | ||
"configurationFile": "runtime", | ||
"disableConsole": false, | ||
"validHosts": ["localhost", "example.com"], | ||
"debugLevel": "trace" | ||
} | ||
@@ -56,10 +83,2 @@ ``` | ||
To use the module, you simply require it, and it exposes the configuration as an Object. | ||
```js | ||
const config = require('@myob-oss/config'); | ||
console.log(config.foo); // prints "baz" from the example above. | ||
``` | ||
## Testing | ||
@@ -75,3 +94,3 @@ | ||
To run the coverage scanner and generate coverage report use the following command. It fails if the threshold coverage has not passed. The threshold settings are stored in `test/.istanbul.yml`. | ||
To run the coverage scanner and generate coverage report use the following command. It fails if the threshold coverage has not passed. The threshold settings are stored in the file `test/.istanbul.yml`. | ||
@@ -78,0 +97,0 @@ ```bash |
@@ -7,6 +7,6 @@ function generateDefaultConfig(options) { | ||
return { | ||
which: 'default', | ||
configurationFile: 'default', | ||
an_array: [1, 2, 3, 4], | ||
an_object: { | ||
foo: 'bar', | ||
property1: 'value1', | ||
}, | ||
@@ -25,6 +25,6 @@ a_number: 1, | ||
return { | ||
which: 'environment', | ||
configurationFile: 'environment', | ||
a_string: 'trainy mctrainface', | ||
an_array: [4, 5], | ||
a_variant: ['how', 'zat'], | ||
a_variant: ['an', 'array'], | ||
}; | ||
@@ -39,9 +39,9 @@ } | ||
return { | ||
which: 'runtime', | ||
configurationFile: 'runtime', | ||
an_object: { | ||
foo: 'baz', | ||
thing: 'amajig', | ||
property1: 'value2', | ||
another: 'property', | ||
}, | ||
a_variant: { | ||
how: 'zat', | ||
an: 'object', | ||
}, | ||
@@ -48,0 +48,0 @@ }; |
@@ -45,11 +45,11 @@ /* global it:false, describe:false, beforeEach:false */ | ||
result.should.be.ok; // eslint-disable-line no-unused-expressions | ||
result.should.be.ok(); | ||
result = config.setConfig(config.getConstants().ENVIRONMENT, environmentConfigFixture); | ||
result.should.be.ok; // eslint-disable-line no-unused-expressions | ||
result.should.be.ok(); | ||
result = config.setConfig(config.getConstants().RUNTIME, runtimeConfigFixture); | ||
result.should.be.ok; // eslint-disable-line no-unused-expressions | ||
result.should.be.ok(); | ||
@@ -93,7 +93,7 @@ const defaultConfig = config.getConfig(config.getConstants().DEFAULT); | ||
result.should.not.be.ok; // eslint-disable-line no-unused-expressions | ||
result.should.not.be.ok(); | ||
result = config.setConfig(config.getConstants().DEFAULT, null); | ||
result.should.not.be.ok; // eslint-disable-line no-unused-expressions | ||
result.should.not.be.ok(); | ||
@@ -116,3 +116,3 @@ done(); | ||
effectiveConfig.which.should.equal('runtime'); | ||
effectiveConfig.configurationFile.should.equal('runtime'); | ||
@@ -119,0 +119,0 @@ done(); |
/* global it:false, describe:false */ | ||
const config = require('../../lib/config-wrapper.js'); | ||
const config2 = require('../../lib/config-wrapper.js'); | ||
describe('ConfigWrapper class', () => { | ||
it('Should expose the configuration', (done) => { | ||
config.should.have.property('which'); | ||
config.which.should.eql('runtime'); | ||
config.should.have.property('configurationFile'); | ||
config.configurationFile.should.eql('runtime'); | ||
done(); | ||
}); | ||
it('Should expose the same configuration if it is required twice', (done) => { | ||
config2.should.have.property('configurationFile'); | ||
config2.configurationFile.should.eql('runtime'); | ||
config2.configurationFile.should.eql(config.configurationFile); | ||
done(); | ||
}); | ||
}); |
@@ -25,3 +25,3 @@ /* global it:false, describe:false, beforeEach:false */ | ||
result.should.eql({ which: 'default' }); | ||
result.should.eql({ configurationFile: 'default' }); | ||
@@ -28,0 +28,0 @@ done(); |
Sorry, the diff of this file is not supported yet
18149
336
103
+ Addedjson5@1.0.2(transitive)
+ Addedminimist@1.2.8(transitive)
- Removedjson5@0.5.1(transitive)
Updateddeepmerge@^2.1.1
Updatedjson5@^1.0.1