为任意对象提供配置管理
# For Aimee
aimee i config# For Node
npm i vpm-config// For Aimee
var Config = require('config');
var config = new Config;// For Node
var Config = require('vpm-config');
var config = new Config;config.init();
// eg1
config.set('foo', 'bar');
config.get('foo'); // => bar
// eg2
config.set('foo.bar', '123');
config.get('foo'); // => {bar: 123}
config.get('foo.bar'); // => 123
// eg3
config.merge('foo.bar', {
test: 123
});
config.get('foo.bar'); // => {test: 123}在其他app中接入config 1
// Build
var app = {};
var Config = require('vpm-config');
app.config = new Config;
app.config.init({
foo: true,
bar: false
})
// your code
// ...
module.exports = app;// Use
var app = require('app');
// Set
app.config.set('bar', true);
// Get
app.config.get('foo') // => true
// your code
// ...在其他app中接入config 2
// Build
var app = {};
var Config = require('vpm-config');
var config = new Config;
config.init({
foo: true,
bar: false
})
app.config = function(){
config.general.apply(config, arguments);
return this;
}
// your code
// ...
module.exports = app;// Use
var app = require('app');
// Set
app.config('bar', true);
// Get
app.config('foo') // => true
// your code
// ...config.init(target)
@paramtargettype: Object可选,传入已有的配置@paramtargettype: String可选,传入初试配置文件绝对路径或模块id,Nodejs下会被当做默认持久化存储路径
config.init({ foo: 'bar' })
// Or
config.init('/User/gavinning/foo.json')config.set(key, value)
@des覆盖配置文件中指定的key@paramkeytype: String必选,需要更新的key@paramvaluetype: AnyType必选,任意数据类型均可
config.set('app.user.name', 'gavinning')config.get(key)
@paramkeytype: String可选,不传key默认返回完整的配置对象
config.get() // => { app: { user: { name: 'gavinning' } }, foo: 'bar' }
config.get('app') // => { user: { name: 'gavinning' } }
config.get('app.user') // => { name: 'gavinning' }config.merge(key, value)
@des合并到配置文件指定的key@paramkeytype: String可选,没有key的时候直接更新到根对象(慎用)@paramvaluetype: AnyType必选,任意数据类型均可
config.merge('app.conf', { path: '/app/gavinning' })
config.get() // => { app: { user: { name: 'gavinning' }, conf: { path: '/app/gavinning' } }, foo: 'bar' }config.general(key, value)
@des通用方法,可以转接到app的config上,让app.config进可攻退可守@paramkeytype: String可选,key && !value时 调用 config.get(key)@paramkeytype: Object可选,此时调用 config.merge(key)@paramvaluetype: AnyType可选,此时调用 config.set(key, value)
// Build
var app = {};
var Config = require('config').Config;
var config = new Config;
config.init({
foo: true,
bar: false
})
app.config = function(){
return config.general.apply(config, arguments) || this;
}config.save(src, options, fn)
@des持久化存储,仅Nodejs下有效@paramsrctype: String初始化参数为路径时,此参数可选@paramoptionstype: Object持久化存储配置项,此参数可选@paramfntype: Function可选,fn ? 异步处理 : 同步处理
config.save() // 同步,默认使用初始化时的路径
config.save('/User/gavinning/foo.json') // 同步
config.save('/User/gavinning/foo.json', {pretty: true}, fn) // 异步
options.pretty = true // => 美化存储结果
options.prettyOptions = {
type: 'tab', // => tab | space�
size: 1 // => 1个单位
}
