diff --git a/packages/core/server/src/__tests__/pm.test.ts b/packages/core/server/src/__tests__/pm.test.ts index 53ad9a2ce..983dc54c7 100644 --- a/packages/core/server/src/__tests__/pm.test.ts +++ b/packages/core/server/src/__tests__/pm.test.ts @@ -399,4 +399,70 @@ describe('pm', () => { expect(record.installed).toBeTruthy(); PluginManager.resolvePlugin = resolvePlugin; }); + + test('life-cycle', async () => { + const resolvePlugin = PluginManager.resolvePlugin; + PluginManager.resolvePlugin = (pluginName) => { + return Plugin1; + }; + const hooks = []; + const result = []; + class Plugin1 extends Plugin { + prop: any; + async afterAdd() { + hooks.push('afterAdd'); + } + async beforeLoad() { + hooks.push('beforeLoad'); + } + async load() { + this.prop = 'a'; + hooks.push('load'); + } + async beforeEnable() { + hooks.push('beforeEnable'); + result.push(this.prop === 'a'); + } + async install() { + hooks.push('install'); + result.push(this.prop === 'a'); + } + async afterEnable() { + hooks.push('afterEnable'); + result.push(this.prop === 'a'); + } + async beforeDisable() { + hooks.push('beforeDisable'); + result.push(this.prop === 'a'); + } + async afterDisable() { + hooks.push('afterDisable'); + result.push(this.prop === 'a'); + } + } + app = mockServer(); + await app.load(); + await app.install(); + await app.pm.repository.create({ + values: { + name: 'Plugin1', + // enabled: true, + // installed: true, + }, + }); + await app.reload(); + // console.log(hooks); + expect(app.pm.get('Plugin1')['prop']).toBeUndefined(); + expect(result).toEqual([]); + await app.pm.enable('Plugin1'); + expect(app.pm.get('Plugin1')['prop']).toBe('a'); + // console.log(hooks.join('/')); + expect(result).toEqual([false, true, true]); + await app.pm.disable('Plugin1'); + // console.log(hooks.join('/')); + expect(app.pm.get('Plugin1')['prop']).toBeUndefined(); + expect(result).toEqual([false, true, true, true, false]); + // console.log(hooks.join('/')); + PluginManager.resolvePlugin = resolvePlugin; + }); }); diff --git a/packages/core/server/src/plugin-manager/plugin-manager.ts b/packages/core/server/src/plugin-manager/plugin-manager.ts index c1f516572..4416cad70 100644 --- a/packages/core/server/src/plugin-manager/plugin-manager.ts +++ b/packages/core/server/src/plugin-manager/plugin-manager.ts @@ -141,32 +141,32 @@ export class PluginManager { } getPlugins() { - return this.pluginInstances; + return this.app.pm.pluginInstances; } getAliases() { - return this.pluginAliases.keys(); + return this.app.pm.pluginAliases.keys(); } get(name: string | typeof Plugin) { if (typeof name === 'string') { - return this.pluginAliases.get(name); + return this.app.pm.pluginAliases.get(name); } - return this.pluginInstances.get(name); + return this.app.pm.pluginInstances.get(name); } has(name: string | typeof Plugin) { if (typeof name === 'string') { - return this.pluginAliases.has(name); + return this.app.pm.pluginAliases.has(name); } - return this.pluginInstances.has(name); + return this.app.pm.pluginInstances.has(name); } del(name: string | typeof Plugin) { const instance = this.get(name); if (instance) { - this.pluginAliases.delete(instance.name); - this.pluginInstances.delete(instance.constructor as typeof Plugin); + this.app.pm.pluginAliases.delete(instance.name); + this.app.pm.pluginInstances.delete(instance.constructor as typeof Plugin); } }