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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -4,27 +4,29 @@ require('v8-compile-cache');
|
|||
const ElectronSentry = require("@sentry/electron");
|
||||
ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214"});
|
||||
|
||||
import {app} from 'electron';
|
||||
import * as electron from 'electron';
|
||||
import {Win} from "./base/win";
|
||||
import {ConfigStore} from "./base/store";
|
||||
import {AppEvents} from "./base/app";
|
||||
// import PluginHandler from "./base/plugins";
|
||||
import PluginHandler from "./base/plugins";
|
||||
|
||||
// const test = new PluginHandler();
|
||||
|
||||
const config = new ConfigStore();
|
||||
const App = new AppEvents(config.store);
|
||||
const Cider = new Win(app, config.store)
|
||||
const Cider = new Win(electron.app, config.store)
|
||||
const plug = new PluginHandler();
|
||||
|
||||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* App Event Handlers
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
||||
|
||||
app.on('ready', () => {
|
||||
electron.app.on('ready', () => {
|
||||
App.ready();
|
||||
plug.callPlugins('onReady');
|
||||
|
||||
console.log('[Cider] Application is Ready. Creating Window.')
|
||||
if (!app.isPackaged) {
|
||||
if (!electron.app.isPackaged) {
|
||||
console.info('[Cider] Running in development mode.')
|
||||
require('vue-devtools').install()
|
||||
}
|
||||
|
@ -32,6 +34,18 @@ app.on('ready', () => {
|
|||
Cider.createWindow();
|
||||
});
|
||||
|
||||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Renderer Event Handlers
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
||||
|
||||
electron.ipcMain.on('playbackStateDidChange', (event, attributes) => {
|
||||
plug.callPlugins('onPlaybackStateDidChange', attributes);
|
||||
});
|
||||
|
||||
electron.ipcMain.on('nowPlayingItemDidChange', (event, attributes) => {
|
||||
plug.callPlugins('onNowPlayingItemDidChange', attributes);
|
||||
});
|
||||
|
||||
//
|
||||
// app.on('before-quit', () => {
|
||||
// app.isQuiting = true;
|
||||
|
|
|
@ -1,33 +1,51 @@
|
|||
let i = 1, k = 1;
|
||||
export default class ExamplePlugin {
|
||||
|
||||
/**
|
||||
* Base Plugin Details (Eventually implemented into a GUI in settings)
|
||||
*/
|
||||
public name: string = 'examplePlugin';
|
||||
public description: string = 'Example plugin';
|
||||
public version: string = '1.0.0';
|
||||
public author: string = 'Example author';
|
||||
|
||||
/**
|
||||
* Runs on plugin load (Currently run on application start)
|
||||
*/
|
||||
constructor() {
|
||||
this.name = 'coolPlugin';
|
||||
this.description = 'A pretty cool plugin';
|
||||
this.version = '1.0.0';
|
||||
this.author = 'Core';
|
||||
}
|
||||
|
||||
onStart(): void {
|
||||
console.log('Example plugin started');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on app ready
|
||||
*/
|
||||
onReady(): void {
|
||||
console.log('Example plugin ready');
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on app stop
|
||||
*/
|
||||
onStop(): void {
|
||||
console.log('Example plugin stopped');
|
||||
}
|
||||
|
||||
OnPlaybackStateChanged(attributes: object): void {
|
||||
/**
|
||||
* Runs on playback State Change
|
||||
* @param attributes Music Attributes (attributes.state = current state)
|
||||
*/
|
||||
onPlaybackStateDidChange(attributes: object): void {
|
||||
console.log('onPlaybackStateDidChange has been called ' + i +' times');
|
||||
i++
|
||||
}
|
||||
|
||||
OnMediaStateChanged(attributes: object): void {
|
||||
/**
|
||||
* Runs on song change
|
||||
* @param attributes Music Attributes
|
||||
*/
|
||||
onNowPlayingItemDidChange(attributes: object): void {
|
||||
console.log('onNowPlayingDidChange has been called ' + k +' times');
|
||||
k++
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue