This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathconfig.js
78 lines (69 loc) · 1.7 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
'use strict'
const streamifier = require('streamifier')
const promisify = require('promisify-es6')
module.exports = (send) => {
return {
get: promisify((key, callback) => {
if (typeof key === 'function') {
callback = key
key = undefined
}
if (!key) {
send({
path: 'config/show',
buffer: true
}, callback)
return
}
send({
path: 'config',
args: key,
buffer: true
}, (err, response) => {
if (err) {
return callback(err)
}
callback(null, response.Value)
})
}),
set: promisify((key, value, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
if (typeof key !== 'string') {
return callback(new Error('Invalid key type'))
}
if (typeof value !== 'object' &&
typeof value !== 'boolean' &&
typeof value !== 'string') {
return callback(new Error('Invalid value type'))
}
if (typeof value === 'object') {
value = JSON.stringify(value)
opts = { json: true }
}
if (typeof value === 'boolean') {
value = value.toString()
opts = { bool: true }
}
send({
path: 'config',
args: [key, value],
qs: opts,
files: undefined,
buffer: true
}, callback)
}),
replace: promisify((config, callback) => {
if (typeof config === 'object') {
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
}
send({
path: 'config/replace',
files: config,
buffer: true
}, callback)
})
}
}