-
Notifications
You must be signed in to change notification settings - Fork 78
/
config.js
81 lines (70 loc) · 2.08 KB
/
config.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
const nest = require('depnest')
const fs = require('fs')
const { join } = require('path')
const { get, cloneDeep, isEqual } = require('lodash')
const JSON5 = require('json5')
// This is needed to over-ride config.sync.load in patchcore.
// By baking a fresh module with the config inside it,
// we avoid a race condition around trying to set / get the config
function configModule (config) {
var _config = {
complete: config,
custom: readCustomConfig(config)
}
function Getter (conf) {
return function (path, fallback) {
if (!path) return conf
return get(conf, path, fallback)
}
}
function setCustomConfig (path, arg) {
if (!Array.isArray(path) && typeof path !== 'string') {
const next = path
return writeCustomConfig(next)
}
const next = cloneDeep(_config.custom)
next.set(path, arg)
writeCustomConfig(next)
}
return {
configModule: {
gives: nest({
'config.sync.load': true,
'config.sync.get': true,
'config.sync.getCustom': true,
'config.sync.setCustom': true
}),
create: api => nest({
'config.sync.load': () => _config.complete,
'config.sync.get': Getter(_config.complete),
'config.sync.getCustom': Getter(_config.custom),
'config.sync.setCustom': setCustomConfig
})
}
}
function readCustomConfig (config) {
try {
const str = fs.readFileSync(
join(config.path, 'config'),
'utf8'
)
return JSON5.parse(str) // will read around comments
} catch (e) {
return {}
}
}
function writeCustomConfig (next) {
if (typeof next !== 'object') throw new Error('config must be an object!')
if (isEqual(_config.custom, next)) return
if (get(_config.custom, 'caps.sign') !== get(next, 'caps.sign')) throw new Error('do not change the caps.sign!')
fs.writeFile(
join(_config.complete.path, 'config'),
JSON.stringify(next, null, 2),
(err) => {
if (err) return console.error(err)
_config.custom = next
}
)
}
}
module.exports = configModule