forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurator.js
More file actions
191 lines (152 loc) · 5.28 KB
/
Copy pathconfigurator.js
File metadata and controls
191 lines (152 loc) · 5.28 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import _ from 'lodash'
import fs from 'fs'
import path from 'path'
import yaml from 'js-yaml'
import Promise from 'bluebird'
const validations = {
'any': (value, validation) => validation(value),
'string': (value, validation) => typeof(value) === 'string' && validation(value),
'choice': (value, validation) => _.includes(validation, value),
'bool': (value, validation) => (value === true || value === false) && validation(value)
}
const defaultValues = {
'any': null,
'string': '',
'bool': false
}
const amendOption = (option, name) => {
const validTypes = _.keys(validations)
if (!option.type || !_.includes(validTypes, option.type)) {
throw new Error(`Invalid type (${option.type || ''}) for config key (${name})`)
}
const validation = option.validation || (() => true)
if (typeof(option.default) !== 'undefined' && !validations[option.type](option.default, validation)) {
throw new Error(`Invalid default value (${option.default}) for (${name})`)
}
if (!option.default && !_.includes(_.keys(defaultValues), option.type)) {
throw new Error(`Default value is mandatory for type ${option.type} (${name})`)
}
return {
type: option.type,
required: option.required || false,
env: option.env || null,
default: option.default || defaultValues[option.type],
validation: validation
}
}
const amendOptions = options => {
return _.mapValues(options, amendOption)
}
const validateSet = (options, name, value) => {
// if name is not in options, throw
if (!_.includes(_.keys(options), name)) {
throw new Error(`Unrecognized configuration key: ${name}`)
}
if (!validations[options[name].type](value, options[name].validation)) {
throw new Error(`Invalid value for key: ${name}`)
}
}
const validateSave = (options, object) => {
const objKeys = _.keys(object)
const requiredKeys = _.filter(_.keys(options), key => options[key].required)
_.each(requiredKeys, required => {
if (!_.includes(objKeys, required)) {
throw new Error(`Missing required configuration: '${required}'`)
}
})
_.each(objKeys, name => {
validateSet(options, name, object[name])
})
}
const validateName = name => {
if (!name || !/^[A-Z0-9._-]+$/i.test(name)) {
throw new Error(`Invalid configuration name: ${name}. The name must only contain letters, _ and -`)
}
}
const overwriteFromDefaultValues = (options, object) => {
_.each(_.keys(options), name => {
if (typeof object[name] === 'undefined') {
object[name] = options[name].default
}
})
return object
}
const overwriteFromEnvValues = (options, object) => {
return _.mapValues(object, (_v, name) => {
if (options[name] && options[name].env && process.env[options[name].env]) {
return process.env[options[name].env]
}
return _v
})
}
const overwriteFromBotfileValues = (config_name, options, botfile, object) => {
return _.mapValues(object, (_v, name) => {
if (botfile
&& botfile.config
&& botfile.config[config_name]
&& typeof botfile.config[config_name][name] !== 'undefined') {
return botfile.config[config_name][name]
}
return _v
})
}
const overwriteFromConfigFileValues = async (config_name, options, projectLocation, object) => {
if (!projectLocation) {
return object
}
const configFilePath = path.resolve(projectLocation, `${config_name}.config.yml`)
if (!fs.existsSync(configFilePath)) {
return object
}
const configFromFile = await Promise.fromCallback(callback => {
yaml.safeLoadAll(fs.readFileSync(configFilePath), value => callback(null, value))
})
return _.mapValues(object, (_v, name) => {
if (typeof configFromFile[name] !== 'undefined') {
return configFromFile[name]
}
return _v
})
}
const removeUnusedKeys = (options, object) => {
const final = {}
_.each(_.keys(options), name => {
if (typeof object[name] !== 'undefined') {
final[name] = object[name]
}
})
return final
}
const createConfig = ({ kvs, name, botfile = {}, options = {}, projectLocation = null }) => {
if (!kvs || !kvs.get || !kvs.set) {
throw new Error('A valid \'kvs\' is mandatory to createConfig')
}
validateName(name)
options = amendOptions(options)
const saveAll = obj => {
validateSave(options, obj)
return kvs.set('__config', obj, name)
}
const loadAll = () => {
return kvs.get('__config', name)
.then(all => overwriteFromDefaultValues(options, all || {}))
.then(all => overwriteFromBotfileValues(name, options, botfile, all))
.then(all => overwriteFromConfigFileValues(name, options, projectLocation, all))
.then(all => overwriteFromEnvValues(options, all))
.then(all => removeUnusedKeys(options, all))
}
const get = name => {
return kvs.get('__config', name + '.' + name)
.then(value => overwriteFromDefaultValues(options, { [name]: value }))
.then(all => overwriteFromBotfileValues(name, options, botfile, all))
.then(all => overwriteFromConfigFileValues(name, options, projectLocation, all))
.then(all => overwriteFromEnvValues(options, all))
.then(obj => obj[name])
}
const set = (name, value) => {
validateSet(options, name, value)
return kvs.set('__config', value, name + '.' + name)
}
return { saveAll, loadAll, get, set, options }
}
module.exports = { createConfig }