-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (66 loc) · 1.97 KB
/
main.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
'use strict'
import { waterfall } from 'async'
import { readdir, statSync } from 'fs'
import fastifyPlugin from 'fastify-plugin'
const ServiceConfigs = async (fastify, { path }) => {
this.env = process.env.NODE_ENV || ''
this.path = path
ServiceConfigs.init((err, configs) => {
if (err) {
throw new Error(err)
}
fastify.decorate('Configs', configs)
})
}
ServiceConfigs.init = (callback) => {
waterfall([
ServiceConfigs.ReadConfigDirectory,
ServiceConfigs.ValidateConfigFiles,
ServiceConfigs.MergeConfigs
], callback)
}
ServiceConfigs.ReadConfigDirectory = (callback) => {
readdir(this.path, (err, content = []) => {
if (err) {
return callback(err)
}
callback(null, content)
})
}
ServiceConfigs.ValidateConfigFiles = (directortContent, callback) => {
let configFiles = {
json: {},
js: {}
}
directortContent.sort()
directortContent.forEach(itm => {
let itmPath = `${this.path}/${itm}`
let isValid = true
isValid = isValid && statSync(itmPath).isFile()
isValid = isValid && (itm.match(new RegExp(`^configs.(${this.env}).json$`, 'i')) !== null || (new RegExp(`^configs.?json|js$`, 'i')).test(itm))
if (isValid) {
configFiles[(new RegExp('.+?.json$', 'i').test(itm)) ? 'json' : 'js'][itm] = require(itmPath)
}
})
callback(null, configFiles)
}
ServiceConfigs.MergeConfigs = (configFiles, callback) => {
// merge json objects
let configs = {}
for (let file in configFiles.json) {
configs = Object.assign({}, configs, configFiles.json[file])
}
// merge js objects
let jsConfigs = {}
for (let file in configFiles.js) {
jsConfigs = Object.assign({}, jsConfigs, configFiles.js[file])
}
// find common keys between json config and js config and merge
Object.keys(jsConfigs).forEach(key => {
if (Object.keys(configs).indexOf(key) !== -1) {
configs[key] = jsConfigs[key](configs[key])
}
})
callback(null, configs)
}
export default fastifyPlugin(ServiceConfigs)