tachybase_todo/packages/server/src/plugin-manager.ts
ChengLei Shao 81978711e4
featPlugin multiple apps (#248)
* feat: multiple apps plugin

* feat: multipleAppManager in Application

* stage

* fix: export error

* test: multiple app

* application model

* feat: create application with plugins

* load and install after sub application created

* create subApp database beforeInstall

* sub apps listen to main app start & stop events

* refactor: getPluginName as package name

* feat: load apps on mainApp starts

* fix: test

* feat: beforeGetApplication event

* fix: test

* fix: test with sqlite memory database

* test: lazyLoad application

* fix: test with sqlite memory

* chore: clone database collection & promise.all
2022-03-28 22:01:10 +08:00

69 lines
1.6 KiB
TypeScript

import { CleanOptions, SyncOptions } from '@nocobase/database';
import Application from './application';
import { Plugin } from './plugin';
interface PluginManagerOptions {
app: Application;
}
export interface InstallOptions {
cliArgs?: any[];
clean?: CleanOptions | boolean;
sync?: SyncOptions;
}
export class PluginManager {
app: Application;
protected plugins = new Map<string, Plugin>();
constructor(options: PluginManagerOptions) {
this.app = options.app;
}
getPlugins() {
return this.plugins;
}
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');
}
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);
}
}
}