2022-01-30 03:11:36 +00:00
|
|
|
import { Database } from '@nocobase/database';
|
2021-09-14 03:09:26 +00:00
|
|
|
import { Application } from './application';
|
2022-02-06 17:14:00 +00:00
|
|
|
import path from 'path';
|
2022-02-28 13:49:50 +00:00
|
|
|
import { InstallOptions } from './plugin-manager';
|
2021-09-22 16:16:04 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export interface PluginInterface {
|
|
|
|
beforeLoad?: () => void;
|
|
|
|
load();
|
|
|
|
getName(): string;
|
2021-09-22 16:16:04 +00:00
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
|
|
|
|
export interface PluginOptions {
|
|
|
|
activate?: boolean;
|
|
|
|
displayName?: string;
|
|
|
|
description?: string;
|
|
|
|
version?: string;
|
|
|
|
install?: (this: Plugin) => void;
|
|
|
|
load?: (this: Plugin) => void;
|
2021-09-22 16:16:04 +00:00
|
|
|
plugin?: typeof Plugin;
|
|
|
|
[key: string]: any;
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export type PluginType = typeof Plugin;
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export abstract class Plugin<O = any> implements PluginInterface {
|
|
|
|
options: O;
|
2021-09-14 03:09:26 +00:00
|
|
|
app: Application;
|
2022-01-30 03:11:36 +00:00
|
|
|
db: Database;
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
constructor(app: Application, options?: O) {
|
|
|
|
this.app = app;
|
|
|
|
this.db = app.db;
|
|
|
|
this.setOptions(options);
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
setOptions(options: O) {
|
|
|
|
this.options = options || ({} as any);
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
getName(): string {
|
|
|
|
return this.constructor.name;
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
beforeLoad() {}
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2022-02-28 13:49:50 +00:00
|
|
|
async install(options?: InstallOptions) {}
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
async load() {
|
|
|
|
const collectionPath = this.collectionPath();
|
|
|
|
if (collectionPath) {
|
|
|
|
await this.db.import({
|
|
|
|
directory: collectionPath,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionPath() {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|