A javascript configuration engine
npm install conditionsexample/ directory to see some simple examples of what is possible.FS and HTTP loading. FS loading is assumed by default, and thenpm install conditions
conditions('config/app.config', 'http://www.example.com/config/app', options)
The above command will result in the following (Note: Some parts may be in parallel):
1. Load ./config/app.config
2. Load http://www.example.com/config/app.config
3. Override 1 with 2 and return the result
* protectStructure - When set to true, all property definitions in the configuration will be set
to non-configurable.
* readOnly - When set to true, all value properties in the configuration will be set to
non-writable.
* levels - An array used to explode filenames to load. Every file specified in the main loader function will
become n files (where n is the number of levels defined) and a sub extension will be added to each of the
filenames. If an empty string is passed, an entry with no sub extension will be generated.
* verbose - When set to true, will print out full errors in the case of load files not found.
file:// path pointing to the current working directory of the process.If an absolute path is received, it will override this relative path. If a URL is supplied the domain will become
part of the base and a file:// URI should be used to clear the HTTP part from the base if desired (and set a new base since we always expect an absolute location.) Some examples below.
To load a file named production in the config directory found in the current working directory (process.cwd()),
then extend it with development and local, which are also found in the config directory.
* conditions('/config/production', 'development', 'local')
To load a file from http://example.com/config/production, extend it with a file fromhttp://example.com/config/production, extend it with a file local1 from the config directory,
then with a file local2, also from the config directory.
conditions('http://example.com/config/production', 'development', 'file:///config/local1', 'local2')
When assigning an object to a config object, a copy of that object (which itself is a config object) will end up
being assigned to the object. i.e. var bar = {}; configObj.foo = bar; configObj.foo === bar; // false
{
"string property": "foo bar",
num: 100,
bool: true,
// Comments inside of the config are also possible
regexp: /yeah/,
subobjects: {
"sure": ['and', 'arrays','too','(', 'You can also define an array as the root', ')']
},
expression: this['string property'] + num
}
Wrapping {} and [] are optional. An array or object will be inferred from the contents.
Expressions are also able to reference other objects in the configuration hierarchy by referencing
the desired objects through their id value (described below). When an expression is part of an
extending configuration the base identifier can be used to access the underlying value, thesource identifier can be used to access the underlying config at its root, and any objects
declared with id properties will be available to the extending config expressions.
prototype so that if an id config value is defined it will still be available on the config {
id: root,
id: "Configuration property",
val: 10,
sub: {
id: sub,
subval: 10
},
sub2: {
val: root.val + sub.val
}
}
An inspection of the values on this object should produce something like:
{
id: "Configuration property",
val: 10,
sub: {
subval: 10
},
sub2: {
val: 20
}
}
with a prototype of:
{
id: "root",
...
}
Note: As you change sub.subval, subsequent calls to sub2.val will result in different values.
server: {
id: server,
port: 8080,
host: 'example.com',
secure: false,
protocol: secure ? 'https' : 'http',
url: ${protocol}://${host}:${port === 80 ? '' : port}/
},
site: {
home: server.url + 'index.html',
api: ${server.url}api/
}
conditions.on(, , ) . on is a shortcut for addListener,removeListener is available to remove handlers from events. Any config object receives changes for the child. "$.).For commands where it is appropriate, the find property is used for searching, and behaves as
follows: It will match if it matches the array value explicitly or, if it is an object and the
array value is an object, if every property matches every property of the same name on the
array element.
The following commands may be issues:
* add - Adds the value defined in the property value to the end of the array.
* remove - Removes a value from the array. The find (See above) property is used to
determine which element to remove.
* update - Replaces an element in the array. The find (See above) property is used to
determine which element to replace.
* extend - Runs the standard extend procedure on an element in the array. The find
(See above) property is used to determine which element to replace.
* clear - Removed all items from the array.
* Resource saver
* Rebuild of internals
* Internal config object properties with additional information (on a per property basis)
* Source of property (file with line and column or dynamic)
* Base property value (Recursive model)
* Other?
* Full usage of Proxy model