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', () => {
let app: MockServer;
beforeEach(() => {
beforeEach(async () => {
app = mockServer();
await app.db.clean({ drop: true });
await app.db.sync();
});
afterEach(async () => {
@ -108,4 +111,20 @@ describe('plugin', () => {
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({
filter: {
name,

View File

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

View File

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