1 | # figurecon
|
2 | #### Simple configuration module with real-time updates
|
3 |
|
4 | [![install size](https://packagephobia.com/badge?p=@rngnrs/figurecon)](https://packagephobia.com/result?p=@rngnrs/figurecon)
|
5 | [![open issues](https://img.shields.io/github/issues/rngnrs/figurecon)](https://github.com/rngnrs/figurecon/issues)
|
6 | [![GitHub license](https://img.shields.io/github/license/rngnrs/figurecon)](https://github.com/rngnrs/figurecon/blob/master/LICENSE)
|
7 |
|
8 | ## Description
|
9 | This module is designed as a simple ES6-based advanced config module.
|
10 |
|
11 | **WARNING: Be sure not to use this package in production.**
|
12 |
|
13 | **It is unreliable because not tested properly.**
|
14 |
|
15 | **Any method can be changed/removed in any time, so use it carefully!**
|
16 |
|
17 | ## Features
|
18 | - Updates values on-the-fly (though [it can be disabled](#disable-watcher-completely))
|
19 | - May use defaults if there is no such value in main config
|
20 | - Supports update subscriptions (hooks)
|
21 |
|
22 | ## Installation
|
23 | ```shell script
|
24 | npm i @rngnrs/figurecon
|
25 |
|
26 | yarn add @rngnrs/figurecon
|
27 | ```
|
28 |
|
29 | ## Usage
|
30 |
|
31 | ### Import the module
|
32 |
|
33 | #### [modern style] With config object, ES6 modules
|
34 | ```js
|
35 | import Figurecon from '@rngnrs/figurecon';
|
36 |
|
37 | const configObject = {
|
38 | 'key': 'value'
|
39 | }
|
40 |
|
41 | const defaults = {
|
42 | 'your.config.here': true
|
43 | };
|
44 |
|
45 | const config = new Figurecon(configObject, defaults);
|
46 | // ...
|
47 | ```
|
48 |
|
49 | #### [modern style] With config file, ES6 modules
|
50 | ```js
|
51 | import { fileURLToPath } from 'url';
|
52 | import { dirname, resolve } from 'path';
|
53 | import Figurecon from '@rngnrs/figurecon';
|
54 |
|
55 | const __filename = fileURLToPath(import.meta.url);
|
56 | const __dirname = dirname(__filename);
|
57 |
|
58 | const defaults = {
|
59 | 'your.config.here': true
|
60 | };
|
61 |
|
62 | const config = new Figurecon(resolve(__dirname, "./config.json"), defaults);
|
63 | // ...
|
64 | ```
|
65 |
|
66 | #### With config file
|
67 | ```js
|
68 | const path = require('path');
|
69 | const Figurecon = require('@rngnrs/figurecon');
|
70 | const defaults = {
|
71 | 'your.config.here': true
|
72 | };
|
73 |
|
74 | const config = new Figurecon(path.resolve(__dirname, "./config.json"), defaults);
|
75 | // ...
|
76 | ```
|
77 |
|
78 | #### With config object
|
79 | ```js
|
80 | const Figurecon = require('@rngnrs/figurecon');
|
81 |
|
82 | const configObject = {
|
83 | 'key': 'value'
|
84 | }
|
85 |
|
86 | const defaults = {
|
87 | 'your.config.here': true
|
88 | };
|
89 |
|
90 | const config = new Figurecon(configObject, defaults);
|
91 | // ...
|
92 | ```
|
93 |
|
94 | ### Get a value
|
95 | **Breaking:** Versions below 1.0.0 could get a value via `config(key, defaults)`.
|
96 | Due to ES6 class rewrite, Figurecon does not support this behaviour:
|
97 | use `config.get(key, defaults)` instead.
|
98 | ```js
|
99 | //...
|
100 | let value1 = config.get('your.config.here'); // => true
|
101 | let value2 = config.get('non.existent.key', 'default'); // => 'default'
|
102 | let value3 = config.get('non.existent.key'); // => undefined
|
103 | ```
|
104 |
|
105 | ### Set a value
|
106 | ```js
|
107 | // ...
|
108 | let isSet1 = config.set('non.existent.key'); // => true, because `undefined` is value too
|
109 | let isSet2 = config.set('non.existent.key', 111); // => true
|
110 | let isSet3 = config.set('non.existent.key', 111); // => false, because already has this value
|
111 | let isSet4 = config.set('non.existent.key', 222); // => true, because another value
|
112 | ```
|
113 |
|
114 | ### Subcriptions (hooks)
|
115 | ```js
|
116 | // ...
|
117 | let func = () => { /* ... */ };
|
118 |
|
119 | // Subscribe to `key` updates
|
120 | let hook = config.on('key', func);
|
121 |
|
122 | // Usually it is an update from config file...
|
123 | // but now we create it directly
|
124 | config.set('key', true);
|
125 |
|
126 | // Unsubscribe
|
127 | // Also you could use `hook` because it returns `func`
|
128 | config.off('key', func);
|
129 | ```
|
130 |
|
131 | ### Use watcher for real-time updates
|
132 | Figurecon uses real-time updates via tiny package called `node-watch` by default.
|
133 | If you want change a library, just use a wrapper that implements such interface:
|
134 | ```js
|
135 | // ...
|
136 | let WatchLib = require('node-watch'); // or any other
|
137 | let watcher = function (fileName, callback = async (eventType = "update", fileName) => {}) {
|
138 | return WatchLib(fileName, callback);
|
139 | };
|
140 | config.watch(watcher);
|
141 | // when you do not need it, use .unwatch for correct process exiting:
|
142 | config.unwatch();
|
143 | ```
|
144 | If watcher already has this interface, just use this:
|
145 | ```js
|
146 | let WatchLib = require('node-watch');
|
147 | const config = new Figurecon(configObject, defaults, {
|
148 | watch: true, // now by default!
|
149 | watcher: WatchLib
|
150 | });
|
151 | ```
|
152 | Otherwise, use `config.reload()` to do this job manually.
|
153 |
|
154 | ### Disable watcher (completely)
|
155 | If you want to disable updates, put `{ watch: false } ` in `options`:
|
156 | ```js
|
157 | const config = new Figurecon(configObject, defaults, {
|
158 | watch: false
|
159 | });
|
160 | ```
|