Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyce committed Aug 31, 2016
1 parent 06ca488 commit 1d5f20c
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IDEA specific files
.idea
*.iml

# Packages
node_modules
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2016 KTH Royal Institute of Technology

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
# kth-node-configuration

Configuration module for Node.js projects.

## Usage

```javascript
const configurator = require('kth-node-configuration')

const config = configurator({
defaults: require('./config/defaults'),
dev: require('./config/dev'),
prod: require('./config/prod'),
ref: require('./config/ref'),
local: require('./config/local')
})

module.exports = {
full: config.full(),
secure: config.secure(),
safe: config.safe(),
env: config.env()
}
```

## Options

- `defaults` should contain settings that will apply if no other config
file has it set. It's recommended that this file contains a "skeleton"
for the secure settings to document what the local settings can set.
- `dev`, `ref`, and `prod` should contain environment specific settings.
They will override the defaults. The configurator selects this file
depending on the `process.env.NODE_ENV` variable.
- `local` should contain settings that either shouldn't be checked into
source control or local overrides specific to the machine running the
app.

## API

- `full()` returns the fully merged configuration.
- `secure()` returns only the secure merged configuration.
- `safe()` returns the fully merged configuration with the secure
section blanked out. This should be safe to use on the client-side.
- `env()` returns the current enviroment setting. Will be one of the
following: `dev`, `ref`, or `prod`.

## Pro-tip!

Use the [npm package __dotenv__][dotenv] to set environment variables.
Take a look at the unit tests for example usage.

[dotenv]: https://www.npmjs.com/package/dotenv
71 changes: 71 additions & 0 deletions configurator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

const _ = require('lodash')

const defaults = {
dev: {},
ref: {},
prod: {},
defaults: {},
local: {}
}

module.exports = function (options) {
options = _.merge({}, defaults, options)

function _getLowerCaseNodeEnv () {
return process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase()
}

function _merged () {
return _.merge({}, options.defaults, options[env()], options.local)
}

function _isProduction (nodeEnv) {
return nodeEnv === 'produktion' || nodeEnv === 'production' || nodeEnv === 'prod'
}

function _isReference (nodeEnv) {
return nodeEnv === 'referens' || nodeEnv === 'reference' || nodeEnv === 'ref'
}

function _isDevelopment (nodeEnv) {
return nodeEnv === 'development' || nodeEnv === 'dev'
}

function env () {
const nodeEnv = _getLowerCaseNodeEnv()

if (_isProduction(nodeEnv)) {
return 'prod'
}

if (_isReference(nodeEnv)) {
return 'ref'
}

if (_isDevelopment(nodeEnv)) {
return 'dev'
}

throw new Error(`Invalid NODE_ENV variable: ${nodeEnv}`)
}

return {
env: env,

full: function () {
return _merged()
},

safe: function () {
const full = this.full()
full.secure = {}
return full
},

secure: function () {
return this.full().secure
}
}
}
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "kth-node-configuration",
"version": "1.0.0",
"description": "Configuration module for Node.js projects",
"main": "configurator.js",
"repository": {
"type": "git",
"url": "https://github.com/KTH/kth-node-configuration"
},
"dependencies": {
"lodash": "4.13.1"
},
"keywords": [
"node",
"configuration"
],
"private": true,
"devDependencies": {
"dotenv": "2.0.0",
"standard": "7.1.0",
"tap-spec": "4.1.1",
"tape": "4.5.1"
},
"scripts": {
"test": "tape test/**/*Test.js | tap-spec",
"codecheck": "standard",
"preversion": "npm run codecheck && npm run test",
"postversion": "git push && git push --tags"
}
}
3 changes: 3 additions & 0 deletions test/unit/config/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NODE_ENV=ref
MONGODB_URL=mongodb://mongodb.local/dbname
SESSION_SECRET=ref-secret
15 changes: 15 additions & 0 deletions test/unit/config/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Contains common configuration that could be used in any environment.
// This is the first file that is processed and can be overridden by
// environment files and local settings.
module.exports = {
secure: {
mongodb: {
url: ''
},

session: ''
},

common: 'app-name',
name: 'configuration'
}
9 changes: 9 additions & 0 deletions test/unit/config/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Contains configuration that should not be in source control. Anything
// in here will override settings in the other files.
module.exports = {
secure: {
mongodb: {
url: 'mongodb://localhost/test'
}
}
}
14 changes: 14 additions & 0 deletions test/unit/config/ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file contains environment specific configuration that can be in
// source control. Note the process.env.MONGODB_URL usage! Settings here
// can still be overridden in the local file.
module.exports = {
secure: {
mongodb: {
url: process.env.MONGODB_URL
},

session: process.env.SESSION_SECRET
},

name: 'configuration-ref'
}
66 changes: 66 additions & 0 deletions test/unit/configuratorTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

const path = require('path')
const test = require('tape')

// You can safely use dotenv to set environment variables.
// Just make sure you do not check in the .env file to source control.
require('dotenv').config({ path: path.join(__dirname, 'config/.env') })

const configurator = require('../../configurator')

// get config files
// const devConfig = require('./config/dev')
const refConfig = require('./config/ref')
// const prodConfig = require('./config/prod')
const localConfig = require('./config/local')
const defaultsConfig = require('./config/defaults')

function create () {
return configurator({
// prod: prodConfig,
// dev: devConfig,
ref: refConfig,
defaults: defaultsConfig,
local: localConfig
})
}

test('falls back to default value', (assert) => {
const config = create()
const full = config.full()
const expected = 'configuration-ref'
assert.equal(full.name, expected, 'should equal ref name setting')
assert.end()
})

test('hides secure options', (assert) => {
const config = create()
const safe = config.safe()
const expected = { secure: {}, name: 'configuration-ref', common: 'app-name' }
assert.deepEqual(safe, expected, 'should equal ref safe settings')
assert.end()
})

test('gets only secure options', (assert) => {
const config = create()
const secure = config.secure()
const expected = { mongodb: { url: 'mongodb://localhost/test' }, session: process.env.SESSION_SECRET }
assert.deepEqual(secure, expected, 'should equal ref secure settings')
assert.end()
})

test('gets correct environment', (assert) => {
const config = create()
const env = config.env()
const full = config.full()
const expectedName = 'configuration-ref'
const expectedEnv = 'ref'
const expectedSession = 'ref-secret'
const expectedCommon = 'app-name'
assert.equal(full.name, expectedName, 'should equal ref name setting')
assert.equal(env, expectedEnv, 'should equal ref environment')
assert.equal(full.secure.session, expectedSession, 'should equal session setting')
assert.equal(full.common, expectedCommon, 'should use default common setting')
assert.end()
})

0 comments on commit 1d5f20c

Please sign in to comment.