1 | import _ from 'lodash'
|
2 | const EventEmitter = require('eventemitter3')
|
3 | import {
|
4 | getBrowserConfigPath, getServerConfigPath, getConfig
|
5 | } from './get-root-config'
|
6 |
|
7 | export class Config {
|
8 |
|
9 | // TODO(vjpr)
|
10 | //globalSchema = {properties: {}};
|
11 |
|
12 | constructor() {
|
13 | this.vent = new EventEmitter
|
14 | // Singleton config.
|
15 | // TODO(vjpr): get rid of this. It's bad to cache config here. For wallaby and more.
|
16 | this.config = getConfig(this.vent)
|
17 | this.env = new EnvironmentHelpers
|
18 | }
|
19 |
|
20 | get(...args) {
|
21 | return _.get(this.config, ...args)
|
22 | }
|
23 |
|
24 | set(key, newValue) {
|
25 | _.set(this.config[key], newValue)
|
26 | }
|
27 |
|
28 | setDefaults(namespace, keyToSchema) {
|
29 | // TODO(vjpr): We want to validate schemas eventually.
|
30 | // For now, we just use the default value.
|
31 | const keyToDefaultVal = _(keyToSchema).mapValues((v, k) => v.default).value()
|
32 | return _.defaultsDeep(this.config, {[namespace]: keyToDefaultVal})
|
33 | }
|
34 |
|
35 | //
|
36 | // Util
|
37 | //
|
38 |
|
39 | getBrowserConfigPath() { return getBrowserConfigPath() }
|
40 |
|
41 | getServerConfigPath() { return getServerConfigPath() }
|
42 |
|
43 | print() {
|
44 | // TODO(vjpr): Print linkable file location of where the caller of this method is.
|
45 | const pj = require('prettyjson')
|
46 | console.log(pj.render(this.config))
|
47 | }
|
48 |
|
49 | }
|
50 |
|
51 | class EnvironmentHelpers {
|
52 |
|
53 | isDev() {
|
54 | // TODO(vjpr): Use implementation from webpackerator.
|
55 | return process.env.NODE_ENV !== 'production'
|
56 | }
|
57 |
|
58 | }
|
59 |
|
60 | ////////////////////////////////////////////////////////////////////////////////
|
61 | // Singleton API
|
62 | ////////////////////////////////////////////////////////////////////////////////
|
63 |
|
64 | // The api is designed to have good compatibility with CommonJS requires.
|
65 |
|
66 | let singleton = new Config
|
67 |
|
68 | // TODO(vjpr): require.main.require('configurize') to ensure only one version is used.
|
69 |
|
70 | // Usage:
|
71 | //
|
72 | // import {config} from 'configurize'
|
73 | // config.get('foo')
|
74 | //
|
75 | // const {config} = require('configurize')
|
76 | // config.get('foo')
|
77 | //
|
78 | // require('configurize').config.get('foo')
|
79 | //
|
80 | // import * as configurize from 'configurize'
|
81 | // configurize.config.get('foo')
|
82 | //
|
83 | //
|
84 | export {singleton as config}
|
85 |
|
86 | // Usage:
|
87 | //
|
88 | // import config from 'configurize'
|
89 | // config.get('foo')
|
90 | //
|
91 | // import {default as config} from 'configurize'
|
92 | // config.get('foo'
|
93 | //
|
94 | // const {default: config} = require('configurize')
|
95 | // config.get('foo')
|
96 | //
|
97 | // require('configurize').default.get('foo')
|
98 | //
|
99 | export default singleton
|
100 |
|
101 | // NOTE: This would override all our exports.
|
102 | //module.exports = singleton
|
103 |
|
104 | ////////////////////////////////////////////////////////////////////////////////
|