Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 29, 2017
2 parents e3f8329 + 57f0a45 commit 3b813c0
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<a name="1.0.3"></a>
## [1.0.3](https://github.com/adonisjs/adonis-sink/compare/v1.0.2...v1.0.3) (2017-05-29)


### Features

* **config:** allow customizer for config.merge ([c7ebade](https://github.com/adonisjs/adonis-sink/commit/c7ebade))
* **config:** merge method give priority to user fields ([feb4e40](https://github.com/adonisjs/adonis-sink/commit/feb4e40))
* **env:** add env provider ([2582754](https://github.com/adonisjs/adonis-sink/commit/2582754))



<a name="1.0.2"></a>
## [1.0.2](https://github.com/adonisjs/adonis-sink/compare/v1.0.1...v1.0.2) (2017-04-28)

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const _ = require('lodash')
const Config = require('./src/Config')
const Helpers = require('./src/Helpers')
const Logger = require('./src/Logger')
const Env = require('./src/Env')

const setupResolver = function (directories, appNamespace = 'App') {
const defaultDirectories = {
Expand All @@ -30,4 +31,4 @@ const setupResolver = function (directories, appNamespace = 'App') {
resolver.appNamespace(appNamespace)
}

module.exports = { Config, Helpers, Logger, setupResolver }
module.exports = { Config, Helpers, Logger, setupResolver, Env }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonis-sink",
"version": "1.0.2",
"version": "1.0.3",
"description": "Development kitchen sink for writing adonisjs providers",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/Config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ class Config {
*
* @param {String} key
* @param {Object} defaultValues
* @param {Function} [customizer]
*
* @return {Object}
*/
merge (key, defaultValues) {
merge (key, defaultValues, customizer) {
const value = _.get(this._config, key, {})
return _.merge(defaultValues, value)
return _.mergeWith(defaultValues, value, customizer)
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Env/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

/*
* adonis-sink
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const _ = require('lodash')

/**
* @module Adonis
* @submodule sink
*/

/**
* Manages env values by reading and writing values
* to `process.env`
*
* @class Env
* @constructor
*/
class Env {
constructor (processValues = {}) {
_.each(processValues, (value, key) => {
this.set(key, value)
})
}

/**
* Returns the value from process.env and if value
* is undefined then default value is returned
*
* @method get
*
* @param {String} key
* @param {Mixed} defaultValue
*
* @return {Mixed}
*/
get (key, defaultValue) {
return _.get(process.env, key, defaultValue)
}

/**
* Update/Set value on process.env variables
*
* @method set
*
* @param {String} key
* @param {Mixed} value
*/
set (key, value) {
_.set(process.env, key, value)
}
}

module.exports = Env
1 change: 0 additions & 1 deletion src/Helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const path = require('path')
* @module Adonis
* @submodule sink
*/

class Helpers {
constructor (appRoot) {
this._appRoot = appRoot
Expand Down
25 changes: 25 additions & 0 deletions test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ test.group('Config', () => {
}
})
})

test('merge empty sets with user values', (assert) => {
const config = new Config()
config.set('database', {
client: 'mysql',
connection: {}
})

const database = config.merge('database', {
client: 'foo',
connection: {
host: '127.0.0.1',
port: 3306
}
}, (obj, src, key) => {
if (key === 'connection') {
return src
}
})

assert.deepEqual(database, {
client: 'mysql',
connection: {}
})
})
})
37 changes: 37 additions & 0 deletions test/env.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

/*
* adonis-sink
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const test = require('japa')

const Env = require('../src/Env')

test.group('Env', () => {
test('merge constructor values to process.env', (assert) => {
const env = new Env({ USERNAME: 'virk' })
assert.equal(env.get('USERNAME'), 'virk')
})

test('get value for a given key from process.env', (assert) => {
const env = new Env()
assert.equal(env.get('PWD'), process.cwd())
})

test('return default value when actual value does not exists', (assert) => {
const env = new Env()
assert.equal(env.get('TESTING', 'nope'), 'nope')
})

test('set value for a given key', (assert) => {
const env = new Env()
env.set('TESTING', 'yup')
assert.equal(env.get('TESTING', 'nope'), 'yup')
})
})

0 comments on commit 3b813c0

Please sign in to comment.