-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (149 loc) · 6.09 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
process.env.SUPPRESS_NO_CONFIG_WARNING = true;
var fs = require('fs');
var path = require('path');
var through = require('through2');
var gutil = require('gulp-util');
var glob = require('glob');
var stream = require('stream');
var series = require('stream-series');
var config = require('config');
var _ = require('lodash');
var AVAILABLE_FORMATS = ['json', 'json5', 'hjson', 'toml', 'iced', 'yaml', 'yml', 'cson', 'properties'];
function Config() {}
Config.prototype.load = function compile(publicPath, localPath, params) {
if (typeof localPath === 'object') {
params = localPath;
localPath = undefined;
}
if (typeof publicPath === 'object') {
params = publicPath;
publicPath = undefined;
}
params = params || {};
publicPath = publicPath || 'config';
params.env = params.env || process.env.NODE_ENV;
params.project = params.project || process.env.PROJECT;
var setDefaultConfig = typeof params.defaults === 'function' ? params.defaults : function(env, projectName) {
return params.defaults || {env: env, project: projectName};
};
var def = params.defaultFileName || 'default',
env = params.env || def,
project = params.project === '*' ? undefined : params.project;
var publicDirPaths = publicPath ? glob.sync(publicPath, params) : [];
var localDirPaths = localPath ? glob.sync(localPath, params) : [];
var publicConfigs = findConfigs(publicDirPaths);
var localConfigs = findConfigs(localDirPaths, {ignoreErrors: true});
var configs = _.merge(this, publicConfigs, localConfigs);
//Stay config for one project
if (project) {
_.forEach(configs, function(config, projectName) {
if (projectName !== project) {
delete configs[projectName];
}
})
}
function findConfigs(directoriesPaths, params) {
var configs = {};
directoriesPaths.forEach(function(dirPath) {
loadConfigs(dirPath, path.basename(path.dirname(dirPath)), configs);
});
return combineConfigs(configs, params);
}
function loadConfigs(dirPath, project, configs) {
var dirFilesPaths = fs.readdirSync(dirPath, 'utf8');
dirFilesPaths.forEach(function(fileName) {
var filePath = path.join(dirPath, fileName);
if (fs.lstatSync(filePath).isDirectory()) {
loadConfigs(filePath, path.basename(filePath), configs);
} else {
var env = path.basename(fileName, path.extname(fileName));
configs[project] = configs[project] || {};
configs[project][env] = readConfig(filePath);
}
});
}
function readConfig(filePath) {
var ext = path.extname(filePath).substring(1);
if (AVAILABLE_FORMATS.indexOf(ext) === -1) return;
var file = fs.readFileSync(filePath, 'utf8');
return config.util.parseString(file, ext);
}
function combineConfigs(configs, params) {
params = params || {};
var inheritedConfigs = {};
_.forEach(configs, function(config, projectName) {
var defaultConfig = config[def];
var enviromentConfig = config[env];
if (defaultConfig && defaultConfig.inherit) {
inheritedConfigs[projectName] = defaultConfig.inherit;
}
if (!params.ignoreErrors && !enviromentConfig && (!defaultConfig || !defaultConfig.inherit)) {
if (!defaultConfig.useIfNoEnv) {
throw new Error('Can not read "' + env + '" config for project "' + projectName +'"');
}
}
var projectConfig = _.merge({}, defaultConfig, enviromentConfig);
var defaultProjectConfig = setDefaultConfig.call(projectConfig, env, projectName);
configs[projectName] = _.merge(defaultProjectConfig, projectConfig);
});
_.forEach(inheritedConfigs, function(parentProjectName, projectName) {
if (!configs[parentProjectName]) {
throw new Error('Can not load ' + parentProjectName + ' config');
}
configs[projectName] = _.merge({}, configs[parentProjectName], configs[projectName]);
});
return configs;
}
return this;
};
Config.prototype.stream = function compile(params) {
params = params || {};
var stream = through.obj(),
config = this,
projectConfigs = {};
function save(projectConfig, projectName) {
var file = new gutil.File({
path: path.join(projectName || '', params.name || 'config.js'),
contents: new Buffer(JSON.stringify(projectConfig, null, params.stringifySpace || 4))
});
stream.write(file);
}
_.forEach(config, function(projectConfig, project) {
projectConfigs[project] = _.get(projectConfig, params.section);
});
save(projectConfigs);
process.nextTick(function() {
return stream.end();
});
return stream;
};
Config.prototype.forEach = function(section, callback) {
var streams = [];
if (!callback) {
callback = section;
section = undefined;
}
_.forEach(this, function(configs, projectName) {
if (section) {
configs = _.get(configs, section);
} else {
configs = configs || {};
}
if (!configs) return;
configs = configs instanceof Array ? configs : [configs];
configs.forEach(function(config) {
var configStream = callback(config, projectName);
if (configStream instanceof stream.Stream) {
streams.push(configStream);
}
});
});
return streams.length ? series.apply(this, streams) : null;
};
Config.prototype.reduce = function(callback, initial, thisArg, right) {
return right ? _.reduceRight(this, callback, initial) : _.reduce(this, callback, initial);
};
Config.prototype.reduceRight = function(callback, initial, thisArg) {
return this.reduce(callback, initial, thisArg, true);
};
module.exports = new Config();