forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (64 loc) · 1.65 KB
/
index.js
File metadata and controls
71 lines (64 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
/**
* adonis-framework
*
* (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 requireAll = require('require-all')
const _ = require('lodash')
const util = require('../../lib/util')
/**
* Manage configuration for an application by
* reading all .js files from config directory.
*/
class Config {
constructor (Helpers) {
const configPath = Helpers.configPath()
/**
* @type {Object}
*/
this.config = requireAll({
dirname: configPath,
filters: /(.*)\.js$/
})
}
/**
* get value for a given key from config store.
*
* @param {String} key - Configuration key to return value for
* @param {Mixed} [defaultValue] - Default value to return when actual value
* is null or undefined
* @return {Mixed}
*
* @example
* Config.get('database.connection')
* Config.get('database.mysql.host')
*
* @public
*/
get (key, defaultValue) {
defaultValue = util.existy(defaultValue) ? defaultValue : null
const returnValue = _.get(this.config, key)
return util.existy(returnValue) ? returnValue : defaultValue
}
/**
* set/update value for a given key inside
* config store.
*
* @param {String} key - Key to set value for
* @param {Mixed} value - Value to be saved next to defined key
*
* @example
* Config.set('database.connection', 'mysql')
* Config.set('database.mysql.host', 'localhost')
*
* @public
*/
set (key, value) {
_.set(this.config, key, value)
}
}
module.exports = Config