-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
92 lines (71 loc) 路 3.04 KB
/
index.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
import * as path from "path";
import * as _ from "lodash";
import { Inject, Service, Container } from "@fullstack-one/di";
import { DefaultConfigNotFoundError } from "./errors";
import ConfigMergeHelper from "./helpers/ConfigMergeHelper";
import EnvironmentBuilder from "./helpers/EnvironmentBuilder";
import { IEnvironment } from "./IEnvironment";
export { IEnvironment };
@Service()
export class Config {
private registeredConfigModules: string[] = [];
private applicationConfig: any = {};
private config: any = {};
private readonly NODE_ENV = process.env.NODE_ENV;
public readonly ENVIRONMENT: IEnvironment;
constructor() {
this.applicationConfig = this.loadApplicationConfig();
this.registerConfig("Config", `${__dirname}/../config`);
const namespace = this.config.Config.namespace;
this.ENVIRONMENT = EnvironmentBuilder.buildEnvironment(this.NODE_ENV, namespace);
Container.set("ENVIRONMENT", JSON.parse(JSON.stringify(this.ENVIRONMENT)));
}
private loadApplicationConfig(): object {
const applicationConfigFolderPath = `${path.dirname(require.main.filename)}/config`;
return this.getConfigFromConfigFiles(applicationConfigFolderPath);
}
private getConfigFromConfigFiles(configDirectory: string): object {
const defaultConfigPath = `${configDirectory}/default.js`;
const environmentConfigPath = `${configDirectory}/${this.NODE_ENV}.js`;
let defaultConfig: object;
try {
defaultConfig = require(defaultConfigPath);
} catch (err) {
throw new DefaultConfigNotFoundError(`config.default.loading.error.not.found: ${defaultConfigPath} \n ${err}`);
}
let environmentConfig: object;
try {
environmentConfig = require(environmentConfigPath);
} catch (err) {
environmentConfig = {};
}
return _.defaultsDeep(environmentConfig, defaultConfig);
}
private applyConfigModule(name: string, baseConfigModule: object): void {
if (name in this.config) return;
const applicationConfigOfModule = this.applicationConfig[name] || {};
const processEnvironmentConfigOfModule = ConfigMergeHelper.getProcessEnvironmentConfig(name);
const configModule = _.defaultsDeep(processEnvironmentConfigOfModule, applicationConfigOfModule, baseConfigModule);
ConfigMergeHelper.checkForMissingConfigProperties(name, configModule);
this.config[name] = configModule;
}
public registerConfig(name: string, configDirectory: string): any {
const baseConfigModule = this.getConfigFromConfigFiles(configDirectory);
this.applyConfigModule(name, baseConfigModule);
return this.getConfig(name);
}
public registerApplicationConfigModule(name: string): any {
const baseConfigModule = {};
this.applyConfigModule(name, baseConfigModule);
return this.getConfig(name);
}
public getConfig(name: string): any {
if (!_.has(this.config, name)) {
throw new Error(`config.module.not.found module name: ${name}`);
}
return _.cloneDeep(this.config[name]);
}
public dangerouslyGetWholeConfig(): any {
return _.cloneDeep(this.config);
}
}