Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

JavaScript configurations as they're meant to be. Kinda.

NotificationsYou must be signed in to change notification settings

namshi/reconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

JavaScript configurations as they're meant to be. Kinda.

Reconfig helps you by keeping your configuration clean,decoupled and smart. Don't believe us? Read on.

The problem

How do write declarative configuration in JavaScript?

Easy, simply create a JS object:

varconfig={protocol:'https',domain:'example.org',};varurl=config.protocol+'://'+config.domain;// https://example.org

Easy andugly as hell. How can we make the config more elegant?

The Solution

Enter Reconfig.

varreconfig=require('reconfig');varconfig=newreconfig({protocol:'https',domain:'example.org',});varurl=config.get('protocol')+'://'+config.get('domain');// https://example.org

Blah, this is just some more code...but reconfig is more than that.

With this library you can self-reference config values:

varreconfig=require('reconfig');varconfig=newreconfig({protocol:'https',domain:'example.org',url:'{{ protocol }}://{{ domain }}',});varurl=config.get('url');// https://example.org

Pretty nice right? Even better, reconfig is smart enough to beable to reference parameters at any level of the config:

varreconfig=require('reconfig');varconfig=newreconfig({credentials:{admin:{read:true,write:true},reader:{read:true,write:false}},users:{someImportantDude:{username:'him',password:'...',credentials:'{{ credentials.admin }}'}}});varcanTheImportantDudeWrite=config.get('users.someImportantDude.credentials').write;// true

Nice, as you saw we self-referenced a nested value in the config (credentials.admin)and we used the same dot-notation to retrieve the credentials of a specific user(users.someImportantDude.credentials).

It's not over: you can also pass parameters to your configurations!

varreconfig=require('reconfig');varconfig=newreconfig({greet:'Hello :who!'});vargreetJohn=config.get('greet',{who:'John'});// Hello, John!

By default, reconfig uses: to interpolate the parameters. For example, in the above code,:who gets replaced by the parameter value.You can specify your own interpolation for resolving the parameters by setting theparamsInterpolation option.

varreconfig=require('reconfig');varconfig=newreconfig({greet:'Hello {who}!'},{paramsInterpolation:['{','}']});vargreetJohn=config.get('greet',{who:'John'});// Hello, John!

Last but not least, reconfig lets you specify a default value toreturn if a config value you're trying to access doesn't exist:

varreconfig=require('reconfig');varconfig=newreconfig({a:1});config.get('b',{},2);// 2

** Setting you config object **If you wish to change parts of your config on an already existent Reconfig instance:

varreconfig=require('reconfig');varconfig=newreconfig({a:1,b:2,c:'{{a}}_{{b}}'});config.get('c')// 1_2;config.set({b:3});config.get('c')// 1_3;

Nodejs specifics

ENV overriders

So you're using reconfig in your node.js app and you'd like your sys admins to changevalues without touching too much JSON? How about the always familiar ENV vars?

Here's how you do it:

just tell reconfig your env vars prefix:

varreconfig=require('reconfig');varconfigValues={a:1,b:[2]};varconfig=newreconfig(configValues,{envPrefix:'MYPREFIX'});

and you'll be able to modify or add new values directly from your shell!

export or add your vars before runnig your node app:

MYPREFIX_a=3 MYPREFIX_b_0=4 MYPREFIX_c=5 node app.js

and here's what you'll get:

config.get('a')// 3config.get('b')[0]// 4  <-- Pay attention!! It works with arrays too! :Dconfig.get('c')// 5

The default properties separator is:_ (1 underscore).You can use your custom separator passing it to the constructor as 3rd parameter:

varconfig=newreconfig(configValues,{envPrefix:'MYPREFIX',separator:'__'});

Installation

Install this library viaNPM:

npm install reconfig

If you need it on the client side we highly recommendbrowserify.

Tests

This library is tested through mocha, simply run either

mocha

or

./node_modules/mocha/bin/mocha

if you don't have mocha installed globally.

About

JavaScript configurations as they're meant to be. Kinda.

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp