@winm/config-helper
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

App config helper

For nodejs App to operate config file easily...

install

pnpm add @winm/config-helper

usage

create helper

new ConfigHelper([helperConfig], [initialConfig])

helperConfig's attributes:

  • format: config file format, support JSON and JSON5, Default is JSON5
  • path: config file path
  • filename: config file name
  • changeToSave: sync to disk when config changed

initialConfig is the initial value of config

import { ConfigHelper, HelperFileFormat } from './index'

const helper = new ConfigHelper({
  format: HelperFileFormat.JSON, // JSON5 is default
  path: './config', // './' is default
})

if you set changeToSave to false, you can invoke syncToFile manually to save config to disk.

init config

helper.init(initialValue, ?force)

set origin config, just for reset function.

if you already initialized the initial value in constructor, you can set force to true to reinitialize initial value

helper.init({
  k: 'origin',
  k2: 'config',
})

helper.initFromFile(path)

initial config value with disk file, config file can be .json or .json5

helper.initFromFile('./config/config.json5')

set config

you can change one of config values by specified key, also can cover the full config by new config value

helper.set('k', 'value')

// full cover
helper.set({
  k: 'v'
})

get config

you can use get function to get specified config value, or just use helper.config['key'] directly

helper.get('k')

// full config
helper.get()

reset config

config will be reset to init value(file also be changed)

helper.reset()

Readme

Keywords

Package Sidebar

Install

npm i @winm/config-helper

Weekly Downloads

0

Version

0.0.1

License

Apache-2.0

Unpacked Size

39.5 kB

Total Files

10

Last publish

Collaborators

  • ziho