1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 |
|
6 | const { loadMulti } = require('./utils/fs/multiLoad');
|
7 |
|
8 | const clone = obj => JSON.parse(JSON.stringify(obj));
|
9 |
|
10 | const load = (env, configDir, base = {}, override = {}) => ({
|
11 | ...clone(base),
|
12 | ...loadMulti(configDir, 'config'),
|
13 | ...loadMulti(configDir, `config.${env}`),
|
14 | ...clone(override),
|
15 | });
|
16 |
|
17 | const mergeSection = (configDir, section, main) => {
|
18 | if (!(section in main))
|
19 | return main;
|
20 |
|
21 | main[section] = load({}, configDir, main[section]);
|
22 | return main;
|
23 | };
|
24 |
|
25 | const merge = (configDir, section, main, subdirs = false) => {
|
26 | if (!subdirs)
|
27 | return mergeSection(configDir, section, main);
|
28 |
|
29 | main[section] = fs.readdirSync(configDir).reduce((sectioncConfig, subsection) => {
|
30 | const subsectionDir = path.join(configDir, subsection);
|
31 | return mergeSection(subsectionDir, subsection, sectioncConfig);
|
32 | }, main[section] || {});
|
33 |
|
34 | return main;
|
35 | };
|
36 |
|
37 | module.exports = {
|
38 | load,
|
39 | merge,
|
40 | };
|