-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
192 lines (178 loc) · 5.25 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
/**
* 载入配置文件
*/
var path = require('path');
var fs = require('fs');
var createNamespace = require('lei-ns').Namespace;
var ConfigFromPath = require('./project_version').ConfigFromPath;
var debug = require('debug')('super-config:main');
/**
* 创建SuperConfig实例
*
* @param {Object} options
*/
function SuperConfig (options) {
this._cacheConfig = {};
this._cacheProjects = null;
this._cachePaths = {};
this._cacheEnvConfig = null;
if (options) this.setOption(options);
}
/**
* 设置
*
* @param {Object} options
* - {String} envName 用于获取当前版本的环境变量名,默认APP_VERSION
* - {String} envConfigName 用于获取当前额外配置信息的环境变量名,默认APP_CONFIG
* - {String} project 项目名,默认无
* - {String} paths 当前配置文件目录,默认为['./config'],依次从这些目录中加载配置文件
* - {String} commonProject 公共配置项目名,默认_common
* - {String} commonVersion 公共配置版本名,默认_common
* - {Boolean} disableCache 是否关闭缓存,如果关闭了则每次getProject()时均重新读取文件,默认false
*/
SuperConfig.prototype.setOption = function (options) {
this._options = options = options || {};
this._envName = options.envName || 'APP_VERSION';
this._envConfigName = options.envConfigName || 'APP_CONFIG';
this._commonProject = options.commonProject || '_common';
this._commonVersion = options.commonVersion || '_common';
this._disableCache = !!options.disableCache;
this._paths = options.paths || ['./config'];
if (!Array.isArray(this._paths)) {
if (typeof this._paths === 'string') {
this._paths = [this._paths];
} else {
throw new Error('`paths` must be an array');
}
}
this._paths = this._paths.map(function (f) {
return path.resolve(f);
});
if (options.project) {
this.setProject(options.project);
}
this.getEnvConfig();
};
SuperConfig.prototype._resolvePaths = function (project, version) {
var hasVersionParam = !!version;
version = version || this._commonVersion;
var key = project + ':' + version;
if (this._disableCache || !this._cachePaths[key]) {
var versions = [];
versions.push([this._commonProject, this._commonVersion]);
if (hasVersionParam) versions.push([this._commonProject, version]);
versions.push([project, this._commonVersion]);
if (hasVersionParam) versions.push([project, version]);
var list = [];
this._paths.forEach(function (dir) {
versions.forEach(function (item) {
var f = path.resolve(dir, item[0], item[1]) + '.js';
if (fs.existsSync(f)) {
list.push(f);
}
});
});
this._cachePaths[key] = list;
}
return this._cachePaths[key];
};
/**
* 取得所有项目列表
*
* @return {Array}
*/
SuperConfig.prototype.projects = function () {
function readdir (dir) {
return fs.readdirSync(dir).map(function (n) {
var f = path.resolve(dir, n);
var s = fs.statSync(f);
return s.isDirectory() ? n : false;
}).filter(function (n) {
return n;
});
}
function addTo (obj, list) {
list.forEach(function (n) {
obj[n] = true;
});
}
if (!this._cacheProjects) {
var names = {};
this._paths.forEach(function (dir) {
addTo(names, readdir(dir));
});
delete names[this._commonProject];
this._cacheProjects = Object.keys(names);
Object.freeze(this._cacheProjects);
}
return this._cacheProjects.slice();
};
/**
* 获取环境变量提供的额外配置信息
*
* @return {Object}
*/
SuperConfig.prototype.getEnvConfig = function () {
if (this._disableCache || !this._cacheEnvConfig) {
var json = process.env[this._envConfigName];
if (json) {
try {
this._cacheEnvConfig = JSON.parse(json);
} catch (err) {
throw new Error('JSON parse `env.' + this._cacheEnvConfig + '` error:\n' + json);
}
} else {
this._cacheEnvConfig = {};
}
Object.freeze(this._cacheEnvConfig);
}
return this._cacheEnvConfig;
};
/**
* 设置当前的项目
*
* @param {String} name
* @param {String} version
*/
SuperConfig.prototype.setProject = function (name, version) {
this._project = name;
this._version = version || process.env[this._envName];
if (!this._version) {
throw new Error('cannot get version name from `env.' + this._envName + '`');
}
return this.getProject(this._project, this._version);
};
/**
* 取指定项目版本配置
*
* @param {String} project
* @param {String} version
* @return {Object}
*/
SuperConfig.prototype.getProject = function (project, version) {
version = version || this._version;
var key = project + ':' + version;
if (this._disableCache || !this._cacheConfig[key]) {
this._cacheConfig[key] = new ConfigFromPath(this, project, version);
}
return this._cacheConfig[key];
};
/**
* 获取指定项目的版本列表
*
* @param {String} project
* @return {Array}
*/
SuperConfig.prototype.versions = function (project) {
project = project || this._project;
return this.getProject(project).get('versions').slice();
};
/**
* 返回当前项目版本配置
*
* @return {Object}
*/
SuperConfig.prototype.current = function () {
return this.getProject(this._project, this._version);
};
module.exports = SuperConfig;