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 {
|
export default class PluginHandler {
|
||||||
private basePluginsPath = path.join(__dirname, '../plugins');
|
private basePluginsPath = path.join(__dirname, '../plugins');
|
||||||
private userPluginsPath = path.join(electron.app.getPath('userData'), 'plugins');
|
private userPluginsPath = path.join(electron.app.getPath('userData'), 'plugins');
|
||||||
private pluginsList: any = [];
|
private pluginsList: any = {};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
||||||
this.pluginsList = this.getPlugins();
|
this.pluginsList = this.getPlugins();
|
||||||
console.log(this.pluginsList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getPlugins(): any {
|
public getPlugins(): any {
|
||||||
let plugins: any = [];
|
let plugins: any = {};
|
||||||
|
|
||||||
|
|
||||||
if (fs.existsSync(this.basePluginsPath)) {
|
if (fs.existsSync(this.basePluginsPath)) {
|
||||||
fs.readdirSync(this.basePluginsPath).forEach(file => {
|
fs.readdirSync(this.basePluginsPath).forEach(file => {
|
||||||
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
||||||
const plugin = require(path.join(this.basePluginsPath, file));
|
const plugin = require(path.join(this.basePluginsPath, file)).default;
|
||||||
plugins.push(new plugin());
|
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 => {
|
fs.readdirSync(this.userPluginsPath).forEach(file => {
|
||||||
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
if (file.endsWith('.ts') || file.endsWith('.js')) {
|
||||||
const plugin = require(path.join(this.userPluginsPath, file));
|
const plugin = require(path.join(this.userPluginsPath, file));
|
||||||
console.log(plugin);
|
if (plugins[file] || plugin in plugins) {
|
||||||
plugins.push(new plugin());
|
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;
|
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");
|
const ElectronSentry = require("@sentry/electron");
|
||||||
ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214"});
|
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 {Win} from "./base/win";
|
||||||
import {ConfigStore} from "./base/store";
|
import {ConfigStore} from "./base/store";
|
||||||
import {AppEvents} from "./base/app";
|
import {AppEvents} from "./base/app";
|
||||||
// import PluginHandler from "./base/plugins";
|
import PluginHandler from "./base/plugins";
|
||||||
|
|
||||||
// const test = new PluginHandler();
|
// const test = new PluginHandler();
|
||||||
|
|
||||||
const config = new ConfigStore();
|
const config = new ConfigStore();
|
||||||
const App = new AppEvents(config.store);
|
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 Event Handlers
|
||||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
||||||
|
|
||||||
app.on('ready', () => {
|
electron.app.on('ready', () => {
|
||||||
App.ready();
|
App.ready();
|
||||||
|
plug.callPlugins('onReady');
|
||||||
|
|
||||||
console.log('[Cider] Application is Ready. Creating Window.')
|
console.log('[Cider] Application is Ready. Creating Window.')
|
||||||
if (!app.isPackaged) {
|
if (!electron.app.isPackaged) {
|
||||||
console.info('[Cider] Running in development mode.')
|
console.info('[Cider] Running in development mode.')
|
||||||
require('vue-devtools').install()
|
require('vue-devtools').install()
|
||||||
}
|
}
|
||||||
|
@ -32,6 +34,18 @@ app.on('ready', () => {
|
||||||
Cider.createWindow();
|
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.on('before-quit', () => {
|
||||||
// app.isQuiting = true;
|
// app.isQuiting = true;
|
||||||
|
|
|
@ -1,33 +1,51 @@
|
||||||
|
let i = 1, k = 1;
|
||||||
export default class ExamplePlugin {
|
export default class ExamplePlugin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Plugin Details (Eventually implemented into a GUI in settings)
|
||||||
|
*/
|
||||||
public name: string = 'examplePlugin';
|
public name: string = 'examplePlugin';
|
||||||
public description: string = 'Example plugin';
|
public description: string = 'Example plugin';
|
||||||
public version: string = '1.0.0';
|
public version: string = '1.0.0';
|
||||||
public author: string = 'Example author';
|
public author: string = 'Example author';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs on plugin load (Currently run on application start)
|
||||||
|
*/
|
||||||
constructor() {
|
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 {
|
onReady(): void {
|
||||||
console.log('Example plugin ready');
|
console.log('Example plugin ready');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs on app stop
|
||||||
|
*/
|
||||||
onStop(): void {
|
onStop(): void {
|
||||||
console.log('Example plugin stopped');
|
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