Loading configs for projects based on environment variable
Install with npm
npm install projects-config
F. e. we have two projects: main
, admin
and two types of environments: dev
, production
configs/ * local configs (not in the repository)
├──admin/
│ └──production.json * {secretKey: 'YYYY'}
│
projects/
├──admin/
│ ├──config/ * public config
│ │ └──default.json * { resources: {adminApi: '//admin.mysite.com/api'} }
│ │
│ └──otherFiles
│
└──main/
├──config/ * public config
│ ├──default.json * { resources: {geoApi: '//maps.googleapis.com/maps/api/js'} }
│ ├──dev.json * { resources: {api: '//dev.mysite.com/api'} }
│ └──production.json * { resources: {api: '//mysite.com/api'} }
│
└──otherFiles
var configs = require('projects-config');
process.env.NODE_ENV = 'production';
configs.load('projects/**/config', 'configs');
console.log(configs);
//log:
//{
// admin: {
// resources: {
// api: '//mysite.com/api',
// geoApi: '//maps.googleapis.com/maps/api/js',
// adminApi: '//admin.mysite.com/api',
// },
// secretKey: 'YYYY'
// },
// main: {
// resources: {
// api: '//mysite.com/api',
// geoApi: '//maps.googleapis.com/maps/api/js'
// }
// }
//}
Plugin provides json
, json5
, hjson
, toml
, yaml
, cson
, properties
file formats. See node-config
Load configs from config's directories
Type: String
Default: 'config'
The path pattern to the directory with the public configurations. Plugin throws error if can not find config for current environment
Type: String
The path pattern to the directory with the local configurations. Plugin doesn't throw error if can not find config for current environment. It will use the default configuration. Local configs merge to public configs. Usually local configs are stored only on the local machine (not in the repository)
Type: String
Default: process.env.NODE_ENV
Environment
Type: String
Default: process.env.PROJECT
Set project name if you need config for one project. Set undefined/false/null
or '*'
for all projects
configs.load('projects/**/config', {project: 'main'});
console.log(configs);
//log:
//{
// main: {
// resources: {
// api: '//mysite.com/api',
// geoApi: '//maps.googleapis.com/maps/api/js'
// }
// }
//}
Type: String
Default: 'default'
File name (without extention) of default config
Type: Object/Function
Default: function(env, projectName) { return {env: env, project: projectName}; };
Sets default structure for each config. You can use this
as link on config of current project
function setDefaultConfig(env, projectName) {
var public = this.public || {};
public.project = projectName; //add projectName to `public` in result configs
return {
project: projectName
public: public
private: {
public: public //copy `public` to `private`
}
};
}
Create projects config stream
//configs:
//{
// project1: { public: {resources: 'resource1'} },
// project2: { public: {resources: 'resource2'} }
//}
configs.stream({section: 'public.resources'})
.pipe(gulp.dest(compiledPath));
//config.js
//{
// project1: 'resource1',
// project2: 'resource2'
//}
Type: String
Default: config.js
File name
Type: String
Part of config which will be used for forming of config file
Type: Number
Default: 4
Number of whitespaces of JSON.stringify
Executes a provided function once per project. If iterated part of configs is an array it will be provided function once per array element
//configs:
//{
// admin: {
// webserver: {port: 7001}
// },
// main: {
// webserver: [
// {port: 7002},
// {port: 7003}
// ]
// }
//}
configs.forEach('webserver', function(config, projectName) {
console.log(projectName, config);
})
//log:
//admin {port: 7001}
//main {port: 7002}
//main {port: 7003}
Type: String
Part of config which will be used for forming of config file
Type: Function
Function to execute for each element, taking two arguments:
config
project config or section of project config
projectName
project name
Type: Stream
Total stream composed of streams which were returned in callbacks
Applies a function against an accumulator and each project config (from left-to-right) to reduce it to a single stream. It is wrapper of _.reduce
Applies a function against an accumulator and each project config (from left-to-right) to reduce it to a single stream. It is wrapper of _.reduceRight
© Oleg Istomin 2015. Released under the MIT license