Repository navigation
Expand file tree
/
Copy pathpluginManager.js
More file actions
60 lines (46 loc) · 1.77 KB
/
Copy pathpluginManager.js
File metadata and controls
60 lines (46 loc) · 1.77 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
/**
* Main plugin manager.
*/
const fs = require('fs')
const path = require('path')
function log(msg) {
console.log('PLUGIN_MGR: ' + msg)
}
const PLUGIN_DIRECTORY = path.join(__dirname, 'plugins')
const PLUGIN_CONFIG_PATH = process.argv[2]
/**
* Requires 'index.js' in every subfolder of ./plugins.
*/
let plugins = fs.readdirSync(PLUGIN_DIRECTORY)
log('Plugins found: ' + plugins.join(','))
plugins.forEach(pluginName => {
// Constructor exported from index.js
let PluginClass = require(path.join(PLUGIN_DIRECTORY, pluginName, 'index.js'))
// Options are in appData/pluginConfig, named with the plugin name
let options = require(path.join(PLUGIN_CONFIG_PATH, `${pluginName}.json`))
// Options object is passed directly to the constructor
let instance = new PluginClass(options)
// Initialize called on the instance; expects an array of headers returned.
if (!instance.init) throw new Error('Plugin instances require a init() method!')
let headers = instance.init()
if (!headers && !Array.isArray(headers)) throw new Error('init() must return an array of headers!')
// Pass the dataOpen event up
process.send({ event: 'dataOpen', dataId: pluginName, data: headers }, err => {
if (err) console.error(err)
})
if (!instance.on) throw new Error('Plugins must be an EventEmitter')
instance.on('data', (data, time) => {
if (!data || !time) throw new Error('Data events must have data and time arguments!')
// Pass it up
process.send({ event: 'data', dataId: pluginName, data, time }, err => {
if (err) console.error(err)
})
})
instance.on('close', () => {
// Pass it up
process.send({ event: 'dataClose', dataId: pluginName }, err => {
if (err) console.error(err)
})
})
log('initialized plugin ' + pluginName)
})