-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginbus.js
More file actions
107 lines (92 loc) · 2.55 KB
/
pluginbus.js
File metadata and controls
107 lines (92 loc) · 2.55 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const {listPackages} = require("./npm");
const EventEmitter = require('node:events');
class PluginBus {
constructor() {
this._init();
try {
this.config = require("./pluginbus.config.json");
} catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
// pass
this.config = {};
}
else {
throw e;
}
}
}
_init() {
this.plugins = [];
this.emitter = new EventEmitter({captureRejections: true});
this.emitter.on('error', console.error);
this.emitter[Symbol.for('nodejs.rejection')] = console.log;
}
async autodetect() {
let packages = await listPackages();
for (let pkgName of Object.keys(packages.dependencies)) {
try {
let pkg = require(pkgName + "/pluginbus.js");
let pkgConfig = {};
try {
pkgConfig = require(pkgName + "/pluginbus.config.json");
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
// pass
}
else {
throw e;
}
}
this.add(new pkg(this, Object.assign({}, {'core': this.config}, pkgConfig)));
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
// pass
}
else {
throw e;
}
}
}
}
async init() {
for (let plugin of this.plugins) {
await plugin.init();
}
}
add(plugin) {
this.plugins.push(plugin);
}
remove(plugin) {
this.plugins = this.plugins.filter(p => p !== plugin);
}
trigger(event, ...args) {
this.emitter.emit(event, ...args);
}
on(event, callback) {
this.emitter.on(event, callback);
}
once(event, callback) {
this.emitter.once(event, callback);
}
off(event, callback) {
this.emitter.off(event, callback);
}
removeAllListeners(event) {
this.emitter.removeAllListeners(event);
}
listeners(event) {
return this.emitter.listeners(event);
}
rawListeners(event) {
return this.emitter.rawListeners(event);
}
listenerCount(event) {
return this.emitter.listenerCount(event);
}
eventNames() {
return this.emitter.eventNames();
}
}
module.exports = PluginBus;