This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |