Feather-config

Feather-based app and library configuration

View the Project on GitHub theVolary/feather-config

feather-config

Installation

npm install git://github.com/theVolary/feather-config.git

Overview

feather-config is a library useful in bootstrapping feather-like applications with a configuration environment. It provides multiple layers of inherited configuration, command-line argument support, and helper methods for safely reading configuration options. Options specified at a particular layer override any options specified at a lower layer. This is done in a manner so that only the properties specified are overwritten. For example, given the following default options:

{
  "host": "localhost",
  "port": 80
}

and the following app-level options in a file named config.json:

{
  "host": "www.example.com"
}

the resulting configuration object will look like this:

{
  "host": "www.example.com",
  "port": 80
}

Note that the port property from the default options was not overridden. This pattern is followed throughout the four layers of inheritance the library supports. Those layers are, in order of evaluation:

  1. defaults
  2. application
  3. environment
  4. command line options

The following diagram may demonstrate the layering a little better.

inheritance stack diagram

Configuration Layers

Default Options

This is the lowest level of options supported. If you're not building a library, the odds are you won't use this level. If omitted, {} is used.

App-level options

These options are specified at the application level. By convention, this is a file named config.json stored in the root of your application's file structure. The location of this file can be overridden. See the API section for more info.

Environment-level options

Environment level options provide support for different environments of execution. Most commonly you see this pattern in web frameworks where you have dev, test, and prod environments. Environment configurations may be specified within the app-level options in the environments property, or in a json file in the app's conf folder. In the latter case, the name of the file minus the extension is used as the name of the environment.

Command line options

The framework provides a measure of support for command line arguments, by providing a hook that will be called for each argument if the user provides a function to process them. In this manner, the application only needs to provide the logic of what to do with each argument. See the commandLineArgsHook option of the init method below for more information.

API

The library itself has just two methods:


init(options, callback)

This method is used to process configuration options and call back with the final configuration options.

options

callback parameters

The callback parameters follow the standard nodejs err, result pattern.

The resulting config object returned in init's callback also contains two utility methods:


safeGet(path, object)

This is a utility method to safely retrieve values from the given object without throwing undefined errors if any property in the path does not exist (null-safe get). If any of the properties in the path variable are not defined, the method returns null.

parameters


Examples

Typical usage is as follows:

var fConf = require("feather-config");

fConf.init({}, function(err, config) {
  ...
});

Full usage with all options looks something like this:

var fConf = require("feather-config");

var confOpts = {
  defaultConfigPath: 'lib/defaults.json',
  defaultOptionsHook: function(defaultOptions) {
    defaultOptions.someRuntimeOption = process.env.MY_VAR;
  },
  appConfigPath: 'myapp.json',
  commandLineArgsHook: function(arg, remainingArgs, cmdLineOptions) {
    if (arg === 'sweetOption') {
      cmdLineOptions.sweetOption = remainingArgs.shift();
    }
  }
};

fConf.init(confOpts, function(err, config) {
  if (err) {
    ...
  } else {
    var myVal = config.safeGet('some.nested.property');
  }
});