forked from CatsMiaow/nestjs-project-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.ts
26 lines (22 loc) · 993 Bytes
/
configuration.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
import type { Config, Default, Objectype, Production } from './config.interface';
const util = {
isObject<T>(value: T): value is T & Objectype {
return value !== null && typeof value === 'object' && !Array.isArray(value);
},
merge<T extends Objectype, U extends Objectype>(target: T, source: U): T & U {
for (const key of Object.keys(source)) {
const targetValue = target[key];
const sourceValue = source[key];
if (this.isObject(targetValue) && this.isObject(sourceValue)) {
Object.assign(sourceValue, this.merge(targetValue, sourceValue));
}
}
return { ...target, ...source };
},
};
export const configuration = async (): Promise<Config> => {
const { config } = <{ config: Default }> await import(`${__dirname}/envs/default`);
const { config: environment } = <{ config: Production }> await import(`${__dirname}/envs/${process.env.NODE_ENV || 'development'}`);
// object deep merge
return util.merge(config, environment);
};