forked from pelias/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (56 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var fs = require('fs'),
path = require('path'),
Mergeable = require('mergeable'),
defaults = new Mergeable( __dirname + '/config/defaults.json' ),
localpath;
const _ = require('lodash');
const Joi = require('joi');
// allow the ops guys to override settings on the server
var generate = function( schema, deep ){
// if first parameter is a boolean, then it's deep, not a schema
if (_.isBoolean(schema)) {
deep = schema;
schema = undefined;
}
// deep defaults to true
if (deep === undefined) {
deep = true;
}
const config = getConfig(deep);
if (_.isObject(schema)) {
const result = Joi.validate(config, schema);
if (result.error) {
throw new Error(result.error.details[0].message);
}
return result.value;
}
return config;
};
function getConfig(deep) {
// load config from ENV
if( process.env.hasOwnProperty('PELIAS_CONFIG') ){
var production = new Mergeable( defaults.export() );
var p = path.resolve(process.env.PELIAS_CONFIG);
if( true === deep ){ production.deepMergeFromPath( p ); }
else { production.shallowMergeFromPath( p ); }
return production;
}
// load config from local file
else if( fs.existsSync( localpath ) ){
var local = new Mergeable( defaults.export() );
if( true === deep ){ local.deepMergeFromPath( localpath ); }
else { local.shallowMergeFromPath( localpath ); }
return local;
}
return defaults;
}
var config = {
defaults: defaults,
generate: generate,
setLocalPath: function( p ){
localpath = p.replace( '~', process.env.HOME );
return localpath;
}
};
config.setLocalPath( '~/pelias.json' );
module.exports = config;