2022-02-28 22:10:04 +08:00
|
|
|
import { CleanOptions, SyncOptions } from '@nocobase/database';
|
2022-01-30 11:11:36 +08:00
|
|
|
import Application from './application';
|
|
|
|
import { Plugin } from './plugin';
|
|
|
|
|
|
|
|
interface PluginManagerOptions {
|
|
|
|
app: Application;
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:49:50 +08:00
|
|
|
export interface InstallOptions {
|
|
|
|
cliArgs?: any[];
|
|
|
|
clean?: CleanOptions | boolean;
|
|
|
|
sync?: SyncOptions;
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export class PluginManager {
|
|
|
|
app: Application;
|
|
|
|
protected plugins = new Map<string, Plugin>();
|
|
|
|
|
|
|
|
constructor(options: PluginManagerOptions) {
|
|
|
|
this.app = options.app;
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
getPlugins() {
|
|
|
|
return this.plugins;
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
get(name: string) {
|
|
|
|
return this.plugins.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
add<P = Plugin, O = any>(pluginClass: any, options?: O): P {
|
|
|
|
const instance = new pluginClass(this.app, options);
|
|
|
|
|
|
|
|
const name = instance.getName();
|
|
|
|
|
|
|
|
if (this.plugins.has(name)) {
|
|
|
|
throw new Error(`plugin name [${name}] `);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.plugins.set(name, instance);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.app.emitAsync('beforeLoadAll');
|
|
|
|
|
|
|
|
for (const [name, plugin] of this.plugins) {
|
|
|
|
await plugin.beforeLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [name, plugin] of this.plugins) {
|
|
|
|
await this.app.emitAsync('beforeLoadPlugin', plugin);
|
|
|
|
await plugin.load();
|
|
|
|
await this.app.emitAsync('afterLoadPlugin', plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.app.emitAsync('afterLoadAll');
|
|
|
|
}
|
2022-02-28 21:49:50 +08:00
|
|
|
|
|
|
|
async install(options?: InstallOptions) {
|
|
|
|
for (const [name, plugin] of this.plugins) {
|
|
|
|
await this.app.emitAsync('beforeInstallPlugin', plugin, options);
|
|
|
|
await plugin.install(options);
|
|
|
|
await this.app.emitAsync('afterInstallPlugin', plugin, options);
|
|
|
|
}
|
|
|
|
}
|
2022-04-17 10:00:42 +08:00
|
|
|
|
|
|
|
static resolvePlugin(pluginName: string) {
|
|
|
|
return require(pluginName).default;
|
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
}
|