2022-10-27 13:00:16 +08:00
|
|
|
import { Database } from '@nocobase/database';
|
2022-06-17 10:25:59 +08:00
|
|
|
import { Application } from './application';
|
2022-02-28 21:49:50 +08:00
|
|
|
import { InstallOptions } from './plugin-manager';
|
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;
|
2022-09-18 14:10:01 +08:00
|
|
|
enabled?: boolean;
|
2021-09-14 11:09:26 +08:00
|
|
|
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 {
|
2022-10-27 13:00:16 +08:00
|
|
|
options: any;
|
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-10-27 13:00:16 +08:00
|
|
|
constructor(app: Application, options?: any) {
|
2022-01-30 11:11:36 +08:00
|
|
|
this.app = app;
|
|
|
|
this.db = app.db;
|
|
|
|
this.setOptions(options);
|
2022-10-27 13:00:16 +08:00
|
|
|
this.afterAdd();
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
get enabled() {
|
|
|
|
return this.options.enabled;
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
set enabled(value) {
|
|
|
|
this.options.enabled = value;
|
2022-09-18 14:10:01 +08:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
setOptions(options: any) {
|
|
|
|
this.options = options || {};
|
2022-09-18 14:10:01 +08:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
getName() {
|
|
|
|
return (this.options as any).name;
|
|
|
|
}
|
2021-09-14 11:09:26 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
afterAdd() {}
|
2022-09-18 14:10:01 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
beforeLoad() {}
|
2021-09-14 11:09:26 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
async load() {}
|
2022-02-28 21:49:50 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
async install(options?: InstallOptions) {}
|
2022-02-07 01:14:00 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
async afterEnable() {}
|
2022-09-18 14:10:01 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
async afterDisable() {}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
async remove() {}
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
2022-06-17 10:25:59 +08:00
|
|
|
|
|
|
|
export default Plugin;
|