-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (29 loc) · 1010 Bytes
/
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
const fs = require('fs')
const path = require('path')
const process = require('process')
const _ = require('lodash')
const mkdirp = require('mkdirp')
function loadFileSync (file, defaults) {
var basedir = module.parent.filename ? path.dirname(module.parent.filename) : process.cwd()
var filepath = path.isAbsolute(file) ? file : path.join(basedir, file)
try {
return _.defaultsDeep(require(filepath), defaults || {})
} catch(err) {
if (defaults && err && err.code == 'MODULE_NOT_FOUND') {
mkdirp.sync(path.dirname(filepath))
fs.writeFileSync(filepath, JSON.stringify(defaults, null, ' '))
return _.cloneDeep(defaults)
}
throw err
}
}
var CONFIG
module.exports = function (settings, file) {
if (CONFIG === undefined) { // init call
return CONFIG = loadFileSync(file, settings)
} else if (settings === undefined && file === undefined) {
return CONFIG
} else if (_.isPlainObject(settings)) {
return CONFIG = _.defaultsDeep(settings, CONFIG)
}
}