Plugins system using typescript and/or ts classes
This commit is contained in:
parent
b0118dff5b
commit
89e867ffab
3 changed files with 69 additions and 23 deletions
|
@ -5,23 +5,26 @@ import * as electron from 'electron'
|
|||
export default class PluginHandler {
|
||||
private basePluginsPath = path.join(__dirname, '../plugins');
|
||||
private userPluginsPath = path.join(electron.app.getPath('userData'), 'plugins');
|
||||
private pluginsList: any = [];
|
||||
private pluginsList: any = {};
|
||||
|
||||
constructor() {
|
||||
|
||||
this.pluginsList = this.getPlugins();
|
||||
console.log(this.pluginsList);
|
||||
}
|
||||
|
||||
public getPlugins(): any {
|
||||
let plugins: any = [];
|
||||
let plugins: any = {};
|
||||
|
||||
|
||||
if (fs.existsSync(this.basePluginsPath)) {
|
||||
fs.readdirSync(this.basePluginsPath).forEach(file => {
|
||||
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
||||
const plugin = require(path.join(this.basePluginsPath, file));
|
||||
plugins.push(new plugin());
|
||||
const plugin = require(path.join(this.basePluginsPath, file)).default;
|
||||
if (plugins[file] || plugin.name in plugins) {
|
||||
console.log(`[${plugin.name}] Plugin already loaded / Duplicate Class Name`);
|
||||
} else {
|
||||
plugins[file] = new plugin();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -31,8 +34,11 @@ export default class PluginHandler {
|
|||
fs.readdirSync(this.userPluginsPath).forEach(file => {
|
||||
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
||||
const plugin = require(path.join(this.userPluginsPath, file));
|
||||
console.log(plugin);
|
||||
plugins.push(new plugin());
|
||||
if (plugins[file] || plugin in plugins) {
|
||||
console.log(`[${plugin.default}] Plugin already loaded / Duplicate Class Name`);
|
||||
} else {
|
||||
plugins[file] = new plugin.default();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -40,4 +46,12 @@ export default class PluginHandler {
|
|||
return plugins;
|
||||
}
|
||||
|
||||
public callPlugins(event: string, ...args: any[]) {
|
||||
for (const plugin in this.pluginsList) {
|
||||
if (this.pluginsList[plugin][event]) {
|
||||
this.pluginsList[plugin][event](...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue