From 1d5f20c5f80f4c16d1ceddbdbfd06d5996e4fb00 Mon Sep 17 00:00:00 2001 From: hoyce mac work Date: Wed, 31 Aug 2016 16:42:10 +0200 Subject: [PATCH] Initial commit --- .gitignore | 6 +++ LICENSE | 8 ++++ README.md | 50 ++++++++++++++++++++++++ configurator.js | 71 +++++++++++++++++++++++++++++++++++ package.json | 30 +++++++++++++++ test/unit/config/.env | 3 ++ test/unit/config/defaults.js | 15 ++++++++ test/unit/config/local.js | 9 +++++ test/unit/config/ref.js | 14 +++++++ test/unit/configuratorTest.js | 66 ++++++++++++++++++++++++++++++++ 10 files changed, 272 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 configurator.js create mode 100644 package.json create mode 100644 test/unit/config/.env create mode 100644 test/unit/config/defaults.js create mode 100644 test/unit/config/local.js create mode 100644 test/unit/config/ref.js create mode 100644 test/unit/configuratorTest.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d109af7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# IDEA specific files +.idea +*.iml + +# Packages +node_modules diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..670855c --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md index cc78f14..c7d6a77 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/configurator.js b/configurator.js new file mode 100644 index 0000000..a27a840 --- /dev/null +++ b/configurator.js @@ -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 + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4770d1c --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/unit/config/.env b/test/unit/config/.env new file mode 100644 index 0000000..4fbe1a5 --- /dev/null +++ b/test/unit/config/.env @@ -0,0 +1,3 @@ +NODE_ENV=ref +MONGODB_URL=mongodb://mongodb.local/dbname +SESSION_SECRET=ref-secret diff --git a/test/unit/config/defaults.js b/test/unit/config/defaults.js new file mode 100644 index 0000000..a524e5b --- /dev/null +++ b/test/unit/config/defaults.js @@ -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' +} diff --git a/test/unit/config/local.js b/test/unit/config/local.js new file mode 100644 index 0000000..fa322a7 --- /dev/null +++ b/test/unit/config/local.js @@ -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' + } + } +} diff --git a/test/unit/config/ref.js b/test/unit/config/ref.js new file mode 100644 index 0000000..580a3bf --- /dev/null +++ b/test/unit/config/ref.js @@ -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' +} diff --git a/test/unit/configuratorTest.js b/test/unit/configuratorTest.js new file mode 100644 index 0000000..a539d75 --- /dev/null +++ b/test/unit/configuratorTest.js @@ -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() +})