Skip to content

raravel/cfg-lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cfg Lite

Configration or data file encryption is cumbersome, but it is essential.

CfgLite makes this behavior easy.

Encrypts all files with different values.

Install

npm

npm install cfg-lite

yarn

yarn add cfg-lite

Usage

File Init

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');

Get

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

Set

/*
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
				},
			},
		},
	}
}
*/

Merge

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
	}
}
*/

Delete

/*
cfg data
{
	key1: 'hello'
}
*/

cfg.delete('key1');

/*
result cfg data
{
}
*/

Or you can clear all values.

cfg.deleteAll();

Save

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

Result

{
	"a": {
		"b": {
			"c": "Hello"
		},
		"c": 55
	},
	"c": {
		"d": {
			"e": "Hello"
		}
	},
	"d": true
}

Command line

Global install

npm install -g cfg-lite

Decoding commed

cfg [cfg file] [key]

Thanks.

About

En/Decrypt json type config file.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published