UNPKG

1.67 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('utils')
8 *
9 * Environment Variables
10 * CONFIG_PREVENT_DEFAULT : boolean
11 */
12const path = require('path'),
13 { getConfig } = require('./lib');
14
15const MODULE_ROOT = path.join(__dirname, '../');
16const debug_i = require('./lib/debug-logger')('index');
17
18let utils = Object.create(null);
19let _isConfigFrozen = (() => {
20
21})();
22
23/**
24 * Gets the configuration specified from options
25 * @param {string | object} options
26 */
27utils.get = (options) => {
28 let config = getConfig(null, options);
29
30 config[Symbol.for('utils')] = utils;
31 debug_i('Symbol.for(utils)');
32 debug_i(`moduleRoot: ${config('#moduleRoot')}`);
33
34 return config;
35};
36
37/**
38 * Initiates the config module, freezing the values.
39 * @param {object} options
40 */
41utils.use = (options) => {
42 // TODO: Make a way to lock/freeze config
43 if (_isConfigFrozen) {
44 debug_i.error('module is already initiated!')
45 }
46
47 let config = utils.get(options);
48
49 // 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.
50 if (module.loaded) {
51 require.cache[module.filename] = config;
52 }
53 else {
54 module.exports = config;
55 }
56
57 return config;
58};
59
60utils.moduleRoot = () => {
61 return MODULE_ROOT;
62}
63
64// *** End of utils definitions ***
65Object.freeze(utils);
66// *** End of utils definitions ***
67
68utils.use({
69 preventDefault: !!process.env.CONFIG_PREVENT_DEFAULT
70});
\No newline at end of file