Skip to content
/ qconf Public
forked from ericelliott/qconf

Painless configuration with defaults file, environment variables, arguments, function parameters.

License

Notifications You must be signed in to change notification settings

malinkie/qconf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qconf

Painless configuration for Node apps with defaults file, environment variables, arguments, function parameters.

defaults file < process.env < command line arguments < function call override

Getting started

Create your config object:

var qconf = require('../qconf.js'),
  config = qconf(); // 99% of the time, this is all you need!

Extra credit

Create a JSON file: ./config/config.json

{
  "default_setting": true,
  "envOverride": false,
  "arg_override": false,
  "param_override": false
}

Maybe source some environment variables:

export env_override=true

Try a command-line option:

node myfile.js --arg-option true

Result

Painless configuration for your apps!

Motivation

Got sick of the complexity of nconf. Don't try to be everything to everybody. Just get a simple job done with simple code.

API

qconf()

This is the function that gets imported when you require qconf.

Creates a configuration singleton object for your app by reading configuration settings from a defaults file, environment variables, command line arguments, and finally a function parameters hash, in reverse priority.

  • @param {Object} [overrides] A map of config keys and values.
  • @param {Array} || {String} [filesPath] An array of config files in order of precedence (least to most) or path to the defaults file. Can be the only parameter.
  • @return {Object} config A configuration object.
  • @return {Function} config.get The get method

qconf.clear()

Clear the existing configuration. This is typically used to clear out any settings prior to reloading configuration with a subsequent call to qconf().

The config object

The config object is the object returned when you call qconf(). This is the getter and setter for all your app's environment variables.

config.get()

Return the value of the attribute requested.

  • @param {String} attr The name of the attribute to return.
  • @return {Any} The value of the requested attribute.

config.set()

Set the value of an attribute.

  • @param {String} attr The name of the attribute to set.
  • @param {Any} value The value to set the attribute to.
  • @return {Object} The config object (for chaining).

Events

The config object is an event emitter.

undefined

config.get() will emit the event 'undefined' if the value of the variable in question is undefined. It will also emit a convenient, searchable message that you can log and easily find in your logs:

'WARNING: Undefined environment variable: ' + `attr`

attr refers to the name of the variable you tried to get.

For example:

var qconf = require('qconf'),
  config = qconf(),
  util = require('util');
 
config.on('undefined', function (msg, attr) {
  util.log(msg);
 
  // or build a custom message:
  util.log('WAT? Can\'t find ' + attr);
});
 
config.get('some_var');
// Logs:
// "WARNING: Undefined environment variable: some_var"
// "WAT? Can't find some_var"

About

Painless configuration with defaults file, environment variables, arguments, function parameters.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.0%
  • Shell 1.0%