-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (81 loc) · 2.61 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
const fs = require('fs')
const SpacebroClient = require('spacebro-client').SpacebroClient
var standardSettings = require('standard-settings')
var assignment = require('assignment')
var settings = standardSettings.getSettings()
var spacebroClient = new SpacebroClient()
var allowAccess = function (path) {
return path && path.startsWith(settings.folder.settings)
}
spacebroClient.on(settings.service.spacebro.client.in.getSettings.eventName, function (data, callback) {
console.log('get settings: ' + data)
if (!allowAccess(data.path)) {
data.error = 'Can not access this settings file, check settings-bro settings.folder.settings'
if (typeof callback === 'function') {
callback(data)
}
}
fs.readFile(data.path, 'utf-8', (err, content) => {
if (err) {
console.log('An error ocurred reading the file :' + err.message)
data.error = err.message
}
// console.log('The file content is : ' + content)
data.content = content
if (typeof callback === 'function') {
callback(data)
}
})
})
spacebroClient.on(settings.service.spacebro.client.in.writeSettings.eventName, function (data, callback) {
console.log('write to file:' + data)
if (!allowAccess(data.path)) {
data.error = 'Can not access this settings file, check settings-bro settings.folder.settings'
if (typeof callback === 'function') {
callback(data)
}
}
fs.writeFile(data.path, data.content, (error) => {
if (error) {
data.error = error.message
}
if (typeof callback === 'function') {
callback(data)
}
})
})
spacebroClient.on(settings.service.spacebro.client.in.patchSettings.eventName, function (data, callback) {
console.log('write to file:' + data)
if (!allowAccess(data.path)) {
data.error = 'Can not access this settings file, check settings-bro settings.folder.settings'
console.log('Error: ' + data.error)
if (typeof callback === 'function') {
callback(data)
}
return
}
fs.readFile(data.path, 'utf-8', (err, content) => {
if (err) {
console.log('An error ocurred reading the file :' + err.message)
data.error = err.message
content = {}
} else {
content = JSON.parse(content)
}
try {
data.content = JSON.parse(data.content)
} catch (e) {
}
content = assignment(content, data.content)
fs.writeFile(data.path, JSON.stringify(content, null, 2), (error) => {
if (error) {
data.error = error.message
} else {
console.log('Success writing the file :' + data.path)
}
if (typeof callback === 'function') {
callback(data)
}
})
})
})