-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
177 lines (161 loc) · 4.24 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
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 sh = require('shelljs')
const yaml = require('js-yaml')
const moment = require('moment')
const args = require('yargs').argv
const timestamp = moment().format('YYYYMMDDHHmmss')
let multiFile = false
let envId
let ENVID
let environmentType
let environmentTypes
let environments
let config
function load (env) {
config = loadConfig()
environments = config.environments || {}
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 {
return yaml.load(fs.readFileSync(file, 'utf8'))
} catch (e) {
if (!/ENOENT:\s+no such file or directory/.test(e)) {
console.log('Error Loading ' + file + ':', e)
throw e
}
}
}
function loadConfig () {
if (fs.existsSync('config.yml')) {
return loadConfigFile('config.yml')
} else {
let templ = {}
multiFile = true
let files = fs.readdirSync('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)
templ[keyName] = loadConfigFile('config/' + files[i])
}
}
return templ
}
}
function getEnvIdFromBranch () {
try {
let branch = sh.exec('git name-rev HEAD --name-only', {silent: true}).stdout
branch = _.last(_.split(branch, '/'))
return _.trimEnd(_.truncate(branch, {
length: 13,
omission: ''
}), '-').replace(/(\r\n|\n|\r)/gm, '')
} catch (e) {
console.log('ERR: ', e)
// Do nothing
}
}
function getEnvId (obj, env) {
return env ||
args.env ||
flow(
pick(keys(obj)),
keys,
head
)(args) ||
process.env.ENVIRONMENT_ID ||
getEnvIdFromBranch()
}
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) || 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 temp = transform(obj, obj)
obj = temp.result
altered = temp.changed
} while (altered)
return obj
}
let file = multiFile ? _.mapValues(configFile, readAndSwap) : configFile
file = _.merge(
{},
file || {},
file[environmentType] || {},
{
envId: envId,
ENVID: ENVID,
timestamp: timestamp
})
file = readAndSwap(file)
return file
}
module.exports = load()
module.exports.load = load
module.exports.log = log
module.exports.require = requireSettings