-
Notifications
You must be signed in to change notification settings - Fork 567
/
configService.ts
270 lines (241 loc) 路 7.23 KB
/
configService.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { basename, join } from 'path';
import { IConfigService, MidwayAppInfo, ScopeEnum } from '../interface';
import { safelyGet } from '../util';
import { readdirSync, statSync } from 'fs';
import * as util from 'util';
import { MidwayEnvironmentService } from './environmentService';
import { MidwayInformationService } from './informationService';
import { extend } from '../util/extend';
import { MidwayInvalidConfigError } from '../error';
import { Init, Inject, Provide, Scope } from '../decorator';
import { Types } from '../util/types';
const debug = util.debuglog('midway:debug');
interface ConfigMergeInfo {
value: any;
env: string;
extraPath?: string;
}
@Provide()
@Scope(ScopeEnum.Singleton)
export class MidwayConfigService implements IConfigService {
private envDirMap: Map<string, Set<any>> = new Map();
private aliasMap = {
prod: 'production',
unittest: 'test',
};
private configMergeOrder: Array<ConfigMergeInfo> = [];
protected configuration = {};
protected isReady = false;
protected externalObject: Record<string, unknown>[] = [];
protected appInfo: MidwayAppInfo;
protected configFilterList: Array<
(config: Record<string, any>) => Record<string, any> | undefined
> = [];
@Inject()
protected environmentService: MidwayEnvironmentService;
@Inject()
protected informationService: MidwayInformationService;
@Init()
protected init() {
this.appInfo = {
pkg: this.informationService.getPkg(),
name: this.informationService.getProjectName(),
baseDir: this.informationService.getBaseDir(),
appDir: this.informationService.getAppDir(),
HOME: this.informationService.getHome(),
root: this.informationService.getRoot(),
env: this.environmentService.getCurrentEnvironment(),
};
}
public add(configFilePaths: any[]) {
for (const dir of configFilePaths) {
if (typeof dir === 'string') {
if (/\.\w+$/.test(dir)) {
// file
const env = this.getConfigEnv(dir);
const envSet = this.getEnvSet(env);
envSet.add(dir);
if (this.aliasMap[env]) {
this.getEnvSet(this.aliasMap[env]).add(dir);
}
} else {
// directory
const fileStat = statSync(dir);
if (fileStat.isDirectory()) {
const files = readdirSync(dir);
this.add(
files.map(file => {
return join(dir, file);
})
);
}
}
} else {
// object add
for (const env in dir) {
this.getEnvSet(env).add(dir[env]);
if (this.aliasMap[env]) {
this.getEnvSet(this.aliasMap[env]).add(dir[env]);
}
}
}
}
}
public addObject(obj: Record<string, unknown>, reverse = false) {
if (this.isReady) {
obj = this.runWithFilter(obj);
if (!obj) {
debug('[config]: Filter config and got undefined will be drop it');
return;
}
this.configMergeOrder.push({
env: 'default',
extraPath: '',
value: obj,
});
if (reverse) {
this.configuration = extend(true, obj, this.configuration);
} else {
extend(true, this.configuration, obj);
}
} else {
this.externalObject.push(obj);
}
}
private getEnvSet(env) {
if (!this.envDirMap.has(env)) {
this.envDirMap.set(env, new Set());
}
return this.envDirMap.get(env);
}
private getConfigEnv(configFilePath) {
// parse env
const configFileBaseName = basename(configFilePath);
const splits = configFileBaseName.split('.');
const suffix = splits.pop();
if (suffix !== 'js' && suffix !== 'ts') {
return suffix;
}
return splits.pop();
}
public load() {
if (this.isReady) return;
// get default
const defaultSet = this.getEnvSet('default');
// get current set
const currentEnvSet = this.getEnvSet(
this.environmentService.getCurrentEnvironment()
);
// merge set
const target = {};
const defaultSetLength = defaultSet.size;
for (const [idx, filename] of [...defaultSet, ...currentEnvSet].entries()) {
let config: Record<string, any> = this.loadConfig(filename);
if (Types.isFunction(config)) {
// eslint-disable-next-line prefer-spread
config = config.apply(null, [this.appInfo, target]);
}
if (!config) {
continue;
}
config = this.runWithFilter(config);
if (!config) {
debug('[config]: Filter config and got undefined will be drop it');
continue;
}
if (typeof filename === 'string') {
debug('[config]: Loaded config %s, %j', filename, config);
} else {
debug('[config]: Loaded config %j', config);
}
this.configMergeOrder.push({
env:
idx < defaultSetLength
? 'default'
: this.environmentService.getCurrentEnvironment(),
extraPath: filename,
value: config,
});
extend(true, target, config);
}
if (this.externalObject.length) {
for (let externalObject of this.externalObject) {
if (externalObject) {
externalObject = this.runWithFilter(externalObject);
if (!externalObject) {
debug('[config]: Filter config and got undefined will be drop it');
continue;
}
debug('[config]: Loaded external object %j', externalObject);
extend(true, target, externalObject);
this.configMergeOrder.push({
env: 'default',
extraPath: '',
value: externalObject,
});
}
}
}
this.configuration = target;
this.isReady = true;
}
public getConfiguration(configKey?: string) {
if (configKey) {
return safelyGet(configKey, this.configuration);
}
return this.configuration;
}
public getConfigMergeOrder(): Array<ConfigMergeInfo> {
return this.configMergeOrder;
}
private loadConfig(
configFilename
): (...args) => any | Record<string, unknown> {
let exports =
typeof configFilename === 'string'
? require(configFilename)
: configFilename;
// if es module
if (exports && exports.__esModule) {
if (exports && exports.default) {
if (Object.keys(exports).length > 1) {
throw new MidwayInvalidConfigError(
`${configFilename} should not have both a default export and named export`
);
}
exports = exports.default;
}
}
return exports;
}
public clearAllConfig() {
this.configuration = {};
}
public clearConfigMergeOrder() {
this.configMergeOrder.length = 0;
}
/**
* add a config filter
* @param filter
*/
public addFilter(
filter: (config: Record<string, any>) => Record<string, any>
) {
this.configFilterList.push(filter);
}
protected runWithFilter(config: Record<string, any>): Record<string, any> {
for (const filter of this.configFilterList) {
debug(
`[config]: Filter config by filter = "${
filter.name || 'anonymous filter'
}"`
);
config = filter(config);
debug('[config]: Filter config result = %j', config);
}
return config;
}
public getAppInfo() {
return this.appInfo;
}
}