-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginLoader.js
101 lines (101 loc) · 2.55 KB
/
PluginLoader.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
92
93
94
95
96
97
98
99
100
101
const VERSION = 1,
$ = require("$");
class PluginCore {
constructor({ appKernel, id, name, icon, future_tag }) {
this.App = appKernel;
this.ID = id;
this.NAME = name;
this.ICON = icon;
this.FUTURE_TAG = future_tag || [];
}
}
class PluginItem {
constructor(file_name, id, name, icon, future_tag, regexp) {
this.FILE_NAME = file_name;
this.ID = id;
this.NAME = name;
this.ICON = icon;
this.FUTURE_TAG = future_tag || [];
}
hasFutureTag(tag) {
return $.hasArray(this.FUTURE_TAG) && this.FUTURE_TAG.includes(tag);
}
canRegexp() {
return this.hasFutureTag("regexp");
}
}
class PluginLoader {
constructor(appKernel, plugin_dir) {
this.App = appKernel;
this.DIR = plugin_dir;
this.PLUGIN_LIST = [];
if (!this.DIR.endsWith("/")) {
this.DIR += "/";
}
}
loadPluginList() {
this.PLUGIN_LIST = [];
const fileList = $file
.list(this.DIR)
.filter(file => $.hasString(file) && file.endsWith(".js"));
$console.info({
fileList
});
if ($.hasArray(fileList)) {
fileList.map(file => {
try {
const pluginPath = this.DIR + file;
// $console.info({
// pluginPath
// });
const pluginFile = require(pluginPath),
plugin = new pluginFile(this.App);
if (plugin && plugin.ID && plugin.NAME && plugin.FUTURE_TAG) {
this.PLUGIN_LIST.push(
new PluginItem(
file,
plugin.ID,
plugin.NAME,
plugin.ICON,
plugin.FUTURE_TAG
)
);
}
} catch (error) {
$console.error(error);
}
});
}
$console.info({
PLUGIN_LIST: this.PLUGIN_LIST
});
}
getPlugin(pluginItem) {
if (pluginItem !== undefined) {
const pluginPath = this.DIR + pluginItem.FILE_NAME;
if ($.file.isFileExist(pluginPath)) {
const pluginFile = require(pluginPath);
if (pluginFile !== undefined) {
const plugin = new pluginFile(this.App);
$console.info(plugin);
return plugin;
}
}
}
return undefined;
}
getPluginList() {
return this.PLUGIN_LIST;
}
runPlugin(pluginItem, data) {
return new Promise((resolve, reject) => {
try {
this.getPlugin(pluginItem).run(data).then(resolve, reject);
} catch (error) {
$console.error(error);
reject(error);
}
});
}
}
module.exports = { VERSION, PluginCore, PluginLoader };