feat: plugin before enable hook (#1648)

This commit is contained in:
ChengLei Shao 2023-04-04 16:08:10 +08:00 committed by GitHub
parent 28ef3b6952
commit a09c5001eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View File

@ -8,8 +8,11 @@ import { mockServer, MockServer } from '@nocobase/test';
describe('plugin', () => { describe('plugin', () => {
let app: MockServer; let app: MockServer;
beforeEach(() => { beforeEach(async () => {
app = mockServer(); app = mockServer();
await app.db.clean({ drop: true });
await app.db.sync();
}); });
afterEach(async () => { afterEach(async () => {
@ -108,4 +111,20 @@ describe('plugin', () => {
expect(Test).toBeDefined(); expect(Test).toBeDefined();
}); });
}); });
describe('enable', function () {
it('should call beforeEnable', async () => {
const beforeEnable = jest.fn();
class TestPlugin extends Plugin {
async beforeEnable() {
beforeEnable();
}
}
app.plugin(TestPlugin);
await app.pm.enable('TestPlugin');
expect(beforeEnable).toBeCalled();
});
});
}); });

View File

@ -30,6 +30,10 @@ export class PluginManagerRepository extends Repository {
} }
} }
for (const plugin of plugins) {
await plugin.beforeEnable();
}
await this.update({ await this.update({
filter: { filter: {
name, name,

View File

@ -68,6 +68,8 @@ export abstract class Plugin<O = any> implements PluginInterface {
async install(options?: InstallOptions) {} async install(options?: InstallOptions) {}
async beforeEnable() {}
async afterEnable() {} async afterEnable() {}
async afterDisable() {} async afterDisable() {}

View File

@ -114,6 +114,12 @@ class SubAppPlugin extends Plugin {
export class MultiAppShareCollectionPlugin extends Plugin { export class MultiAppShareCollectionPlugin extends Plugin {
afterAdd() {} afterAdd() {}
async beforeEnable() {
if (!this.db.inDialect('postgres')) {
throw new Error('multi-app-share-collection plugin only support postgres');
}
}
async beforeLoad() { async beforeLoad() {
if (!this.db.inDialect('postgres')) { if (!this.db.inDialect('postgres')) {
throw new Error('multi-app-share-collection plugin only support postgres'); throw new Error('multi-app-share-collection plugin only support postgres');