1 | const nconf = require('nconf')
|
2 | const path = require('path')
|
3 | const fs = require('fs')
|
4 | const isArray = require('lodash/isArray')
|
5 | const each = require('lodash/each')
|
6 |
|
7 | let app = process.env.APP
|
8 | let stage = process.env.STAGE
|
9 |
|
10 | initNconf(process.env.ROOT_PATH || process.cwd())
|
11 |
|
12 | function initNconf (dirname) {
|
13 | let addNconfFile = (nconf, filename) => {
|
14 | let filePath = path.join(dirname, 'config', filename + '.json')
|
15 | if (fs.existsSync(filePath)) {
|
16 | nconf.file(filePath)
|
17 | return true
|
18 | }
|
19 | console.log('Warning! APP and/or STATE are provided but config file ' +
|
20 | 'wasn\'t found: ' + filePath)
|
21 | return false
|
22 | }
|
23 |
|
24 | nconf.env()
|
25 | if (app && stage) addNconfFile(nconf, app + '_' + stage)
|
26 | else if (stage) addNconfFile(nconf, stage)
|
27 | else if (app) addNconfFile(nconf, app)
|
28 |
|
29 | nconf.defaults(require(dirname + '/config.json'))
|
30 |
|
31 |
|
32 | if (!process.env.REDIS_URL && nconf.get('REDIS_URL')) {
|
33 | process.env.REDIS_URL = nconf.get('REDIS_URL')
|
34 | }
|
35 |
|
36 |
|
37 | if (isArray(nconf.get('COPY_TO_ENV'))) {
|
38 | each(nconf.get('COPY_TO_ENV'), (option) => {
|
39 | process.env[option] = nconf.get(option)
|
40 | })
|
41 | }
|
42 |
|
43 |
|
44 | if (isArray(nconf.get('PUBLIC'))) {
|
45 | global.env = global.env || {}
|
46 | each(nconf.get('PUBLIC'), (option) => {
|
47 | global.env[option] = nconf.get(option)
|
48 | })
|
49 | }
|
50 | }
|