UNPKG

1.62 kBJavaScriptView Raw
1const nconf = require('nconf')
2const path = require('path')
3const fs = require('fs')
4const isArray = require('lodash/isArray')
5const each = require('lodash/each')
6
7let app = process.env.APP
8let stage = process.env.STAGE
9
10initNconf(process.env.ROOT_PATH || process.cwd())
11
12function 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 // Copy REDIS_URL into env if present (it'll be used by redis-url module)
32 if (!process.env.REDIS_URL && nconf.get('REDIS_URL')) {
33 process.env.REDIS_URL = nconf.get('REDIS_URL')
34 }
35
36 // Copy MONGO_URL into env if present
37 if (!process.env.MONGO_URL && nconf.get('MONGO_URL')) {
38 process.env.MONGO_URL = nconf.get('MONGO_URL')
39 }
40
41 // Copy stuff required in Derby-part and vendor libs into ENV
42 if (isArray(nconf.get('COPY_TO_ENV'))) {
43 each(nconf.get('COPY_TO_ENV'), (option) => {
44 process.env[option] = nconf.get(option)
45 })
46 }
47
48 // Copy public env vars into global.env
49 if (isArray(nconf.get('PUBLIC'))) {
50 global.env = global.env || {}
51 each(nconf.get('PUBLIC'), (option) => {
52 global.env[option] = nconf.get(option)
53 })
54 }
55}