2022-01-30 11:11:36 +08:00
|
|
|
import { Database } from '@nocobase/database';
|
2021-09-14 11:09:26 +08:00
|
|
|
import { Application } from './application';
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export interface PluginInterface {
|
|
|
|
beforeLoad?: () => void;
|
|
|
|
load();
|
|
|
|
getName(): string;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
2021-09-14 11:09:26 +08:00
|
|
|
|
|
|
|
export interface PluginOptions {
|
|
|
|
activate?: boolean;
|
|
|
|
displayName?: string;
|
|
|
|
description?: string;
|
|
|
|
version?: string;
|
|
|
|
install?: (this: Plugin) => void;
|
|
|
|
load?: (this: Plugin) => void;
|
2021-09-23 00:16:04 +08:00
|
|
|
plugin?: typeof Plugin;
|
|
|
|
[key: string]: any;
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export type PluginType = typeof Plugin;
|
2021-09-14 11:09:26 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export abstract class Plugin<O = any> implements PluginInterface {
|
|
|
|
options: O;
|
2021-09-14 11:09:26 +08:00
|
|
|
app: Application;
|
2022-01-30 11:11:36 +08:00
|
|
|
db: Database;
|
2021-09-14 11:09:26 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
constructor(app: Application, options?: O) {
|
|
|
|
this.app = app;
|
|
|
|
this.db = app.db;
|
|
|
|
this.setOptions(options);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
setOptions(options: O) {
|
|
|
|
this.options = options || ({} as any);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
getName(): string {
|
|
|
|
return this.constructor.name;
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
beforeLoad() {}
|
2021-09-14 11:09:26 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
abstract load();
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|