Giter VIP home page Giter VIP logo

crampon's Introduction

crampon

Hierarchical configuration, made easy.

Basic Usage (No Hierarchy)

In it's most basic usage, you can add configuration objects to a crampon instance and then access that configuration:

var Crampon = require('crampon'),
    crampon = new Crampon().add({
        root: '/home/apps/ftknox'
    });

var config = crampon.getConfig();

Adding Data

Data can be added using the add and addFile methods. New data is merged with old data and if there are conflicts then new values will take precedence:

var crampon = new Crampon().add({
        root: '/home/apps/ftknox',
        db: {
            name: 'ftknox',
            pwd: '1234'
        }
    })
    .addFile('./conf'); // could be conf.js or conf.json if extension is not specified

// conf.js: module.exports = { db: { pwd:'abcd' } };

assert.deepEqual(crampon.getConfig(), {
    root: '/home/apps/ftknox',
    db: {
        name: 'ftknox',
        pwd: 'abcd'
    }
});

Hierarchical Configuration

Hierarchical configuration can be used to setup environment specific configuration with the ability to inherit and override configuration for each environment:

// Set the hierarchy when constructing an instance of crampon
var crampon = new Crampon(['prod', 'test', 'dev']).add({
    prod: {
        root: '/home/apps/ftknox',
        db: {
            name: 'ftknox',
            pwd: '1234'
        },
        debugLevel: 3
    },
    test: {
        debugLevel: 2
    },
    dev: {
        db: {
            name: 'ftknox_dev'
        },
        debugLevel: 1
    }
});

// Get the dev configuration
var config = crampon.getConfig('dev');

// Note that the environment param defaults to the NODE_ENV environment variable

assert.deepEqual(config, {
    root: '/home/apps/ftknox',
    db: {
        name: 'ftknox_dev',
        pwd: '1234'
    },
    debugLevel: 3
});

Overrides

When using a hierarchical configuration, data added via add and addFile must have environment keys at the root. In order to override environment specific configuration with configuration that applies in all cases you can use addOverride and addOverrideFile.

Continuing the above example, you could override the debugLevel in your dev environment in this way:

crampon.addOverride({
    debugLevel: 1
});

// Get the configuration
var config = crampon.getConfig('dev');

// debugLevel is now 1 in all environments
assert.deepEqual(config, {
    root: '/home/apps/ftknox',
    db: {
        name: 'ftknox_dev',
        pwd: '1234'
    },
    debugLevel: 1
});

Arrays

As of version 0.2, arrays in configuration are replaced, rather than merged.

var crampon = new Crampon().add({
        favoriteColor: 'pink',
        otherColors: [
            'blue',
            'red'
        ]
    })
    .add({
        otherColors: [
            'green',
            'black'
        ]
    });

assert.deepEqual(crampon.getConfig(), {
    favoriteColor: 'pink',
    otherColors: [
        'green',
        'black'
    ]
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.