-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataCollector.js
More file actions
60 lines (51 loc) · 1.62 KB
/
DataCollector.js
File metadata and controls
60 lines (51 loc) · 1.62 KB
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
/* eslint-env node */
const fs = require('fs')
const path = require('path')
const YAML = require('yamljs')
class DataCollector {
constructor() {
this.store = null
this.listeners = {}
}
registerListener(listener) {
const id = Math.max(...Object.keys(this.listeners), 0) + 1
this.listeners[id] = listener
return id
}
unregisterListener(listenerId) {
delete this.listeners[listenerId]
}
notifyListeners(change) {
Object.values(this.listeners).forEach(listener => listener(change))
}
readDir(source) {
return new Promise((resolve, reject) => {
fs.readdir(source, null, (err, result) => err ? reject(err) : resolve(result.map(file => path.join(source, file))))
})
}
async get(source) {
if (!this.store) {
const list = await this.readDir(path.join(__dirname, source))
this.store = list.map(file => YAML.load(file))
}
return this.store
}
async saveNodeChanges(id, change) {
const nodes = await this.get('data')
const node = nodes.find(n => n.id === +id)
if (node) {
Object.keys(change).forEach(name => node[name] = change[name])
fs.writeFileSync(path.join(__dirname, 'data', id + '.yaml'), YAML.stringify(node))
}
this.notifyListeners({type: 'update', node})
return node
}
async initializeFromData() {
const initFile = process.env.INIT_FILE || path.join(__dirname, 'public', 'data.json')
const data = JSON.parse(fs.readFileSync(initFile).toString())
data.nodes.forEach(node => {
fs.writeFileSync(path.join(__dirname, 'data', node.id + '.yaml'), YAML.stringify(node))
})
}
}
module.exports = DataCollector