configuration-manager
Allows to create and manage configuration files in your project. You can use gulp-config-parameters plugin to automate how your configuration is created and managed.
Release Notes
0.4.0
- renamed main entry point. Now imports should be made like this:
import ... from "configuration-manager";
0.3.0
- renamed package name from
configurator.ts
toconfiguration-manager
- added new
configuration-manager/configuration-manager/
namespace - added support of environment variables
- added support of included configurations
- variables names now are
$VARIABLE_NAME$
and environment variable now is%ENV_VAR_NAME%
Installation
-
Install module:
npm install configuration-manager --save
-
Use typings to install all required definition dependencies.
typings install
-
ES6 features are used, so you may want to install es6-shim too:
npm install es6-shim --save
if you are building nodejs app, you may want to
require("es6-shim");
in your app. or if you are building web app, you man want to add<script src="path-to-shim/es6-shim.js">
on your page.
Usage
Create your configuration file, lets say ./config.json
:
Then register your configuration file in configurator and use it to get your configuration properties:
; Configurator;console; // prints: factory name: BMWconsole; // prints: show engine info?: trueconsole; // prints: car engine: [Object object]
If you have separate parameters file you can use it this way:
Lets say you have created ./parameters.json
And your ./config.json
is like this:
Now you can use configuration (with replaced parameters) this way:
; Configurator;Configurator;console; // prints: factory name: BMWconsole; // prints: show engine info?: trueconsole; // prints: car engine: [Object object]
This allows you to create a common configuration file for your app, and use different parameters on different platforms. You can use gulp-config-parameters plugin to automate this process.
You can use environment variables in your configuration this way:
Lets say you have SOME_ENVIRONMENT_VARIABLE
and ENGINE_DESCRIPTION_FROM_ENV
environment variables defined,
then you can use them in your configuration (config.json
) or parameters (parameters.json
) this way:
Variables will be replaced with environment variable values then. Take a look on this sample.
Using multiple configuration files
Sometimes you configuration is getting huge and you want to split it into multiple files. To make it easier configuration-manager supports this syntax to include other configuration files:
config.json:
connection.json:
When you are adding you configuration using addConfiguration
method you need to specify a path to your directory with
configuration files this way:
const baseDir = __dirname + "/configurations";configurator;
or you can simply use loadConfiguration
method:
const baseDir = __dirname + "/configurations";configurator;
Take a look on this sample.
typedi you can inject your configuration in your classes
If you are using;;; @ private factoryName: string; private showEngineInfo: boolean; { thisfactoryName = factoryName; // gives you "BMW" thisshowEngineInfo = showEngineInfo; // gives you "true" }
You can also inject right to the properties:
;;; @ @ factoryName: string; // value is "BMW" @ showEngineInfo: boolean; // value is "true"
Samples
Take a look on samples in ./sample for more examples of usage.
Todos
- cover with tests
- add support of more complicated expressions, like default parameters, or fallback parameters