-
Notifications
You must be signed in to change notification settings - Fork 0
/
vinconfig.js
59 lines (46 loc) · 1.54 KB
/
vinconfig.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
const debug = require('debug')('vinconfig')
const fs = require('fs')
const path = require('path')
const Validator = require('jsonschema').Validator
const v = new Validator()
function Vinconfig (schema = {}, opts = {}) {
v.addSchema(schema)
const directory = opts.directory || path.join(process.cwd(), 'config')
const defaultConfig = opts.default || 'development'
const envVar = opts.envVar || 'CONFIG'
debug(`Config directory ${directory}`)
debug(`Default config ${defaultConfig}`)
let CONFIG = process.env[envVar] || defaultConfig
debug(`env var ${envVar} is ${CONFIG}`)
CONFIG = CONFIG.trim()
let configPath
if (fs.existsSync(CONFIG)) {
debug(`Using file path from process.env.CONFIG`)
configPath = CONFIG
}
else {
debug(`Using config name '${CONFIG}'`)
configPath = path.resolve(directory, CONFIG + '.json')
if (!fs.existsSync(configPath)) {
configPath = path.resolve(directory, CONFIG + '.js')
if (!fs.existsSync(configPath)) {
throw new Error(`Could not find config file at ${configPath}`)
}
}
}
debug(`Loading config file ${configPath}`)
const config = require(configPath)
debug(JSON.stringify(config, null, 2))
const valid = v.validate(config, schema)
if (valid.errors.length) {
const msgs = []
valid.errors.forEach(e => {
msgs.push(e.stack)
console.log('CONFIG ERR: ' + e.stack)
})
const err = `Config ${configPath} does not match provided schema: ${msgs.join(' & ')}`
throw new Error(err)
}
return config
}
module.exports = Vinconfig