Confman - 针对「Node 运用」的配置文件加载模块

一句话引见

Confman 是一个壮大的设置文件加载器,不管你喜好 yaml 、cson、json、properties、plist、ini、toml、xml 照样 js,都能满足你的希望,而且越发简朴、越发壮大。

《Confman - 针对「Node 运用」的配置文件加载模块》 《Confman - 针对「Node 运用」的配置文件加载模块》

支撑的特征

  • 支撑多种设置文件花样,默许包含 yaml/cson/json/properties/plist/ini/toml/xml/js

  • 支撑设置文件互相援用,不管何种花样都能够「援用别的恣意花样」的设置文件

  • 支撑「基于目次」的多文件设置

  • 支撑「环境设置」,辨别加载临盆、测试等差别的设置

  • 能够异常随意马虎的「扩大」新的设置文件花样

  • 能够「夹杂运用」差别的设置文件花样

  • 内置多种「指令」,并可随意马虎的扩大新的指令

如今就装置

$ npm install confman --save

来几个示例

差别的环境设置

目次

app
├── index.js
├── config.dev.yaml
├── config.prod.yaml
└── config.yaml

index.js

const confman = require('confman');
const configs = confman.load(`${__dirname}/config`);
console.log(configs);

启动运用

$ NODE_ENV=prod node index.js 

经由过程指定 NODE_ENV 能够加载指定的「环境设置文件 config.prod.yaml」,并和「默许设置 config.yaml」举行兼并,
如果有雷同的设置,「环境设置会掩盖默许设置」

设置文件互相援用

文件一: test1.yaml

name: test1
#能够运用 $require 援用别的文件
child: $requrie ./test2

文件二: test2.json

{
  "name": "test2",
   "child": "$require other-file"
}

$require 能够在恣意支撑的花样的设置文件中运用

基于目次的多文件设置

目次构造

├── config
│   ├── conn.yaml
│   ├── index.yaml
│   └── mvc.yaml
├── config.dev
│   └── conn.yaml
├── config.prod
│   └── conn.yaml
└── index.js

index.js

const confman = require('confman');
const configs = confman.load(`${__dirname}/config`);
console.log(configs);

增加新花样

实在,多半状况你不须要这么做,如果确切有须要,你可如许编写一个自定义 loader

module.exports = {
  extname: '.xxx',
  load: function (configPath) {
    //...
    return configs;
  }
};

注册自定义 loader

confman.loaders.push(require('your-loader-path'));

新的扩大名

体式格局一,映射到一个已增加的 loader

confman.loaders.push({
  extname: '.xxx',
  loader: '.yaml'
});

体式格局二,直接映射到一个未增加的自定义 loader

confman.loaders.push({
  extname: '.xxx',
  loader: require('your-loader-path')
});

内置的指令

如上边用到的 $require,Confman 许可运用指令完成某些设置,内置的指令包含:

  • $require 援用指令,用于援用别的设置文件,参数为相对于当前文件的相对路径或绝对路径

  • $calc 盘算指令,用于盘算一个表达式,如 $calc root.baseUrl+”/xxx” (表达式中可用变量有 root:根对象,parent:父对象,self:当前对象)

  • $read 读取指令,用于读取一个文本文件,参数为相对于当前文件的相对路径或绝对路径

示例 example.yaml

name: example
test1: $require ./test1.json
test2: $read ./test2.txt
test3: $calc root.name + ":test3"

如果 test1.json 的内容为 { "name": "test1" }test2.txt 的内容为 my name is test2,
经由过程 Confman.load('./example') 加载 example 的效果为:

{
  "name": "example",
  "test1": { "name": "test1" },
  "test2": "my name is test2",
  "test3": "example:test3"
}

自定义指令

编写一个自定义指令的代码以下:

module.exports = {
  name: 'xxx',
  exec: function(context){
    //context.fromPath 来自哪一个设置文件
    //context.parser 当前 Confman 实例
    //context.root 根对象
    //context.parent 父对象
    //context.self 当前对象
    //context.name 设置属性名
    //context.value 指令后的值
    return {} //返回值为指令实行效果
  }
};

注册自定义指令

confman.directives.push(require('your_directive_path'));

别的的题目

  • 新的发起或 Bug 请运用 isseus 反应

  • 孝敬代码,请运用 Pull Request,需一并提交相干测试而且不能低于现有掩盖率

如今或将来有可能会用到?那你应该去加个 Star
GitHub : https://github.com/Houfeng/co…

    原文作者:Houfeng
    原文地址: https://segmentfault.com/a/1190000006082132
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞