1 | const _ = require('lodash');
|
2 | const { configFileName } = require('../consts');
|
3 | const config = require('../default.config.json');
|
4 |
|
5 | class Config {
|
6 | constructor() {
|
7 | this.config = config;
|
8 | this.merge();
|
9 | }
|
10 |
|
11 | get all() {
|
12 | return this.config;
|
13 | }
|
14 |
|
15 | get(path, defaultValue) {
|
16 | return _.get(this.config, path, defaultValue);
|
17 | }
|
18 |
|
19 | set(path, value) {
|
20 | _.set(this.config, path, value);
|
21 | }
|
22 |
|
23 | merge() {
|
24 | try {
|
25 | const userConfig = require(`${process.cwd()}/${configFileName}`);
|
26 | this.config = { ...config, ...userConfig };
|
27 | } catch (e) {
|
28 | this.config = {
|
29 | ...this.config,
|
30 | ...this.config.default,
|
31 | };
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | const configure = new Config();
|
37 |
|
38 | module.exports = configure;
|