forked from jsonw23/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (160 loc) · 4.31 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
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
const _ = require('lodash')
const flow = require('lodash/fp/flow')
const head = require('lodash/fp/head')
const pick = require('lodash/fp/pick')
const keys = require('lodash/fp/keys')
const fs = require('fs')
const yaml = require('js-yaml')
const args = require('yargs').argv
const timestamp = _.now()
let multiFile = false
let envId
let ENVID
let environmentType
let environmentTypes
let environments
let config
let defaultsEnvVars = {}
let processEnv = {
NODE_ENV: 'prod'
}
function load (opts) {
let env, basepath
if (_.isPlainObject(opts)) {
({env, basepath, defaultsEnvVars} = opts)
}
Object.assign(processEnv, defaultsEnvVars, process.env)
config = loadConfig(basepath)
environments = config.environments || {default: 'prod'}
envId = getEnvId(config, env)
ENVID = envId ? envId.toUpperCase() : undefined
environmentTypes = environments.static || keys(config)
environmentType = _.includes(environmentTypes, envId) ? envId : environments.default
config = swapVariables(config)
return config
}
function loadConfigFile (file) {
try {
let text = fs.readFileSync(file, 'utf8')
let subbed = substitute(processEnv, text)
return yaml.load(subbed.replace)
} catch (e) {
if (!/ENOENT:\s+no such file or directory/.test(e)) {
console.log('Error Loading ' + file + ':', e)
throw e
}
}
}
function loadConfig (basepath = '.') {
if (fs.existsSync(`${basepath}/config.yml`)) {
return loadConfigFile(`${basepath}/config.yml`)
} else {
let tmpl = {}
multiFile = true
let files = fs.readdirSync(`${basepath}/config.yml`)
for (let i = 0; i < files.length; i++) {
if (files[i].endsWith('.yml')) {
let keyName = files[i].substring(0, files[i].length - '.yml'.length)
tmpl[keyName] = loadConfigFile(`${basepath}/config/` + files[i])
}
}
return tmpl
}
}
function getEnvId (obj, env) {
return env ||
args.env ||
flow(
pick(keys(obj)),
keys,
head
)(args) ||
processEnv.NODE_ENV
}
function substitute (file, p) {
let success = false
let replaced = p.replace(/\${([\w.-]+)}/g, function (match, term) {
if (!success) {
success = _.has(file, term)
}
return _.get(file, term) || _.get(file.defaultsEnvVars, term) || match
})
return {success: success, replace: replaced}
}
function transform (file, obj) {
let changed = false
let resultant = _.mapValues(obj, function (p) {
if (_.isPlainObject(p)) {
let transformed = transform(file, p)
if (!changed && transformed.changed) {
changed = true
}
return transformed.result
}
if (_.isString(p)) {
let subbed = substitute(file, p)
if (!changed && subbed.success) {
changed = true
}
return subbed.replace
}
if (_.isArray(p)) {
for (let i = 0; i < p.length; i++) {
if (_.isString(p[i])) {
p[i] = substitute(file, p[i]).replace
}
}
}
return p
})
return {changed: changed, result: resultant}
}
function log () {
console.log('CONFIG:', envId || '-', environmentType || '-')
}
function requireSettings (settings) {
let erredSettings = []
settings = _.isString(settings) ? [settings] : settings
_.forEach(settings, function (setting) {
if (!_.has(config, setting)) {
erredSettings.push(setting)
}
})
if (erredSettings.length > 1) {
throw new Error('The following settings are required in config.yml: ' + erredSettings.join('; '))
}
if (erredSettings.length === 1) {
throw new Error(erredSettings[0] + ' is required in config.yml')
}
}
function swapVariables (configFile) {
function readAndSwap (obj) {
let altered = false
do {
let tmp = transform(obj, obj)
obj = tmp.result
altered = tmp.changed
} while (altered)
return obj
}
let file = multiFile ? _.mapValues(configFile, readAndSwap) : configFile
file = _.merge(
{},
file || {},
file[environmentType] || {},
{
envId,
ENVID,
timestamp
})
/* unuseful replaced in loadConfigFile const enved = transform(process.env, file).result;
file = readAndSwap(enved)
return file */
return readAndSwap(file)
}
module.exports = function (opts) {
return load(opts)
}
module.exports.load = load
module.exports.log = log
module.exports.require = requireSettings