UNPKG

950 BJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5
6const { loadMulti } = require('./utils/fs/multiLoad');
7
8const clone = obj => JSON.parse(JSON.stringify(obj));
9
10const load = (env, configDir, base = {}, override = {}) => ({
11 ...clone(base),
12 ...loadMulti(configDir, 'config'),
13 ...loadMulti(configDir, `config.${env}`),
14 ...clone(override),
15});
16
17const 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
25const 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
37module.exports = {
38 load,
39 merge,
40};