@kanian77/choux 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/ChouxModule.d.ts +3 -0
- package/dist/core/ChouxModule.d.ts.map +1 -0
- package/{src/core/ChouxModule.ts → dist/core/ChouxModule.js} +11 -11
- package/dist/core/ChouxModule.js.map +1 -0
- package/dist/core/HookRegistry.d.ts +47 -0
- package/dist/core/HookRegistry.d.ts.map +1 -0
- package/dist/core/HookRegistry.js +79 -0
- package/dist/core/HookRegistry.js.map +1 -0
- package/dist/core/HookRegistry.spec.d.ts +2 -0
- package/dist/core/HookRegistry.spec.d.ts.map +1 -0
- package/dist/core/HookRegistry.spec.js +67 -0
- package/dist/core/HookRegistry.spec.js.map +1 -0
- package/dist/core/Plugin.d.ts +22 -0
- package/dist/core/Plugin.d.ts.map +1 -0
- package/dist/core/Plugin.js +4 -0
- package/dist/core/Plugin.js.map +1 -0
- package/dist/core/PluginLoader.d.ts +13 -0
- package/dist/core/PluginLoader.d.ts.map +1 -0
- package/dist/core/PluginLoader.js +147 -0
- package/dist/core/PluginLoader.js.map +1 -0
- package/dist/core/PluginLoader.spec.d.ts +2 -0
- package/dist/core/PluginLoader.spec.d.ts.map +1 -0
- package/dist/core/PluginLoader.spec.js +114 -0
- package/dist/core/PluginLoader.spec.js.map +1 -0
- package/dist/core/PluginManager.d.ts +12 -0
- package/dist/core/PluginManager.d.ts.map +1 -0
- package/dist/core/PluginManager.js +60 -0
- package/dist/core/PluginManager.js.map +1 -0
- package/dist/core/PluginManager.spec.d.ts +2 -0
- package/dist/core/PluginManager.spec.d.ts.map +1 -0
- package/dist/core/PluginManager.spec.js +50 -0
- package/dist/core/PluginManager.spec.js.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -0
- package/{src/core/index.ts → dist/core/index.js} +1 -0
- package/dist/core/index.js.map +1 -0
- package/dist/decorators/hookDecorator.d.ts +8 -0
- package/dist/decorators/hookDecorator.d.ts.map +1 -0
- package/dist/decorators/hookDecorator.js +22 -0
- package/dist/decorators/hookDecorator.js.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/{src/decorators/index.ts → dist/decorators/index.js} +1 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/example-plugin/module.d.ts.map +1 -0
- package/{src → dist}/example-plugin/module.js +9 -9
- package/dist/example-plugin/module.js.map +1 -0
- package/dist/example-plugin/services.d.ts.map +1 -0
- package/{src → dist}/example-plugin/services.js +2 -1
- package/dist/example-plugin/services.js.map +1 -0
- package/dist/example-plugin/tokens.d.ts.map +1 -0
- package/dist/example-plugin/tokens.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/{src/index.ts → dist/index.js} +1 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/functions/index.d.ts +3 -0
- package/dist/lib/functions/index.d.ts.map +1 -0
- package/{src/lib/functions/index.ts → dist/lib/functions/index.js} +1 -0
- package/dist/lib/functions/index.js.map +1 -0
- package/dist/lib/functions/registerHookHandler.d.ts +2 -0
- package/dist/lib/functions/registerHookHandler.d.ts.map +1 -0
- package/dist/lib/functions/registerHookHandler.js +7 -0
- package/dist/lib/functions/registerHookHandler.js.map +1 -0
- package/dist/lib/functions/registerHooksForInstance.d.ts +2 -0
- package/dist/lib/functions/registerHooksForInstance.d.ts.map +1 -0
- package/dist/lib/functions/registerHooksForInstance.js +22 -0
- package/dist/lib/functions/registerHooksForInstance.js.map +1 -0
- package/dist/lib/test-related/plugins/plugin-a/module.d.ts +21 -0
- package/dist/lib/test-related/plugins/plugin-a/module.d.ts.map +1 -0
- package/dist/lib/test-related/plugins/plugin-a/module.js +84 -0
- package/dist/lib/test-related/plugins/plugin-a/module.js.map +1 -0
- package/dist/lib/test-related/plugins/plugin-b/module.d.ts +21 -0
- package/dist/lib/test-related/plugins/plugin-b/module.d.ts.map +1 -0
- package/dist/lib/test-related/plugins/plugin-b/module.js +84 -0
- package/dist/lib/test-related/plugins/plugin-b/module.js.map +1 -0
- package/dist/lib/types/HookMetadata.d.ts +5 -0
- package/dist/lib/types/HookMetadata.d.ts.map +1 -0
- package/dist/lib/types/HookMetadata.js +1 -0
- package/dist/lib/types/HookMetadata.js.map +1 -0
- package/dist/lib/types/IHookRegistry.d.ts +10 -0
- package/dist/lib/types/IHookRegistry.d.ts.map +1 -0
- package/dist/lib/types/IHookRegistry.js +1 -0
- package/dist/lib/types/IHookRegistry.js.map +1 -0
- package/dist/lib/types/IPluginManager.d.ts +8 -0
- package/dist/lib/types/IPluginManager.d.ts.map +1 -0
- package/dist/lib/types/IPluginManager.js +1 -0
- package/dist/lib/types/IPluginManager.js.map +1 -0
- package/{src/lib/types/LoadedPlugin.ts → dist/lib/types/LoadedPlugin.d.ts} +4 -4
- package/dist/lib/types/LoadedPlugin.d.ts.map +1 -0
- package/dist/lib/types/LoadedPlugin.js +1 -0
- package/dist/lib/types/LoadedPlugin.js.map +1 -0
- package/dist/lib/types/PluginMetadata.d.ts +7 -0
- package/dist/lib/types/PluginMetadata.d.ts.map +1 -0
- package/dist/lib/types/PluginMetadata.js +1 -0
- package/dist/lib/types/PluginMetadata.js.map +1 -0
- package/dist/lib/types/index.d.ts +7 -0
- package/dist/lib/types/index.d.ts.map +1 -0
- package/{src/lib/types/index.ts → dist/lib/types/index.js} +1 -0
- package/dist/lib/types/index.js.map +1 -0
- package/dist/lib/types/tokens.d.ts +5 -0
- package/dist/lib/types/tokens.d.ts.map +1 -0
- package/{src/lib/types/tokens.ts → dist/lib/types/tokens.js} +1 -0
- package/dist/lib/types/tokens.js.map +1 -0
- package/package.json +1 -1
- package/src/core/HookRegistry.spec.ts +0 -75
- package/src/core/HookRegistry.ts +0 -78
- package/src/core/Plugin.ts +0 -26
- package/src/core/PluginLoader.spec.ts +0 -138
- package/src/core/PluginLoader.ts +0 -192
- package/src/core/PluginManager.spec.ts +0 -57
- package/src/core/PluginManager.ts +0 -47
- package/src/decorators/hookDecorator.ts +0 -30
- package/src/example-plugin/module.d.ts.map +0 -1
- package/src/example-plugin/module.js.map +0 -1
- package/src/example-plugin/module.ts +0 -80
- package/src/example-plugin/services.d.ts.map +0 -1
- package/src/example-plugin/services.js.map +0 -1
- package/src/example-plugin/services.ts +0 -22
- package/src/example-plugin/tokens.d.ts.map +0 -1
- package/src/example-plugin/tokens.js.map +0 -1
- package/src/example-plugin/tokens.ts +0 -1
- package/src/lib/functions/registerHookHandler.ts +0 -11
- package/src/lib/functions/registerHooksForInstance.ts +0 -26
- package/src/lib/test-related/plugins/plugin-a/module.ts +0 -74
- package/src/lib/test-related/plugins/plugin-b/module.ts +0 -74
- package/src/lib/types/HookMetadata.ts +0 -4
- package/src/lib/types/IHookRegistry.ts +0 -10
- package/src/lib/types/IPluginManager.ts +0 -8
- package/src/lib/types/PluginMetadata.ts +0 -9
- package/src/lib/types/README.md +0 -3
- /package/{src → dist}/example-plugin/module.d.ts +0 -0
- /package/{src → dist}/example-plugin/services.d.ts +0 -0
- /package/{src → dist}/example-plugin/tokens.d.ts +0 -0
- /package/{src → dist}/example-plugin/tokens.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=HookMetadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HookMetadata.js","sourceRoot":"","sources":["../../../src/lib/types/HookMetadata.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type HookFn = (...args: any[]) => void | Promise<void>;
|
|
2
|
+
export type HookMap = Map<string, HookFn[]>;
|
|
3
|
+
export interface IHookRegistry {
|
|
4
|
+
register(name: string, fn: HookFn): void;
|
|
5
|
+
trigger(name: string, ...args: any[]): Promise<void>;
|
|
6
|
+
unregister(name: string, fn?: HookFn): void;
|
|
7
|
+
getHooks(name: string): HookFn[];
|
|
8
|
+
clear(): void;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=IHookRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IHookRegistry.d.ts","sourceRoot":"","sources":["../../../src/lib/types/IHookRegistry.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAC9D,MAAM,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACzC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACjC,KAAK,IAAI,IAAI,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=IHookRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IHookRegistry.js","sourceRoot":"","sources":["../../../src/lib/types/IHookRegistry.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { LoadedPlugin } from './LoadedPlugin';
|
|
2
|
+
export interface IPluginManager {
|
|
3
|
+
initialize(pluginsDir: string): Promise<void>;
|
|
4
|
+
shutdown(): Promise<void>;
|
|
5
|
+
getPlugin(name: string): LoadedPlugin | undefined;
|
|
6
|
+
getAllPlugins(): LoadedPlugin[];
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=IPluginManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IPluginManager.d.ts","sourceRoot":"","sources":["../../../src/lib/types/IPluginManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9C,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAAC;IAClD,aAAa,IAAI,YAAY,EAAE,CAAC;CACjC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=IPluginManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IPluginManager.js","sourceRoot":"","sources":["../../../src/lib/types/IPluginManager.ts"],"names":[],"mappings":""}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ModuleOptions } from '@kanian77/simple-di';
|
|
2
2
|
import type { Plugin } from '../../core/Plugin';
|
|
3
3
|
import type { PluginMetadata } from './PluginMetadata';
|
|
4
|
-
|
|
5
4
|
export interface LoadedPlugin {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
plugin: Plugin;
|
|
6
|
+
metadata: PluginMetadata;
|
|
7
|
+
pluginClass: new <T extends ModuleOptions = ModuleOptions>(options: T) => Plugin;
|
|
9
8
|
}
|
|
9
|
+
//# sourceMappingURL=LoadedPlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoadedPlugin.d.ts","sourceRoot":"","sources":["../../../src/lib/types/LoadedPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,cAAc,CAAC;IACzB,WAAW,EAAE,KAAK,CAAC,SAAS,aAAa,GAAG,aAAa,EAAE,OAAO,EAAE,CAAC,KAAM,MAAM,CAAC;CACnF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=LoadedPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoadedPlugin.js","sourceRoot":"","sources":["../../../src/lib/types/LoadedPlugin.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PluginMetadata.d.ts","sourceRoot":"","sources":["../../../src/lib/types/PluginMetadata.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=PluginMetadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PluginMetadata.js","sourceRoot":"","sources":["../../../src/lib/types/PluginMetadata.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const PLUGIN_MANAGER_TOKEN: unique symbol;
|
|
2
|
+
export declare const HOOK_REGISTRY_TOKEN: unique symbol;
|
|
3
|
+
export declare const PLUGIN_METADATA_TOKEN: unique symbol;
|
|
4
|
+
export declare const HOOK_METADATA_KEY: unique symbol;
|
|
5
|
+
//# sourceMappingURL=tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.d.ts","sourceRoot":"","sources":["../../../src/lib/types/tokens.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,eAA0B,CAAC;AAC5D,eAAO,MAAM,mBAAmB,eAAyB,CAAC;AAC1D,eAAO,MAAM,qBAAqB,eAA2B,CAAC;AAC9D,eAAO,MAAM,iBAAiB,eAAyB,CAAC"}
|
|
@@ -2,3 +2,4 @@ export const PLUGIN_MANAGER_TOKEN = Symbol('PluginManager');
|
|
|
2
2
|
export const HOOK_REGISTRY_TOKEN = Symbol('HookRegistry');
|
|
3
3
|
export const PLUGIN_METADATA_TOKEN = Symbol('PluginMetadata');
|
|
4
4
|
export const HOOK_METADATA_KEY = Symbol('hookMetadata');
|
|
5
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../../src/lib/types/tokens.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAC5D,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAC1D,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAC9D,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'bun:test';
|
|
2
|
-
import { HookRegistry } from './HookRegistry';
|
|
3
|
-
import type { HookFn } from '../lib/types';
|
|
4
|
-
|
|
5
|
-
describe('HookRegistry', () => {
|
|
6
|
-
let registry: HookRegistry;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
registry = new HookRegistry();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('registers and triggers a hook', async () => {
|
|
13
|
-
let called = false;
|
|
14
|
-
const fn: HookFn = () => {
|
|
15
|
-
called = true;
|
|
16
|
-
};
|
|
17
|
-
registry.register('test:hook', fn);
|
|
18
|
-
await registry.trigger('test:hook');
|
|
19
|
-
expect(called).toBe(true);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('unregisters a specific hook handler', async () => {
|
|
23
|
-
let called = false;
|
|
24
|
-
const fn: HookFn = () => {
|
|
25
|
-
called = true;
|
|
26
|
-
};
|
|
27
|
-
registry.register('test:hook', fn);
|
|
28
|
-
registry.unregister('test:hook', fn);
|
|
29
|
-
await registry.trigger('test:hook');
|
|
30
|
-
expect(called).toBe(false);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('unregisters all handlers for a hook', async () => {
|
|
34
|
-
let called = false;
|
|
35
|
-
const fn: HookFn = () => {
|
|
36
|
-
called = true;
|
|
37
|
-
};
|
|
38
|
-
registry.register('test:hook', fn);
|
|
39
|
-
registry.unregister('test:hook');
|
|
40
|
-
await registry.trigger('test:hook');
|
|
41
|
-
expect(called).toBe(false);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('getHooks returns all registered handlers', () => {
|
|
45
|
-
const fn1: HookFn = () => {};
|
|
46
|
-
const fn2: HookFn = () => {};
|
|
47
|
-
registry.register('test:hook', fn1);
|
|
48
|
-
registry.register('test:hook', fn2);
|
|
49
|
-
const hooks = registry.getHooks('test:hook');
|
|
50
|
-
expect(hooks.length).toBe(2);
|
|
51
|
-
expect(hooks).toContain(fn1);
|
|
52
|
-
expect(hooks).toContain(fn2);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('clear removes all hooks', () => {
|
|
56
|
-
const fn: HookFn = () => {};
|
|
57
|
-
registry.register('test:hook', fn);
|
|
58
|
-
registry.clear();
|
|
59
|
-
expect(registry.getHooks('test:hook').length).toBe(0);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('trigger calls all registered async handlers', async () => {
|
|
63
|
-
let result = '';
|
|
64
|
-
const fn1: HookFn = async () => {
|
|
65
|
-
result += 'a';
|
|
66
|
-
};
|
|
67
|
-
const fn2: HookFn = async () => {
|
|
68
|
-
result += 'b';
|
|
69
|
-
};
|
|
70
|
-
registry.register('test:hook', fn1);
|
|
71
|
-
registry.register('test:hook', fn2);
|
|
72
|
-
await registry.trigger('test:hook');
|
|
73
|
-
expect(result).toBe('ab');
|
|
74
|
-
});
|
|
75
|
-
});
|
package/src/core/HookRegistry.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Service } from '@kanian77/simple-di';
|
|
2
|
-
import { HOOK_REGISTRY_TOKEN } from '../lib/types/tokens';
|
|
3
|
-
import type { HookFn, HookMap, IHookRegistry } from '../lib/types';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Registry for application hooks.
|
|
7
|
-
*/
|
|
8
|
-
@Service({ token: HOOK_REGISTRY_TOKEN })
|
|
9
|
-
export class HookRegistry implements IHookRegistry {
|
|
10
|
-
private hooks: HookMap = new Map();
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Register a hook handler.
|
|
14
|
-
* @param name The name of the hook to register the function for.
|
|
15
|
-
* @param fn The hook handler function.
|
|
16
|
-
*/
|
|
17
|
-
register(name: string, fn: HookFn): void {
|
|
18
|
-
const list = this.hooks.get(name) || [];
|
|
19
|
-
this.hooks.set(name, [...list, fn]);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Trigger a hook, calling all registered handlers.
|
|
24
|
-
* @param name The name of the hook to trigger.
|
|
25
|
-
* @param args Arguments to pass to the hook handlers.
|
|
26
|
-
*/
|
|
27
|
-
async trigger(name: string, ...args: any[]): Promise<void> {
|
|
28
|
-
const fns = this.hooks.get(name) || [];
|
|
29
|
-
for (const fn of fns) {
|
|
30
|
-
await fn(...args);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Unregister a hook handler.
|
|
36
|
-
* If no function is provided, all handlers for the hook will be removed.
|
|
37
|
-
* @param name The name of the hook to unregister from.
|
|
38
|
-
* @param fn Optional specific handler to remove.
|
|
39
|
-
*/
|
|
40
|
-
unregister(name: string, fn?: HookFn): void {
|
|
41
|
-
if (!fn) {
|
|
42
|
-
this.hooks.delete(name);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const list = this.hooks.get(name) || [];
|
|
47
|
-
const filtered = list.filter((registeredFn) => registeredFn !== fn);
|
|
48
|
-
this.hooks.set(name, filtered);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Get all registered handlers for a specific hook.
|
|
53
|
-
* @param name The name of the hook to retrieve handlers for.
|
|
54
|
-
* @returns An array of hook handler functions.
|
|
55
|
-
*/
|
|
56
|
-
getHooks(name: string): HookFn[] {
|
|
57
|
-
return this.hooks.get(name) || [];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Clear all registered hooks.
|
|
62
|
-
*/
|
|
63
|
-
clear(): void {
|
|
64
|
-
this.hooks.clear();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Standard lifecycle hooks
|
|
69
|
-
export const LIFECYCLE_HOOKS = {
|
|
70
|
-
BEFORE_PLUGIN_LOAD: 'core:before-plugin-load',
|
|
71
|
-
AFTER_PLUGIN_LOAD: 'core:after-plugin-load',
|
|
72
|
-
BEFORE_PLUGIN_INIT: 'core:before-plugin-init',
|
|
73
|
-
AFTER_PLUGIN_INIT: 'core:after-plugin-init',
|
|
74
|
-
BEFORE_PLUGIN_DESTROY: 'core:before-plugin-destroy',
|
|
75
|
-
AFTER_PLUGIN_DESTROY: 'core:after-plugin-destroy',
|
|
76
|
-
APPLICATION_START: 'core:application-start',
|
|
77
|
-
APPLICATION_SHUTDOWN: 'core:application-shutdown',
|
|
78
|
-
} as const;
|
package/src/core/Plugin.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Module } from '@kanian77/simple-di';
|
|
2
|
-
import type { PluginMetadata } from '../lib/types';
|
|
3
|
-
|
|
4
|
-
export abstract class Plugin extends Module {
|
|
5
|
-
abstract readonly metadata: PluginMetadata;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Called when plugin is loaded but before dependencies are resolved
|
|
9
|
-
*/
|
|
10
|
-
async onLoad?(): Promise<void>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Called after all dependencies are resolved and injected
|
|
14
|
-
*/
|
|
15
|
-
async onInit?(): Promise<void>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Called when plugin is being unloaded
|
|
19
|
-
*/
|
|
20
|
-
async onDestroy?(): Promise<void>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Register custom hooks specific to this plugin
|
|
24
|
-
*/
|
|
25
|
-
protected registerCustomHooks?(): void;
|
|
26
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
|
2
|
-
import { PluginLoader } from './PluginLoader';
|
|
3
|
-
import { HOOK_REGISTRY_TOKEN } from '../lib/types/tokens';
|
|
4
|
-
import { LIFECYCLE_HOOKS } from './HookRegistry';
|
|
5
|
-
import type { IHookRegistry, LoadedPlugin } from '../lib/types';
|
|
6
|
-
import { Plugin } from './Plugin';
|
|
7
|
-
import { tmpdir } from 'os';
|
|
8
|
-
import { mkdtempSync } from 'fs';
|
|
9
|
-
// Minimal IHookRegistry mock
|
|
10
|
-
const mockHookRegistry: IHookRegistry = {
|
|
11
|
-
trigger: async () => {},
|
|
12
|
-
register: () => {},
|
|
13
|
-
unregister: () => {},
|
|
14
|
-
getHooks: () => [],
|
|
15
|
-
clear: () => {},
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// // Patch inject to return our mock
|
|
19
|
-
// import * as simpleDi from "@kanian77/simple-di";
|
|
20
|
-
// (simpleDi as any).inject = () => mockHookRegistry;
|
|
21
|
-
|
|
22
|
-
// Patch fs.promises for plugin discovery
|
|
23
|
-
import { mkdir, writeFile, rm } from 'fs/promises';
|
|
24
|
-
import { join, resolve } from 'path';
|
|
25
|
-
|
|
26
|
-
// Helper for plugin mocks
|
|
27
|
-
class PluginMock extends Plugin {
|
|
28
|
-
override metadata = { name: 'pluginA', version: '1.0.0' };
|
|
29
|
-
override imports = [];
|
|
30
|
-
override providers = [];
|
|
31
|
-
override async onLoad() {}
|
|
32
|
-
override async onInit() {}
|
|
33
|
-
override async onDestroy() {}
|
|
34
|
-
override registerCustomHooks() {}
|
|
35
|
-
}
|
|
36
|
-
class PluginMockA extends Plugin {
|
|
37
|
-
override metadata = { name: 'A', version: '1.0.0', dependencies: ['B'] };
|
|
38
|
-
override imports = [];
|
|
39
|
-
override providers = [];
|
|
40
|
-
}
|
|
41
|
-
class PluginMockB extends Plugin {
|
|
42
|
-
override metadata = { name: 'B', version: '1.0.0', dependencies: ['A'] };
|
|
43
|
-
override imports = [];
|
|
44
|
-
override providers = [];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
describe('PluginLoader', () => {
|
|
48
|
-
let loader: PluginLoader;
|
|
49
|
-
let tempRoot: string;
|
|
50
|
-
|
|
51
|
-
beforeEach(async () => {
|
|
52
|
-
loader = new PluginLoader();
|
|
53
|
-
tempRoot = mkdtempSync(join(tmpdir(), 'plugin-test-'));
|
|
54
|
-
|
|
55
|
-
// Create pluginA with module.ts
|
|
56
|
-
const pluginADir = join(tempRoot, 'pluginA');
|
|
57
|
-
await mkdir(pluginADir);
|
|
58
|
-
await writeFile(join(pluginADir, 'module.ts'), '// pluginA module');
|
|
59
|
-
|
|
60
|
-
// Create pluginB without module.ts
|
|
61
|
-
const pluginBDir = join(tempRoot, 'pluginB');
|
|
62
|
-
await mkdir(pluginBDir);
|
|
63
|
-
|
|
64
|
-
// Create a non-directory file
|
|
65
|
-
await writeFile(join(tempRoot, 'file.txt'), 'not a directory');
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
afterEach(async () => {
|
|
69
|
-
await rm(tempRoot, { recursive: true, force: true });
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('findPlugins returns only directories with module.ts', async () => {
|
|
73
|
-
const plugins = await loader.findPlugins('./src/lib/test-related/plugins');
|
|
74
|
-
|
|
75
|
-
expect(plugins).toEqual([
|
|
76
|
-
'src/lib/test-related/plugins/plugin-a',
|
|
77
|
-
'src/lib/test-related/plugins/plugin-b',
|
|
78
|
-
]);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('loadPlugin throws if no default export', async () => {
|
|
82
|
-
const origJoin = require('path').join;
|
|
83
|
-
require('path').join = (...args: string[]) => args.join('/');
|
|
84
|
-
const origImport = (global as any).import;
|
|
85
|
-
(global as any).import = async (_: string) => ({});
|
|
86
|
-
await expect(
|
|
87
|
-
loader.loadPlugin('./src/lib/test-related/plugins/plugin-a')
|
|
88
|
-
).rejects.toThrow();
|
|
89
|
-
(global as any).import = origImport;
|
|
90
|
-
require('path').join = origJoin;
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('loadPlugin loads valid plugin and triggers hooks', async () => {
|
|
94
|
-
const modulePath = resolve('src/lib/test-related/plugins/plugin-a');
|
|
95
|
-
const loaded = await loader.loadPlugin(modulePath);
|
|
96
|
-
expect(loaded.plugin).toBeInstanceOf(
|
|
97
|
-
await import(join(modulePath, 'module.ts')).then((m) => m.default)
|
|
98
|
-
);
|
|
99
|
-
expect(loaded.metadata.name).toBe('pluginA');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('unloadPlugin triggers destroy hooks and removes plugin', async () => {
|
|
103
|
-
const loaded: LoadedPlugin = {
|
|
104
|
-
plugin: new PluginMock({}),
|
|
105
|
-
metadata: { name: 'pluginA', version: '1.0.0' },
|
|
106
|
-
pluginClass: PluginMock,
|
|
107
|
-
};
|
|
108
|
-
loader['loadedPlugins'].set('pluginA', loaded);
|
|
109
|
-
await loader.unloadPlugin('pluginA');
|
|
110
|
-
expect(loader.getPlugin('pluginA')).toBeUndefined();
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('getAllPlugins returns all loaded plugins', () => {
|
|
114
|
-
const loaded: LoadedPlugin = {
|
|
115
|
-
plugin: new PluginMock({}),
|
|
116
|
-
metadata: { name: 'pluginA', version: '1.0.0' },
|
|
117
|
-
pluginClass: PluginMock,
|
|
118
|
-
};
|
|
119
|
-
loader['loadedPlugins'].set('pluginA', loaded);
|
|
120
|
-
expect(loader.getAllPlugins()).toEqual([loaded]);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('resolveDependencyOrder throws on circular dependency', () => {
|
|
124
|
-
const pluginA: LoadedPlugin = {
|
|
125
|
-
plugin: new PluginMockA({}),
|
|
126
|
-
metadata: { name: 'A', version: '1.0.0', dependencies: ['B'] },
|
|
127
|
-
pluginClass: PluginMockA,
|
|
128
|
-
};
|
|
129
|
-
const pluginB: LoadedPlugin = {
|
|
130
|
-
plugin: new PluginMockB({}),
|
|
131
|
-
metadata: { name: 'B', version: '1.0.0', dependencies: ['A'] },
|
|
132
|
-
pluginClass: PluginMockB,
|
|
133
|
-
};
|
|
134
|
-
expect(() =>
|
|
135
|
-
loader['resolveDependencyOrder']([pluginA, pluginB])
|
|
136
|
-
).toThrow();
|
|
137
|
-
});
|
|
138
|
-
});
|
package/src/core/PluginLoader.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { Module, inject } from '@kanian77/simple-di';
|
|
2
|
-
import { Plugin } from './Plugin';
|
|
3
|
-
import type { PluginMetadata, LoadedPlugin, IHookRegistry } from '../lib/types';
|
|
4
|
-
import { LIFECYCLE_HOOKS } from './HookRegistry';
|
|
5
|
-
import { HOOK_REGISTRY_TOKEN } from '../lib/types/tokens';
|
|
6
|
-
import * as fs from 'fs/promises';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
|
|
9
|
-
// LoadedPlugin now imported from types
|
|
10
|
-
|
|
11
|
-
export class PluginLoader {
|
|
12
|
-
private loadedPlugins: Map<string, LoadedPlugin> = new Map();
|
|
13
|
-
|
|
14
|
-
async findPlugins(pluginsDir: string): Promise<string[]> {
|
|
15
|
-
try {
|
|
16
|
-
const entries = await fs.readdir(pluginsDir, { withFileTypes: true });
|
|
17
|
-
const pluginDirs: string[] = [];
|
|
18
|
-
|
|
19
|
-
for (const entry of entries) {
|
|
20
|
-
if (entry.isDirectory()) {
|
|
21
|
-
const pluginPath = path.join(pluginsDir, entry.name);
|
|
22
|
-
const modulePath = path.join(pluginPath, 'module.ts');
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
await fs.access(modulePath);
|
|
26
|
-
pluginDirs.push(pluginPath);
|
|
27
|
-
} catch {
|
|
28
|
-
// Skip directories without module.ts
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return pluginDirs;
|
|
34
|
-
} catch (error) {
|
|
35
|
-
console.warn(`Could not read plugins directory: ${pluginsDir}`, error);
|
|
36
|
-
return [];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async loadPlugin(pluginPath: string): Promise<LoadedPlugin> {
|
|
41
|
-
const modulePath = path.join(pluginPath, 'module.ts');
|
|
42
|
-
console.log(`Loading plugin from ${modulePath}`);
|
|
43
|
-
const hookRegistry = inject<IHookRegistry>(HOOK_REGISTRY_TOKEN);
|
|
44
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.BEFORE_PLUGIN_LOAD, pluginPath);
|
|
45
|
-
|
|
46
|
-
// Dynamic import of the plugin module
|
|
47
|
-
const module = await import(modulePath);
|
|
48
|
-
const PluginClass = module.default;
|
|
49
|
-
|
|
50
|
-
if (!PluginClass || !PluginClass.prototype) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`Invalid plugin at ${pluginPath}: must export a Plugin class as default`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Create plugin instance (which is also a Module)
|
|
57
|
-
const plugin = new PluginClass();
|
|
58
|
-
|
|
59
|
-
if (!plugin.metadata) {
|
|
60
|
-
throw new Error(`Plugin at ${pluginPath} must have metadata property`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Call plugin's onLoad lifecycle method
|
|
64
|
-
if (plugin.onLoad) {
|
|
65
|
-
await plugin.onLoad();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Register custom hooks if defined
|
|
69
|
-
if (plugin.registerCustomHooks) {
|
|
70
|
-
plugin.registerCustomHooks();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const loadedPlugin: LoadedPlugin = {
|
|
74
|
-
plugin, // plugin is already a Module
|
|
75
|
-
metadata: plugin.metadata,
|
|
76
|
-
pluginClass: PluginClass,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
this.loadedPlugins.set(plugin.metadata.name, loadedPlugin);
|
|
80
|
-
|
|
81
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.AFTER_PLUGIN_LOAD, loadedPlugin);
|
|
82
|
-
|
|
83
|
-
return loadedPlugin;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async loadPlugins(pluginsDir: string): Promise<LoadedPlugin[]> {
|
|
87
|
-
const pluginDirs = await this.findPlugins(pluginsDir);
|
|
88
|
-
const loadedPlugins: LoadedPlugin[] = [];
|
|
89
|
-
|
|
90
|
-
// Load all plugins first
|
|
91
|
-
for (const dir of pluginDirs) {
|
|
92
|
-
try {
|
|
93
|
-
const loaded = await this.loadPlugin(dir);
|
|
94
|
-
loadedPlugins.push(loaded);
|
|
95
|
-
} catch (error) {
|
|
96
|
-
console.error(`Failed to load plugin from ${dir}:`, error);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Resolve dependencies and initialize
|
|
101
|
-
const sortedPlugins = this.resolveDependencyOrder(loadedPlugins);
|
|
102
|
-
|
|
103
|
-
for (const loaded of sortedPlugins) {
|
|
104
|
-
await this.initializePlugin(loaded);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return sortedPlugins;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private resolveDependencyOrder(plugins: LoadedPlugin[]): LoadedPlugin[] {
|
|
111
|
-
const sorted: LoadedPlugin[] = [];
|
|
112
|
-
const visited = new Set<string>();
|
|
113
|
-
const visiting = new Set<string>();
|
|
114
|
-
|
|
115
|
-
const visit = (plugin: LoadedPlugin) => {
|
|
116
|
-
const name = plugin.metadata.name;
|
|
117
|
-
|
|
118
|
-
if (visiting.has(name)) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
`Circular dependency detected involving plugin: ${name}`
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (visited.has(name)) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
visiting.add(name);
|
|
129
|
-
|
|
130
|
-
// Visit dependencies first
|
|
131
|
-
const dependencies = plugin.metadata.dependencies || [];
|
|
132
|
-
for (const depName of dependencies) {
|
|
133
|
-
const dep = plugins.find((p) => p.metadata.name === depName);
|
|
134
|
-
if (!dep) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
`Plugin ${name} depends on ${depName}, but it's not loaded`
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
visit(dep);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
visiting.delete(name);
|
|
143
|
-
visited.add(name);
|
|
144
|
-
sorted.push(plugin);
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
for (const plugin of plugins) {
|
|
148
|
-
visit(plugin);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return sorted;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
private async initializePlugin(loaded: LoadedPlugin): Promise<void> {
|
|
155
|
-
const hookRegistry = inject<IHookRegistry>(HOOK_REGISTRY_TOKEN);
|
|
156
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.BEFORE_PLUGIN_INIT, loaded);
|
|
157
|
-
|
|
158
|
-
// Call plugin's onInit lifecycle method
|
|
159
|
-
if (loaded.plugin.onInit) {
|
|
160
|
-
await loaded.plugin.onInit();
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.AFTER_PLUGIN_INIT, loaded);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async unloadPlugin(pluginName: string): Promise<void> {
|
|
167
|
-
const loaded = this.loadedPlugins.get(pluginName);
|
|
168
|
-
if (!loaded) {
|
|
169
|
-
throw new Error(`Plugin ${pluginName} is not loaded`);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const hookRegistry = inject<IHookRegistry>(HOOK_REGISTRY_TOKEN);
|
|
173
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.BEFORE_PLUGIN_DESTROY, loaded);
|
|
174
|
-
|
|
175
|
-
// Call plugin's onDestroy lifecycle method
|
|
176
|
-
if (loaded.plugin.onDestroy) {
|
|
177
|
-
await loaded.plugin.onDestroy();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
this.loadedPlugins.delete(pluginName);
|
|
181
|
-
|
|
182
|
-
await hookRegistry.trigger(LIFECYCLE_HOOKS.AFTER_PLUGIN_DESTROY, loaded);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
getPlugin(name: string): LoadedPlugin | undefined {
|
|
186
|
-
return this.loadedPlugins.get(name);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
getAllPlugins(): LoadedPlugin[] {
|
|
190
|
-
return Array.from(this.loadedPlugins.values());
|
|
191
|
-
}
|
|
192
|
-
}
|