-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetConfigs.js
61 lines (53 loc) · 2.02 KB
/
getConfigs.js
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
'use strict';
var path = require('path'),
_ = require('lodash'),
BB = require('bluebird'),
fs = BB.promisifyAll(require('fs')),
chalk = require('chalk');
module.exports = getConfigs;
function getConfigs(nodeEnv, configsDir, verbose) {
var vars = null;
return BB
.try(function() {
var varsPath = path.join(configsDir, 'vars', nodeEnv + '.json');
vars = require(varsPath);
vars.env = nodeEnv;
verbose && console.log(chalk.green('> building configs:'), configsDir);
})
.catch(function(error) {
console.log(chalk.red('> configuration building error:\n'), error);
console.log(chalk.yellow('> if you are not using configs, ignore the error above'));
console.log(chalk.yellow('> returning empty object'));
vars = {};
})
.then(function() {
return fs.readdirAsync(configsDir);
})
.map(function(fileName) {
fileName = path.join(configsDir, fileName);
verbose && console.log(chalk.green('> reading file:'), fileName);
if ('.json' === path.extname(fileName)) {
return fs.readFileAsync(fileName);
}
})
.then(function(contents) {
return _.compact(contents);
})
.map(function(content) {
return JSON.parse(_.template(content)(vars));
})
.reduce(function (configs, config) {
return _.extend(configs, config);
}, {})
.then(function(configs) {
verbose && console.log(chalk.green('> node env:'), nodeEnv);
configs.env = nodeEnv;
return configs;
})
.catch(function(error) {
console.log(chalk.red('> configuration templating error:\n', error));
console.log(chalk.yellow('> if you are not using configs, ignore the error above'));
console.log(chalk.yellow('> returning empty object'));
return {};
});
}