UNPKG

4.29 kBMarkdownView Raw
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
9This 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
24npm i @rngnrs/figurecon
25
26yarn add @rngnrs/figurecon
27```
28
29## Usage
30
31### Import the module
32
33#### [modern style] With config object, ES6 modules
34```js
35import Figurecon from '@rngnrs/figurecon';
36
37const configObject = {
38 'key': 'value'
39}
40
41const defaults = {
42 'your.config.here': true
43};
44
45const config = new Figurecon(configObject, defaults);
46// ...
47```
48
49#### [modern style] With config file, ES6 modules
50```js
51import { fileURLToPath } from 'url';
52import { dirname, resolve } from 'path';
53import Figurecon from '@rngnrs/figurecon';
54
55const __filename = fileURLToPath(import.meta.url);
56const __dirname = dirname(__filename);
57
58const defaults = {
59 'your.config.here': true
60};
61
62const config = new Figurecon(resolve(__dirname, "./config.json"), defaults);
63// ...
64```
65
66#### With config file
67```js
68const path = require('path');
69const Figurecon = require('@rngnrs/figurecon');
70const defaults = {
71 'your.config.here': true
72};
73
74const config = new Figurecon(path.resolve(__dirname, "./config.json"), defaults);
75// ...
76```
77
78#### With config object
79```js
80const Figurecon = require('@rngnrs/figurecon');
81
82const configObject = {
83 'key': 'value'
84}
85
86const defaults = {
87 'your.config.here': true
88};
89
90const 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)`.
96Due to ES6 class rewrite, Figurecon does not support this behaviour:
97use `config.get(key, defaults)` instead.
98```js
99//...
100let value1 = config.get('your.config.here'); // => true
101let value2 = config.get('non.existent.key', 'default'); // => 'default'
102let value3 = config.get('non.existent.key'); // => undefined
103```
104
105### Set a value
106```js
107// ...
108let isSet1 = config.set('non.existent.key'); // => true, because `undefined` is value too
109let isSet2 = config.set('non.existent.key', 111); // => true
110let isSet3 = config.set('non.existent.key', 111); // => false, because already has this value
111let isSet4 = config.set('non.existent.key', 222); // => true, because another value
112```
113
114### Subcriptions (hooks)
115```js
116// ...
117let func = () => { /* ... */ };
118
119// Subscribe to `key` updates
120let hook = config.on('key', func);
121
122// Usually it is an update from config file...
123// but now we create it directly
124config.set('key', true);
125
126// Unsubscribe
127// Also you could use `hook` because it returns `func`
128config.off('key', func);
129```
130
131### Use watcher for real-time updates
132Figurecon uses real-time updates via tiny package called `node-watch` by default.
133If you want change a library, just use a wrapper that implements such interface:
134```js
135// ...
136let WatchLib = require('node-watch'); // or any other
137let watcher = function (fileName, callback = async (eventType = "update", fileName) => {}) {
138 return WatchLib(fileName, callback);
139};
140config.watch(watcher);
141// when you do not need it, use .unwatch for correct process exiting:
142config.unwatch();
143```
144If watcher already has this interface, just use this:
145```js
146let WatchLib = require('node-watch');
147const config = new Figurecon(configObject, defaults, {
148 watch: true, // now by default!
149 watcher: WatchLib
150});
151```
152Otherwise, use `config.reload()` to do this job manually.
153
154### Disable watcher (completely)
155If you want to disable updates, put `{ watch: false } ` in `options`:
156```js
157const config = new Figurecon(configObject, defaults, {
158 watch: false
159});
160```