-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
100 lines (79 loc) · 2.48 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const Joi = require('@hapi/joi');
const default_config = require( __dirname + '/config/defaults.json' );
let localpath = process.env.HOME + '/pelias.json'; // default location of pelias.json
// generate the final configuration, taking into account user overrides
// as well as preferences for deep or shallow merges
function generate( 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)) {
return getValidatedSchema(config, schema);
}
return config;
}
function getValidatedSchema(config, schema) {
const validationResult = schema.validate(config);
if (validationResult.error) {
throw new Error(validationResult.error.details[0].message);
}
return addGetFunction(validationResult.value);
}
function generateDefaults() {
const default_copy = _.merge({}, default_config);
return addGetFunction(default_copy);
}
function generateCustom(custom_config, deep) {
let new_config;
// default to deep if unset
if (deep === true || deep === undefined) {
new_config = _.merge({}, generateDefaults(), custom_config);
} else {
new_config = _.assign({}, generateDefaults(), custom_config);
}
return addGetFunction(new_config);
}
function getConfig(deep) {
// load config from ENV
let custom_config;
if( process.env.hasOwnProperty('PELIAS_CONFIG') ){
custom_config = require( path.resolve(process.env.PELIAS_CONFIG) );
} else if ( fs.existsSync (localpath) ) {
custom_config = require( localpath );
}
return generateCustom(custom_config, deep);
}
/*
* Because it's not enumberable, the get function has to be added every time after
* cloning an object with _.merge or _.assign
*
* see: https://lodash.com/docs/4.17.15#get
*/
function addGetFunction(object) {
// set 'get' convenience function on returned object
Object.defineProperty(object, 'get', {
value: _.get.bind(null, object),
enumerable: false // allows comparison to `expected.json` in tests
});
return object;
}
module.exports = {
defaults: default_config,
generate: generate,
generateCustom: generateCustom,
generateDefaults: generateDefaults,
setLocalPath: function( p ){
localpath = p.replace( '~', process.env.HOME );
return localpath;
}
};