-
Notifications
You must be signed in to change notification settings - Fork 2
/
loader-config.ts
57 lines (49 loc) · 1.29 KB
/
loader-config.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
import * as glob from 'glob';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as d from 'debug';
const debug = d('Gabliam:loader-config');
export class LoaderConfig {
/**
* Load configuration
* @param {string} folder the configuration folder
* @returns any
*/
load(folder: string, profile = process.env.PROFILE || null): any {
debug('loadConfig', folder);
const files = glob.sync('**/application?(-+([a-zA-Z])).yml', {
cwd: folder
});
let config = {};
if (!files || files.length === 0) {
return config;
}
const defaultProfileFile = files.find(file => file === 'application.yml');
if (defaultProfileFile) {
config = this.loadYmlFile(`${folder}/${defaultProfileFile}`);
}
if (profile) {
const profileFile = files.find(
file => file === `application-${profile}.yml`
);
if (profileFile) {
config = _.merge(
{},
config,
this.loadYmlFile(`${folder}/${profileFile}`)
);
}
}
debug('loadConfig', config);
return config;
}
private loadYmlFile(ymlPath: string) {
const data = fs.readFileSync(ymlPath, 'utf8');
try {
return yaml.load(data) || {};
} catch (e) {
return {};
}
}
}