plugin-loader
Usage
Define a base class and api for plugins:
import { PluginBase } from "@weedzcokie/plugin-loader";
export type PluginApi = {
x: number
y: number
}
export default abstract class Plugin extends PluginBase<PluginApi> {
x: number;
y: number;
init?(): void;
constructor(api: PluginApi) {
super(api);
this.x = api.x;
this.y = api.y;
}
}
Plugin manifest (plugin.json)
{
"name": "test-plugin",
"version": "1.0.0"
}
version
must be a semver compatible version string.
And with dependencies, similar to how package.json
defines dependencies:
{
"name": "plugin-2",
"version": "1.0.0",
"dependencies": {
"test-plugin": "^1.0.0"
}
}
Load plugins:
Assuming the following directory tree:
├─ plugins
│ ├─ test-plugin
│ │ ├── index.ts
│ │ └── plugin.json
│ └─ plugin-2
│ ├── index.ts
│ └── plugin.json
├─ index.ts
└─ Plugin.ts
import Loader, { NodeHandler } from "@weedzcokie/plugin-loader";
import Plugin, { PluginApi } from "./Plugin.ts":
const plugins = await Loader<Plugin, PluginApi>(["test-plugin"], {
api: {
x: 1,
y: 2,
},
path: path.resolve('./plugins'),
log: (msg: string) => console.log('[PluginLoader]', msg),
handlers: {
default: NodeHandler
}
});