forked from pelias/config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (50 loc) · 1.49 KB
/
Copy pathindex.js
File metadata and controls
63 lines (50 loc) · 1.49 KB
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
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const Joi = require('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)) {
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
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 );
}
if (deep === true) {
return _.merge({}, default_config, custom_config);
} else {
return _.assign({}, default_config, custom_config);
}
}
module.exports = {
defaults: default_config,
generate: generate,
setLocalPath: function( p ){
localpath = p.replace( '~', process.env.HOME );
return localpath;
}
};