npm install yaml-configuration-loader
var loader = require('yaml-configuration-loader');
Here is the full list of accessible methods:
###load( [name,] path )
load and parse a yaml file
- name: (Optional) String. configuration name. if set, the configuration will be saved.
- path: String. file path
- Object. Configuration content
###get( name )
retrieve a loaded configuration
- name: String. configuration name.
- Object. Configuration content
###define( name, value )
define a new constant, it will be used during the configuration parsing
- name: String. constant name.
- value: Mixed. value.
###clear( )
clear the loaded configuration and the defined constants
###${env:ENVIRONMENT_VARIABLE}
retrieves the value of an environment variable
process.env.foobar = 'foobar_value';
file.yml
foo:
bar: '${env:foobar}'
loaded configuration (json)
{
"foo": {
"bar": "foobar_value"
}
}
###${const:CONSTANT_VARIABLE}
retrieves the value of a defined constant
by default only one constant is available: APP_PATH
it corresponds to the entry point of your application
it's the only constant which you can override
var loader = require('yaml-configuration-loader');
loader.define('foobar','foobar_value');
file.yml
foobar: ${const:APP_PATH}
foo:
bar: '${const:foobar}'
loaded configuration (json)
{
"foobar": "foo/bar/app.js",
"foo": {
"bar": "foobar_value"
}
}
###${local:LOCAL_VARIABLE}
retrieves the value of a property from the current configuration
file.yml
foo:
foobar : 'foobar_value'
bar: '${local:foo.foobar}'
loaded configuration (json)
{
"foo": {
"foobar": "foobar_value",
"bar": "foobar_value"
}
}
###${config:CONFIGURATION_NAME:CONFIGURATION_VARIABLE}
retrieves the value of a defined configuration
var loader = require('yaml-configuration-loader');
loader.load('foobar_conf', 'xxx/xxx.yml')
xxx.yml
foo:
bar: 'bar'
file.yml
foo:
bar: '${config:foobar_conf:foo.bar}'
loaded configuration (json)
{
"foo": {
"bar": "bar"
}
}
you can also add a default value to your dynamic fields with the
pipe syntax
by default the library will transform the given value into the most relevant type${const:foo|12} => "foo": 12
,${const:foo|bar} => "foo": "bar"
you can override this behaviour by adding quotes around the value, the parser will consider the value as a string.${const:foo|'12'} => "foo": "12"
source.yml
foo: '${const:foobar|\'hello\'}'
bar: '${const:foobar|"world"}'
foobar: '${env:foobar|"some \"string\""}'
barfoo: '${const:foobar|1337}'
barfoobar: '${config:foobar|"1024"}'
loaded configuration (json)
{
"foo": "hello",
"bar": "world",
"foobar": "some \"string\"",
"barfoo": 1337,
"barfoobar": "1024"
}
Yaml-config-loader
provides an importing system
You simply need to add an import statement and add some files to import
each files are described by two properties
- source: String. file path
- if: (Optional) Object. conditions { 'field (dynamic of not)' : 'value to check' }
you can use all syntaxes above presented except the ${local:...}
imports:
- { source: 'dev/config.yml', if: { '${env:envname}': 'dev' } }
imports:
- { source: 'dev/config.yml', if: { '${env:envname}': 'dev' } }
foo: 'bar'
All syntaxes of js-yaml are available
npm install
Build command: grunt es6
It will create js files from the es6 sources.
Unit tests: grunt unit
This library is based on the awesome js-yaml parser.