core/src/plugin/plugin.ts

134 lines
3.0 KiB
TypeScript

import { PluginConfiguration, Service } from '../types';
import { ScopedEventEmitter } from '../util/events';
export interface IPlugin {
/**
* Plugin manifest information.
*/
manifest: IPluginManifest;
/**
* Core scoped event stream.
* This is how you talk to other plugins and the core using events.
*/
stream: ScopedEventEmitter;
/**
* Plugin configuration file.
* Note: `@Configurable` decorator is required for the configuration file.
*/
config: PluginConfiguration;
/**
* Service provider, only used for Protocol service plugins, such as `irc`.
*/
service: Service | null;
}
/**
* Base class for all plugins
*/
export class Plugin implements IPlugin {
public service: Service | null = null;
protected on = this.addEventListener;
constructor(
public manifest: IPluginManifest,
public stream: ScopedEventEmitter,
public config: PluginConfiguration
) {}
/**
* Called when plugin first starts.
*
* Please use this instead of the constructor to set up your plugin,
* as this will be called at the appropriate time for initialization.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
public initialize(): void {}
/**
* Shortcut to the plugin's manifest name.
*/
public get name(): string {
return this.manifest.name;
}
/**
* Shortcut to the plugin's manifest version.
*/
public get version(): string {
return this.manifest.version;
}
/**
* Shortcut to the core event stream.
* @param name Event name
* @param fn Listener
*/
protected addEventListener(name: string, fn: any): void {
this.stream.on(this.name, name, fn);
}
/**
* Shortcut to the core stream event emitter.
* @param event Event name
* @param data Data
*/
protected emit(event: string, data: any): void {
this.stream.emit.call(this.stream, event, data);
}
/**
* Shortcut to the core stream event emitter, named variant.
* Emit events to a particular recipient.
* @param name Scope name
* @param event Event name
* @param data Data
*/
protected emitTo(name: string, event: string, data: any): void {
this.stream.emitTo.call(this.stream, name, event, data);
}
}
/**
* A plugin's manifest JSON.
*/
export interface IPluginManifest {
/**
* Source repository name.
* Populated automatically, not present in file.
*/
repository: string;
/**
* Full path for the plugin directory.
* Populated automatically, not present in file.
*/
fullPath: string;
/**
* Main file name.
*/
main: string;
/**
* Plugin name.
*/
name: string;
/**
* Plugin tags for organization.
*/
tags?: string[];
/**
* Plugin version (semver).
*/
version: string;
/**
* Plugin description.
*/
description: string;
/**
* Other plugins this plugin depends on.
*/
dependencies: string[];
/**
* NPM packages, including version number, the plugin depends on.
*/
npmDependencies: string[];
}