Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
implement node-config (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kc-dot-io authored and daffl committed Oct 22, 2016
1 parent 93d7a35 commit afb1618
Showing 7 changed files with 51 additions and 36 deletions.
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -6,15 +6,13 @@
## About

`feathers-configuration` allows you to load default and environment specific JSON configuration files and environment variables and set them on your application. Here is what it does:
This release of `feathers-configuration` simply acts as a wrapped around [node-config](https://github.com/lorenwest/node-config).

- Given a root and configuration path load a `default.json` in that path
- When the `NODE_ENV` is not `development`, also try to load `<NODE_ENV>.json` in that path and merge both configurations
- Go through each configuration value and sets it on the application (via `app.set(name, value)`).
- If the value is a valid environment variable (e.v. `NODE_ENV`), use its value instead
- If the value start with `./` or `../` turn it it an absolute path relative to the configuration file path
- Both `default` and `<env>` configurations can be modules which provide their computed settings with `module.exports = {...}` and a `.js` file suffix. See `test/config/testing.js` for an example.
All rules listed above apply for `.js` modules.
By default this implementation will look in `config/*` for `default.json`.

As per the [config docs](https://github.com/lorenwest/node-config/wiki/Configuration-Files) this is highly configurable.

Future releases will also include adapters for external configuration storage.

## Usage

@@ -25,7 +23,7 @@ import feathers from 'feathers';
import configuration from 'feathers-configuration';

// Use the current folder as the root and look configuration up in `settings`
let app = feathers().configure(configuration(__dirname, 'settings'))
let app = feathers().configure()
```

## Example
@@ -55,18 +53,22 @@ In `config/production.js` we are going to use environment variables (e.g. set by

Now it can be used in our `app.js` like this:

```
```js
import feathers from 'feathers';
import configuration from 'feathers-configuration';

let conf = configuration();

let app = feathers()
.configure(configuration(__dirname));
.configure(conf);

console.log(app.get('frontend'));
console.log(app.get('host'));
console.log(app.get('port'));
console.log(app.get('mongodb'));
console.log(app.get('templates'));
console.log(conf());

```

If you now run
@@ -80,7 +82,14 @@ node app
// -> path/to/templates
```

Or with a different environment and variables:
Or via custom environment variables by setting them in `config/custom-environment-variables.json`:

```js
{
"port": "PORT",
"mongodb": "MONGOHQ_URL"
}
```

```
PORT=8080 MONGOHQ_URL=mongodb://localhost:27017/production NODE_ENV=production node app
@@ -91,6 +100,8 @@ PORT=8080 MONGOHQ_URL=mongodb://localhost:27017/production NODE_ENV=production n
// -> path/to/templates
```

You can also override these variables with arguments. Read more about how with [node-config](https://github.com/lorenwest/node-config)

## License

Copyright (c) 2015
5 changes: 4 additions & 1 deletion example/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import feathers from 'feathers';
import configuration from '../src';

let conf = configuration();

let app = feathers()
.configure(configuration(__dirname));
.configure(conf);

console.log(app.get('frontend'));
console.log(app.get('host'));
console.log(app.get('port'));
console.log(app.get('mongodb'));
console.log(app.get('templates'));
console.log(conf());
4 changes: 4 additions & 0 deletions example/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": "PORT",
"mongodb": "MONGOHQ_URL"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -35,15 +35,15 @@
"compile": "rm -rf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"jshint": "jshint src/. test/. --config",
"mocha": "NODE_ENV=testing mocha test/ --compilers js:babel-core/register",
"mocha": "NODE_CONFIG_DIR=./test/config/ NODE_ENV=testing mocha test/ --compilers js:babel-core/register",
"test": "npm run jshint && npm run mocha && nsp check"
},
"directories": {
"lib": "lib"
},
"dependencies": {
"debug": "^2.2.0",
"deep-assign": "^2.0.0"
"config": "^1.21.0"
},
"devDependencies": {
"babel-cli": "^6.1.4",
33 changes: 13 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import fs from 'fs';
import path from 'path';
import makeDebug from 'debug';
import deepAssign from 'deep-assign';
import path from 'path';

const debug = makeDebug('feathers:configuration');
const config = require('config');
const separator = path.sep;

export default module.exports = function (root, configFolder = 'config', deep = true) {
export default module.exports = function () {
return function() {
const app = this;
const env = app.settings.env;

let app = this;

const convert = current => {
const result = Array.isArray(current) ? [] : {};

@@ -29,7 +29,7 @@ export default module.exports = function (root, configFolder = 'config', deep =
} else if(value.indexOf('.') === 0 || value.indexOf('..') === 0) {
// Make relative paths absolute
value = path.resolve(
path.join(root, configFolder),
path.join(config.util.getEnv('NODE_CONFIG_DIR')),
value.replace(/\//g, separator)
);
}
@@ -42,24 +42,17 @@ export default module.exports = function (root, configFolder = 'config', deep =
return result;
};

let config = convert(require(path.join(root, configFolder, 'default')));

const env = config.util.getEnv('NODE_ENV');
debug(`Initializing configuration for ${env} environment`);
const conf = convert(config);

const envConfig = path.join(root, configFolder, env);
// We can use sync here since configuration only happens once at startup
if(fs.existsSync(`${envConfig}.js`) || fs.existsSync(`${envConfig}.json`)) {
config = deep ? deepAssign(config, convert(require(envConfig))) :
Object.assign(config, convert(require(envConfig)));
} else {
debug(`Configuration file for ${env} environment not found at ${envConfig}`);
if(!app) {
return conf;
}

Object.keys(config).forEach(name => {
let value = config[name];

Object.keys(conf).forEach(name => {
let value = conf[name];
debug(`Setting ${name} configuration value to`, value);

app.set(name, value);
});
};
4 changes: 4 additions & 0 deletions test/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": "PORT",
"mongodb": "MONGOHQ_URL"
}
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { join } from 'path';
import plugin from '../src';

describe('feathers-configuration', () => {
const app = feathers().configure(plugin(__dirname));
const app = feathers().configure(plugin());

it('initialized app with default data', () =>
assert.equal(app.get('port'), 3030)

0 comments on commit afb1618

Please # to comment.