Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide .get(), .set() helpers, refactor config #126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = class TrailsApp extends events.EventEmitter {
}

this.pkg = app.pkg
this.config = lib.Trails.assignConfigDefaults(app.config)
this.config = new lib.Config(app.config)
this.api = app.api
this.bound = false
this.started = false
Expand Down
79 changes: 79 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

const path = require('path')
const _ = {
get: require('lodash.get'),
set: require('lodash.set')
}

module.exports = class Config {

/**
* Assign default configuration values to an object
*
* @param {Object?} config Object onto which the defaults should be assigned
* @return {Object} The object, modified to have all the defaults
*/
static defaults(config) {
config = config || {}

if (!_.get(config, 'log')) {
_.set(config, 'log', {})
}

if (!_.get(config, `env.${process.env.NODE_ENV}`)) {
_.set(config, `env.${process.env.NODE_ENV}`, {})
}

if (!_.get(config, 'main.packs')) {
_.set(config, 'main.packs', [])
}

if (!_.get(config, 'main.maxListeners')) {
_.set(config, 'main.maxListeners', 128)
}

if (!_.get(config, 'main.paths.root')) {
_.set(config, 'main.paths.root', path.resolve(process.cwd()))
}

if (!_.get(config, 'main.paths.temp')) {
_.set(config, 'main.paths.temp', path.resolve(_.get(config, 'main.paths.root'), '.tmp'))
}

return config
}


/**
* Create a new instance, optionally merging config values into it
*
* @param {Object?} values Optional values to merge into the Config
*/
constructor (values) {
Object.assign(this, values)
Config.defaults(this)
}

/**
* Safely retrieve configuration properties
*
* @param {String} property The path of the configuration value to retrieve
* @param {Mixed} defaultValue If the property does not exist, use this as default
* @return {Mixed} The property's value, `undefined` if it does not exist
*/
get(property, defaultValue) {
return _.get(this, property, defaultValue)
}

/**
* Safely set configuration properties
*
* @param {String} property The path of the configuration value to set
* @param {Mixed} value Value to set the property to
* @return {Mixed} The value set
*/
set(property, value) {
return _.set(this, property, value)
}
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.i18n = require('./i18n')
exports.Trailpack = require('./trailpack')
exports.Trails = require('./trails')
exports.Config = require('./config')
39 changes: 0 additions & 39 deletions lib/trails.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
'use strict'

const path = require('path')

const TrailsUtil = module.exports = {

assignConfigDefaults (config) {
if (!config) {
config = { }
}
if (!config.env) {
config.env = { }
}
if (!config.env[process.env.NODE_ENV]) {
config.env[process.env.NODE_ENV] = { }
}
if (!config.main) {
config.main = { }
}
if (!config.main.packs) {
config.main.packs = [ ]
}
if (!config.main.paths) {
config.main.paths = { }
}
if (!config.main.maxListeners) {
config.main.maxListeners = 128
}
if (!config.log) {
config.log = { }
}

if (!config.main.paths.root) {
config.main.paths.root = path.resolve(process.cwd())
}

if (!config.main.paths.temp) {
config.main.paths.temp = path.resolve(process.cwd(), '.tmp')
}

return config
},

/**
* Bind listeners various application events
*/
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
"url": "https://github.com/trailsjs/trails/issues"
},
"homepage": "http://trailsjs.io",
"dependencies": {},
"dependencies": {
"lodash.get": "^4.1.2",
"lodash.set": "^4.0.1"
},
"devDependencies": {
"eslint": "^2.0.0-beta.2",
"eslint-config-trails": "^1.0",
Expand Down
52 changes: 52 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@ const testAppDefinition = require('./testapp')

describe('Trails', () => {
describe('@TrailsApp', () => {
describe('config', () => {
let app

before(() => {
app = new TrailsApp(testAppDefinition)
})

describe('.get() helper', () => {
it('should exist', () => {
assert('get' in app.config, 'config.get() is not defined')
assert.equal(typeof app.config.get, 'function')
})

it('should not be enumerable', () => {
assert(!('get' in Object.keys(app.config)), 'config.get() should not be enumerable')
})

it('should return value at given property path', () => {
const property = app.config.get('main.paths.root')

// Not asserting actual string value because the path can change any time
// Having a string instead of undefined is good enough indicator that it works
assert.equal(typeof property, 'string')
})

it('should return undefined for non-existent properties', () => {
const property = app.config.get('just.some.random.stuff')

assert.equal(property, undefined)
})
})

describe('.set() helper', () => {
it('should exist', () => {
assert('set' in app.config, 'config.set() is not defined')
assert.equal(typeof app.config.set, 'function')
})

it('should not be enumerable', () => {
assert(!('set' in Object.keys(app.config)), 'config.set() should not be enumerable')
})

it('should set the value at given path', () => {
const value = { test: true }

app.config.set('some.location', value)

assert.equal(app.config.some.location, value)
})
})
})

describe('idempotence', () => {
it('should be able to start and stop many instances in a single node process', () => {
let cycles = [ ]
Expand Down