0.0.1 • Published 7 months ago

@winm/config-helper v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
7 months ago

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()
0.0.1

7 months ago