huafua-configparser
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

基于node的configparser

有时候想在项目中以app.conf的形式引入我们的配置,但是里头的都是字符窜,想要单独处理下又觉得浪费时间,这时候本模块就起作用了。

配置文件为如下格式:

[section]
optionName=optionValue
optionName1=optionValue1

1. 几个Api

该模块向外提供了ConfigOptionConfigSectionConfigParser类:

支持config.section.option的形式获取配置项的值

ConfigParser提供的Api:

  • addSection(section:ConfigSection):添加节点
  • addSections(sections:Array<ConfigSection>):批量添加节点
  • addOption(sectionName:string,option:ConfigOption):添加配置项
  • addOptions(sectionName:string,options:Array<ConfigOption>):批量添加配置项
  • readAsync(configpath:string):异步形式读取配置文件,配置文件路径必须为绝对路径
  • read(configpath:string):同步形式读取配置文件,配置文件路径必须为绝对路径
  • get(sectionName?:string,optionName?:string):获取指定节点或配置项的值,当没有给定参数则直接返回该配置的json数据
  • getInt(sectionName:string,optionName:string):获取指定配置项的int
  • getString(sectionName:string,optionName:string):获取指定配置项的string
  • getJson(sectionName?:string):获取指定节点的json对象,若没有指定参数则获取当前配置的json对象
  • set(sectionName:string,optionName:string,optionValue:string|number):设置指定节点的配置项
  • save(distPath:string):保存当前配置到指定文件,文件路径须为绝对路径
  • saveAsJson(distPath?:string):保存当前配置json数据到指定文件,文件路径须为绝对路径
  • clear():清空配置
  • remove(sectionName:string,optionName?:string):删除指定节点或配置项

ConfigSection提供的Api

  • ConfigSection(sectionName:string):构造方法,传递节点名称实例化对象
  • init(options:Array<ConfigOption>):通过配置项数组初始化配置节点
  • addOption(option:ConfigOption):添加配置项
  • addOptions(options:Array<ConfigOption>):批量添加配置项
  • get(optionName:string):获取当前节点指定配置项的值
  • getInt(optionName:string):获取当前节点指定的配置项int
  • getString(optionName:string):获取当前节点指定配置项的string
  • getJson():获取配置节点的json数据
  • set(optionName:string,optionValue:string|number):该节点设置一个配置项
  • remove(optionName:string):删除指定配置项

2. 使用

  1. 导入
    var ConfigParser=require("huafua-configparser");
  2. 实例化
    var config=new ConfigParser();
  3. 指定配置文件
    config.read(path.resolve(__dirname,"app.conf"));
  4. 使用Api

Package Sidebar

Install

npm i huafua-configparser

Weekly Downloads

15

Version

1.0.3

License

MIT

Unpacked Size

29.6 kB

Total Files

11

Last publish

Collaborators

  • huafua