-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
156 lines (136 loc) · 4.78 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var path = require('path');
var ensure = require('file-ensure');
var yaml = require('js-yaml');
var fs = require('fs');
var _ = require('lodash');
var Reconfig = require('reconfig');
var appRoot = require('app-root-path').path;
var appPkgConf = require(appRoot + '/package.json');
var appName = appPkgConf.name.replace(/ /g, '');
var configDir = path.join(appRoot, 'config');
var defaultFiles = (appPkgConf.nmConfig || {}).defaultFiles || ['base', 'dev.example', 'staging', 'live'];
var env = 'dev';
if (!_.isArray(defaultFiles)) {
console.error('nmConfig Error: nmConfig.defaultFiles MUST be an array. Please check your package.json');
process.exit();
}
/**
* Automatically creates the config dir and its needed files
* The needed file list can be customized adding a
* nmConfig.defaultFiles property in the project's package.json
*/
function boostrapConfigDir() {
!fs.existsSync(configDir) && fs.mkdirSync(configDir);
defaultFiles.forEach(function(fileName) {
var filePath = path.join(configDir, fileName + '.yml');
(!fs.existsSync(filePath)) && fs.writeFileSync(filePath, '');
});
}
/**
* Leverages on file ensure to ensure the wanted config file
*
* @param {String} fileName
* @param {String} env
*/
function ensureFile(fileName, env) {
var file = path.join(configDir, fileName);
var from = path.join(configDir, fileName.replace('yml', 'example.yml'));
var options = {};
if (fs.existsSync(from)) {
options.from = from;
}
try {
ensure(file, options);
} catch(error) {
if (env === 'dev') {
throw error;
return;
}
console.error('Cannot ensure file: ' + fileName + '. Error: ', error);
}
}
/**
* Parses the list of yaml files in the fongi directory
* combining then in a final javascript object to use
* as reconfig config parameter
*
* @param {Array} files
* @return {Object}
*/
function combineFiles(files) {
var config = { env: env };
files.forEach(function(file) {
var configFilePath = path.join(configDir, file + '.yml');
var envConfig = yaml.safeLoad(fs.readFileSync(configFilePath, 'utf8'));
config = _.mergeWith(config, envConfig, function(baseValue, envValue) {
if (_.isArray(baseValue) && _.isArray(envValue)) {
return envValue;
}
});
});
return config;
}
function combinePackageConfig(config, name) {
return (appPkgConf[name]) ? _.merge(config, appPkgConf[name]) : config;
}
boostrapConfigDir();
/**
* nmConfig main logic:
* It will parse all your yml files
* merge them and return a Reconfig instance
* for your configuration convenience.
*
* Options parameters:
*
* baseFiles: A list of files creating the base configuration
* before applying the environment specific config.
* These files will be merged in order, the env file
* will be the last applied.
*
* separator: The separator Reconfig will use for console vars
* overlays.
*
* projectName: Defines Your project's name. If none is given, the project's name
* will be inferred form your package.json "name" value. All spaces will be removed.
*
* prefix: The prefix that Reconfig will use while grabbing
* console variable and applying overlays.
* Default value: PROJECTNAME_CONFIG (all uppercase) *
*
* ensure: Tells to nmConfig to check for the existence of <filename>.yml file.
* If a <fileName>.example.yml is found, it will be used to produce
* the ensured file.
*
* env: Forced value for the environment:
* by default nmConfig will read you env form:
* - PROJECTNAME_ENV *
* - NODE_ENV
* - or default to "dev"
*
*
* @param {Object} options
* @return {Object}
*/
module.exports = function(options) {
options = options || {};
var config = {};
var files = options.baseFiles || ['base'];
var reconfigSeparator = options.separator || '_';
appName = options.projectName || appName;
var shellVars = {
env: appName.toUpperCase() + '_ENV',
reconfigPrefix: appName.toUpperCase() + '_CONFIG'
};
var reconfigPrefix = options.prefix || process.env.RECONFIG_PREFIX ||shellVars.reconfigPrefix;
env = options.env || process.env[shellVars.env]|| process.env.NODE_ENV || 'dev';
if (!_.isArray(files)) {
files = [files];
}
files.push(env);
options.ensure && ensureFile(options.ensure, env);
config = combineFiles(files);
config = combinePackageConfig(config, appName);
config.rootDir = config.basedir = appRoot;
(config.verboseConfig === true) && console.log('----\nnmConfig, loading configuration object:\n %s\n----\nYour environment config prefix is: ' + reconfigPrefix + '\n----\n', JSON.stringify(config, null, 2));
return new Reconfig(config, {envPrefix: reconfigPrefix, separator: reconfigSeparator});
}