UNPKG

1.73 kBJavaScriptView Raw
1/**
2 * @file @creativefeather/config/src/index.js
3 * @author creativefeather <creativefeather@outlook.com>
4 * @license MIT
5 *
6 * @Symbols:
7 * Symbol.for('config module root')
8 * Symbol.for('config options')
9 * Symbol.for('utils')
10 */
11const path = require('path'),
12 { getConfig } = require('./lib');
13
14const MODULE_ROOT = path.join(__dirname, '../');
15const debug_i = require('./lib/debug-logger')('index');
16
17let utils = Object.create(null);
18let _isConfigFrozen = (() => {
19
20})();
21
22/**
23 * Gets the configuration specified from options
24 * @param {string | object} options
25 */
26utils.get = (options) => {
27 let config = getConfig(null, options);
28
29 config[Symbol.for('utils')] = utils;
30 debug_i('Symbol.for(utils)');
31
32 return config;
33};
34
35/**
36 * Initiates the config module, freezing the values.
37 * @param {object} options
38 */
39utils.use = (options) => {
40 // TODO: Make a way to lock/freeze config
41 if (_isConfigFrozen) {
42 debug_i.error('module is already initiated!')
43 }
44
45 let config = utils.get(options);
46
47 // Because the module is potentially 'redefined' when use is called, we'll differentiate between when the module is initializing the first time and when it has already initialized.
48 if (module.loaded) {
49 require.cache[module.filename] = config;
50 }
51 else {
52 module.exports = config;
53 }
54
55 return config;
56};
57
58utils.moduleRoot = () => {
59 return MODULE_ROOT;
60}
61
62// *** End of utils definitions ***
63Object.freeze(utils);
64// *** End of utils definitions ***
65
66
67
68// *** Exports ***
69if (!process.env.CONFIG_PREVENT_DEFAULT) {
70 utils.use();
71}
72else {
73 debug_i('Symbol.for(utils)');
74 module.exports[Symbol.for('utils')] = utils;
75}
\No newline at end of file