0.1.0 • Published 8 years ago

blacktea.configmanager v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

#blacktea.configManager A configuration manager able to manage configurations inside json files. ##Overview

##Installation Install it via the npm command: npm install blacktea.configManager --save ##Usage For the library to work, you need to add a configuration path to your package.json file: "configurationDirectory":"Config" In the above example we set /Config as the configuration directory. Now load the library: var configuration = require("blacktea.configManager"); Suppose you have a json file called ./Config/subdir/test.json with the following content:

{
    "path":{
        "to":{
            "value":"testingValue"
        }
    }
}

reading a configuration is as simple as: var someValue = configuration.get("subdir/test","/path/to/value"); In the above example you read the value "testingValue". Setting a value is just as simple: configuration.set("subdir/test","path/to/new/value",{new:"value"}) Now you have a json file which looks like this:

{
    "path":{
        "to":{
            "value":"testingValue",
            "new":{
                "value":{
                    "new":"value"
                }
            }
        }
    }
}

If you want to delete a value: configuration.delete("subdir/test","/path/to/value"); The resulting json file looks like this:

{
    "path":{
        "to":{
            "new":{
                "value":{
                    "new":"value"
                }
            }
        }
    }
}

You can also merge values: configuration.merge("subdir/test","path/to/new/value",{another:["a","b","c"]}); And you get:

{
    "path":{
        "to":{
            "new":{
                "value":{
                    "new":"value",
                    "another":["a","b","c"]
                }
            }
        }
    }
}

Accessing array values is also possible: var arrayValue = configuration.get("subdir/test","/path/to/new/value/another[1]"); will get you the value "b" The library also supports some useful methods like push/pop or append/shift. All are described in detail in dhe documentation section. The library supports a simple forEach method, which goes through all elements of a configuration json value and executes a callback. The value can be either an object or array. If it is neither, the callback will execute only once on the json value itself:

configuration.forEach("subdir/test","path/to/new/value/another",function(value,key,configurationPath){
    console.log("the value is: "+value);
    console.log("the key is: "+key);
    console.log("the configuration path is: "+configurationPath);
});

This module uses the json-templating library blacktea.jsonTemplates, which means it supports basic template strings. It injects a helper with its own functions into the jsonTemplates library and thus you can make a few very nice things. Assuming our test.json file would have the following content:

{
    "key1":"{{config.get('subdir/test','key3')}}",
    "key2":10,
    "key3":"{{config.get('subdir/test','key2')}}"
}

##Documentation -get(configPath, jsonPath) gets a configuration value. Throws an exception if the value doesn't exist. configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file -getOrDefault(configPath, jsonPath, defaultValue) gets a configuration value. If the value doesn't exist, it returns the default value. configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file defaultValue - the default value to return, if the actual value doesn't exist -set(configPath, jsonPath, value) sets a configuration value. configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file value - the value to set -delete(configPath, jsonPath) deletes a configuration value. configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file -merge(configPath, jsonPath, value) merges a value into an existing configuration. configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file value - the value to merge -forEach(configPath, jsonPath, callbask) goes through all values and calls a callback with the value, key and the json path of the current iteration. If value is an array or object, it calls the callback on each member, else it calls the callback on the actual value configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file callback - the callback with the arguments value, key, jsonPath. -append(configPath, jsonPath, value) appends a value to a configuration array configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file value - the value to append -push(configPath, jsonPath, value) prepends a value to a configuration array configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file value - the value to prepend -shift(configPath, jsonPath) removes the first value of a configuration array and returns it configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file -pop(configPath, jsonPath) removes the last value of a configuration array and returns it configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file -clear(configPath, jsonPath) empties a configuration array configPath - the path of the configuration file jsonPath - the path to the value inside of the configuration file -packageJson(configPath, defaultValue) an attempt to read the package.json file. The module searches for the file in the root directory jsonPath - the configuration path to the value inside the package.json defaultValue - the default value to return if the configuration wasn't found ##Licence blacktea.configManager is released under the MIT License.