forked from openmove/configYmlOld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (168 loc) · 4.62 KB
/
index.js
File metadata and controls
188 lines (168 loc) · 4.62 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
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 isDev
let environmentType
let environmentTypes
let environments
let config
let defaultsEnvVars = {}
let processEnv = {
NODE_ENV: 'prod'
}
function load (opts) {
let env, basepath, configfile;
if (_.isPlainObject(opts)) {
({env, basepath, configfile, defaultsEnvVars} = opts)
}
Object.assign(processEnv, defaultsEnvVars, process.env)
config = loadConfig(basepath, configfile)
environments = config.environments || {default: 'prod'}
envId = getEnvId(config, env)
ENVID = envId.toUpperCase()
isDev = ['DEV','DEVELOP','DEVELOPMENT'].includes(ENVID);
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 = '.', configfile = 'config.yml') {
if (fs.existsSync(`${basepath}/${configfile}`)) {
return loadConfigFile(`${basepath}/${configfile}`)
}
else if(fs.existsSync(`${basepath}/config`)) {
let tmpl = {}
multiFile = true
let files = fs.readdirSync(`${basepath}/config`)
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
}
else {
console.log(`Not found config in path: ${basepath}`);
throw new Error(`Not found config in path: ${basepath}`);
}
}
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 file: ' + erredSettings.join('; '))
}
if (erredSettings.length === 1) {
throw new Error(erredSettings[0] + ' is required in config yml file')
}
}
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,
isDev,
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