Node.js configuration library.
$ npm install --save-dev flavorsconst flavors = require('flavors');
const config = flavors('my-config', {
// options
});In all examples below it's assumed that a script that loads configuration using flavors is executed from the same
directory configuration is loaded from.
But in real use cases most likely it will be different and you may need to specify workingDir option.
There are two ways to extend configuration.
Configuration with name a-b-c is loaded from the following configuration files:
config.jsa/config.jsa/b/config.jsa/b/c/config.jsa/b/c/config.js
On each step loaded configuration is merged with configuration from previous step.
See extends property documentation.
Configuration files can be provided in multiple formats.
Each supported format requires special loader to be implemented.
This module comes with several built-in loaders:
Also other loaders are available as separate node.js modules:
You can use custom loaders by setting loaders option.
Custom loader definition is a node.js module that exports object with the following properties:
Extension of files that are accepted by this loader.
Function that accepts path to configuration file and returns an object with the following properties:
Loads configuration from node.js module.
You can use this loader with require('flavors/jsLoader') (for example in loaders option).
Simply export an object with configuration values.
dev/config.js:
module.exports = {
value: 1
};load configuration:
const config = flavors('dev');Export function that accepts a configuration the current configuration extends.
a/config.js:
module.exports = {
value: 1
};a/b/config.js:
module.exports = (config, info) => {
return {
parentValue: config.value,
value: 2,
};
};load configuration:
const config = flavors('a-b');will load
{
parentValue: 1,
value: 2
}Here a-b configuration extends a configuration. So a configuration will be loaded and passed to an exported function.
Read more about function export in load configuration property documentation.
The above object and function exports are the special cases of a general configuration definition:
module.exports = {
extends: 'some-other-configuration',
merge: false,
load: config => {
return {
value: config.value + 1
};
}
};In this general case exported object must contain at least of the following special properties. In other case exported object is treated as simple object export.
Object or function accepting configuration the current configuration extends.
If no other special properties are specified then it can be simplified to simple object export or function export.
Function accepts two parameters:
- extended configuration object
- object with the additional information:
{
"config": {
"name": "a-b",
"nameParts": ["a", "b"]
},
"currentConfig": {
"dir": "/home/user/project/a",
"file": "/home/user/project/a/config.js",
"name": "a",
"nameParts": ["a"]
},
"parentConfig": {
"dir": "/home/user/project",
"name": "",
"nameParts": []
},
"options": {
"configDirName": "/home/user/project"
}
}configcontains properties of the configuration that we want to loaddiris the directory where configuration is locatednameis the name of configurationnamePartsis the name of configuration splitted byconfigNameSeparatoroptions
currentConfigcontains properties of the configuration in extension hierarchy that is currently being loaded:dirfileis a path to the configuration file if it existsnamenameParts
parentConfigcontains properties of the parent configuration if the current config has single parent:dirfilenamenameParts
parentConfigsis an array of parent configurations if the current configuration has multiple parentsoptionscontains object passed tooptionsparameter
Sometimes it's not very convenient to have deeply nested directory trees like a/b/c/d/config.js and long names like a-b-c-d.
You can use alternative way of extending configuration by using extends property.
For example, you have d configuration located in d/config.js:
module.exports = {
extends: 'a-b-c',
load: config => {
return {
value: config.value + 1
}
}
};This will load a-b-c configuration and pass it as config parameter to load function.
By default it's true and loaded configuration is merged with the configuration it extends.
For example, you have a and a-b configurations:
a/config.js:
module.exports = {
value1: 1
};a/b/config.js:
module.exports = {
value2: 2
};load configuration:
const config = flavors('a-b');will load
{
value1: 1,
value2: 2
}But if you change a/b/config.js to:
module.exports = {
merge: false,
load: {
value2: 2
}
};then config will be loaded as:
{
value2: 2
}This property is used to set configuration that applies after loading all child configurations.
a/config.js:
module.exports = {
postload: config => {
config.a = 3;
return {
b: 4
};
}
};a/b/config.js:
module.exports = {
a: 1,
b: 2
};loaded config:
{
a: 3,
b: 4
}Configuration definition is the same as when using JavaScript loader general configuration definition except that obviously load property can be only an object.
You can use this loader with require('flavors/jsonLoader') (for example in loaders option).
const config = flavors(configName, options);Required name of the configuration to load.
It can be simple name like dev and such configuration will be loaded
from dev/config.js or it can be composite name like dev-custom and
the configuration will be loaded from dev/config.js and dev/custom/config.js.
See also extending configuration.
Optional object parameter containing various options.
By default composite configurations are loaded from a simple directory structure like a/b/c for configuration name a-b-c.
But with multiple configurations like a-b-c, a-b-d, a-e-f, g-h-i and when each configuration directory stores additional files it can be more convenient to place configurations under separate directories.
For instance, setting configDirName to config makes it search configuration a-b-c under config/a/config/b/config/c/config directory.
Configuration file name. Default value is config.
String which separates composite configuration name parts. Default value is -.
If you want to use configuration names like a/b/c then set it to /.
Array of loaders for various configuration file formats.
By default it's an array of built-in loaders: JavaScript loader and JSON loader.
For example, to disable JSON loader and use only JavaScript loader pass [require('flavors/jsLoader')] as loaders option value.
Function that merges configurations in extension hierarchy.
It accepts previous configuration object as first argument, current configuration object as second and object with the additional information as third (see load configuration property documentation).
By default it calls deepmerge with default options.
By default postload functions are applied in direct order. Passing true for this option reverses the order of postload execution, so that for the a-b-c configuration with a/config.js, a/b/config.js and a/b/c/config.js files firstly postload from a/b/c/config.js is executed, then returned result is merged with already loaded a-b-c configuration, then the same is performed for a/b/config.js and a/config.js files.
Function that applies transformation to each configuration in extension hierarchy.
It accepts configuration object as first argument and object with the additional information as second (see load configuration property documentation).
By default configuration is resolved relatively to process.cwd() path.
For example, if your script is located at /home/user/myProject/loadConfig.js and executed from /home/user/ path then
the dev configuration will be loaded from the /home/user/dev/config.js which is not the right path.
To change this behaviour specify workingDir option:
/home/user/myProject/loadConfig.js::
const config = flavors('dev', {workingDir: __dirname})Allows to run commands in the environment with loaded flavors configuration.
const runner = require('flavors/runner');
runner(command, configName, options);$ FLAVORS_CONFIG_NAME=release-beta npx flavors run 'echo $app_version' See CLI.
Can be a one of the following types:
- string: shell command, executable name or its path;
runner('echo $value', configName, options);
runner('/path/to/your/executable', configName, options);- non-empty string array containing shell command, executable name or its path as first elements and its arguments as other elements;
runner(['echo', '$value'], configName, options);- structure with the following fields:
command: required, see 1;args: optional arguments;
runner({command: 'echo', args: ['Hello, ', '$value', '!'] }, configName, options);- function receiving flavors configuration and returning value of the one of listed above types or
undefined(i.e. withoutreturnstatement);
runner(config => ['echo', config.value], configName, options);
runner(config => ({ command: 'echo', args: ['Hello, ', config.value, '!'] }), configName, options);
runner(config => { console.log(config.value); }, configName, options);- plugin structure:
command: see 4;options- plugin specific flavors options, which is merged withoptionsparameter;
example/config.js:
module.exports = {
value: 'world'
};echoPlugin.js:
module.exports = {
command: config => ['echo', 'Hello, ' + config.value],
options: {
transform: config => {
config.value += '!';
return config;
}
}
};runner(require('./echoPlugin'), 'example', options);
// prints "Hello, world!"- structure with the following fields:
plugin: see 5;args: array with additional plugin arguments or function receiving flavors configuration and returning this array;
runner({plugin: require('./echoPlugin'), args: config => [' And goodbye, ' + config.value]}, 'example', options);
// prints "Hello, world! And goodbye, world!"Contains the same fields as flavors options parameter with following additional parameters:
When command resolved to executable name and its arguments runner will try to resolve it to command defined in flavors configuration.
This command must be a string or a function, that accepts arguments, loaded flavors configuration and runner function that allows to run subsequent commands.
commandTest/config.js:
module.exports = {
value: 'Hello, world!',
command: {
echo: args => {
console.log('custom echo: ' + args.join(' '));
},
dockerCompose: {
test: (args, config) => console.log(config.value)
},
// "command.enabled" option is set to false to avoid calling this "ls" command recursively and call system "ls" executable
ls: (args, config, runner) => runner(['ls', ...args], {command: {enabled: false}})
}
};runner(['echo', 'a', 'b', 'c'], 'commandTest');
// prints "custom echo: a b c"
runner(['dockerCompose', 'test'], 'commandTest');
// prints "Hello, world!"
runner(['ls', '.'], 'commandTest');
//prints current directory contentDefault is command. Runner will search commands in flavors configuration under the property name specified in this option.
Default is true.
Set to false to disable command resolving from flavors configuration.
Set this options to true to use child_process.spawn() to run command asynchronously.
By default child.process.spawnSync() is used.
Options passed to child_process.spawnSync() or child_process.spawn() method (see async option).
For example, use { shell: true } to execute command inside shell to enable variable expansion:
runner('echo $someValue', configName, {shell: true});Returns result of child_process.spawn() or child_process.spawnSync() call (see async option).
$ npx flavors --help
flavors - print loaded configuration or configure environment and run command.
Commands:
cli.js print Load and print configuration with specified name in JSON format [default]
cli.js run <command> [args..] Load configuration and run command
Options:
--dir-name, -d Configuration directory name
--file-name, -f Configuration file name (excluding extension)
--loader, -l Name of a Node.js module or a path to it
--name, -n Configuration name. Use this option or FLAVORS_CONFIG_NAME environment variable. [required]
--options-path, -o Path to options file
--separator, -s Configuration name separator
--working-dir, -w Directory name where configuration resolving starts from
--help Show help [boolean]
--version Show version number
If you run commands with arguments that start with dash (-z) or double-dash (--some-option) or it's the same as flavors CLI options then add -- after command:
$ npx flavors run -n "myConfig" echo -- -n --hello --world
-n --hello --world
See the LICENSE file for details.