2022-01-30 11:11:36 +08:00
|
|
|
import { Database } from '@nocobase/database';
|
2022-03-28 22:01:10 +08:00
|
|
|
import finder from 'find-package-json';
|
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-06-17 10:25:59 +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-04-17 10:00:42 +08:00
|
|
|
public abstract getName(): string;
|
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-02-28 21:49:50 +08:00
|
|
|
async install(options?: InstallOptions) {}
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
async load() {
|
|
|
|
const collectionPath = this.collectionPath();
|
|
|
|
if (collectionPath) {
|
|
|
|
await this.db.import({
|
|
|
|
directory: collectionPath,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionPath() {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
protected getPackageName(dirname: string) {
|
|
|
|
const f = finder(dirname);
|
|
|
|
const packageObj = f.next().value;
|
|
|
|
return packageObj['name'];
|
|
|
|
}
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
2022-06-17 10:25:59 +08:00
|
|
|
|
|
|
|
export default Plugin;
|