puppyrock
v0.0.1
Published
puppyrock is a small, no-fuss, dependency free library for managing and loading config files
Readme
PuppyRock is a lightweight, dependency-free, ES6 compliant JavaScript module to handle config files.
The focus of this module is to keep is lean and lightweight, while still being Just-Useful-Enough.
Usage
All you have to do is require and instantiate the config.
const Config = require('puppyrock')
const options = {
defaultConf: {
a: 2
}
}
const config = new Config(options)
console.log(config.a) // 2The Config class takes an options argument. See Options for valid properties.
Options
| Property | Type | note | |----------|-------|------| | defaultConf | Object | The default base config which userConf and environment variables is merged into | | userConf | String | Path to user defined config file | | envPrefix | String | Treat environment variables, prefixed with this string, as configuration parameters|
Example options
const options = {
defaultConf: {
a: 2
},
userConf: './config.conf',
envPrefix: 'PUPPYROCK_'
}Config inheritance
PuppyRock merges configs in the following order:
- defaultConf is copied into the final config
- userConf is then parsed and merged into the final config, overwritting any conflicts from defaultConf
- Environment variables are then consulted and any matching the envPrefix will be parsed and merged into the final config - Overwritting any clonficts from userConf and defaultConf
userConf format
The userConf options should point to a plain text file, separated by newlines. The config parameters in the config file is a set of key/value pairs seperated by an '='.
The right side of the '=' can either be a word, number or a comma separated list of words and/or commas. PuppyRock will cast the values the following way:
- A word is cast as a String
- A number is cast as a Number
- A comma-separated value is cast as an Array
All other lines will be ignored, thus you can use # to denote comments
Example
#This is a comment
a=b
number=25
list=dog,cat,42The above example will be parsed the following way:
a=bis parsed as{a: 'b'}number=25is parsed as{number: 25}list=dog,cat,42is parsed as{list: ['dog', 'cat', 42 ]}
Limitations
Deep nesting
You cannot create a list within a list. Thus list=listA=a1,a2,listB=b1,b2 is not supported and will not yield
{
list: [
{ listA: [
'a1',
'a2'
]
},
{ listB: [
'b1',
'b2'
]
}
]
}Numbers as strings
As any word resembling a non-float number is coerced into a number, it is not possible to create a String that contains, say, '42'
Objects are not supported
As you might be able to get from the userConf format section, only Strings, Numbers and Arrays are supported. Thus it is not possible to have an Object of key:values in the config (Well, you could have it in the defaultConf Object and not overwrite it. But for the sake of sanity, please don't)
FAQ
You state that is't dependency-free, when in reality it's not?
While PuppyRock has no production dependencies, it does have a few development dependencies. These are necesarry in order to provide unit- and integration-testing to ensure the code-quality of PuppyRock
