Configration or data file encryption is cumbersome, but it is essential.
CfgLite makes this behavior easy.
Encrypts all files with different values.
npm
npm install cfg-lite
yarn
yarn add cfg-lite
If the file does not exist, it is new created. And if the file extension does not exist, .cfg
is automatically recognized.
You can specify a unique value of up to 32 characters.
import CfgLite from 'cfg-lite';
const cfg = new CfgLite('/path/to/file.cfg');
const uniqueCfg = new CfgLite('/path/to/file', 'MyUniqueKey');
All keys are separated by periods. Therefore, the key cannot contain periods.
If the value doesn't exist, it returns undefined like optional chaining.
The object is returned as a deep copy.
/*
cfg data
{
key1: 'hello',
}
*/
cfg.get('key1'); // 'hello'
cfg.get('key1.key2.key-key'); // undefined
/*
cfg data
{
}
*/
cfg.set('key1.key2', 55); // ok
cfg.set('key3.key4.key5', cfg.get('key1')); // ok
/*
result cfg data
{
key1: {
key2: 55
},
key3: {
key4: {
key5: {
key1: {
key2: 55
},
},
},
}
}
*/
Overwrite the object with new values.
/*
cfg data
{
key1: {
key2: 11,
key3: 22
}
}
*/
cfg.merge('key1', { key2: 33, key4: 55 });
/*
result cfg data
{
key1: {
key2: 33,
key3: 22,
key4: 55
}
}
*/
/*
cfg data
{
key1: 'hello'
}
*/
cfg.delete('key1');
/*
result cfg data
{
}
*/
Or you can clear all values.
cfg.deleteAll();
It is inefficient to write a file every time there is a new change. To save the changes to a file you need to use the following function.
cfg.save();
cfg.save('new.cfg'); // New save, Leave before cfg file
cfg.save('new.cfg', true); // New save, Remove before cfg file
{
"a": {
"b": {
"c": "Hello"
},
"c": 55
},
"c": {
"d": {
"e": "Hello"
}
},
"d": true
}
Global install
npm install -g cfg-lite
cfg [cfg file] [key]
Thanks.