@kairojsmodule/plugins 0.0.0-stage → 1.1.0
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/index.d.ts +19 -0
- package/package.json +20 -4
- package/src/Plugin.js +15 -0
- package/src/PluginManager.js +60 -0
- package/src/index.js +2 -0
- package/test/plugins.test.js +48 -0
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare abstract class Plugin {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
description: string;
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
constructor(manifest?: { name?: string; version?: string; description?: string; author?: string });
|
|
7
|
+
register(app: any): Promise<void> | void;
|
|
8
|
+
boot(app: any): Promise<void> | void;
|
|
9
|
+
shutdown(app: any): Promise<void> | void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export declare class PluginManager {
|
|
13
|
+
constructor(app?: any);
|
|
14
|
+
register(plugin: Plugin | (new () => Plugin)): Promise<Plugin>;
|
|
15
|
+
boot(): Promise<void>;
|
|
16
|
+
get(name: string): Plugin | undefined;
|
|
17
|
+
has(name: string): boolean;
|
|
18
|
+
list(): Array<{ name: string; version: string; description: string; enabled: boolean }>;
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairojsmodule/plugins",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Plugin manifest specification and lifecycle manager for KairoJS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"kairojs",
|
|
13
|
+
"plugins",
|
|
14
|
+
"modular",
|
|
15
|
+
"extensions",
|
|
16
|
+
"ecosystem"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"author": "Riski Ardiane <@kairojsmodule>"
|
|
22
|
+
}
|
package/src/Plugin.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class Plugin {
|
|
2
|
+
constructor(manifest = {}) {
|
|
3
|
+
this.name = manifest.name || this.constructor.name;
|
|
4
|
+
this.version = manifest.version || '1.0.0';
|
|
5
|
+
this.description = manifest.description || '';
|
|
6
|
+
this.author = manifest.author || '';
|
|
7
|
+
this.enabled = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async register(app) {}
|
|
11
|
+
|
|
12
|
+
async boot(app) {}
|
|
13
|
+
|
|
14
|
+
async shutdown(app) {}
|
|
15
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Plugin } from './Plugin.js';
|
|
2
|
+
|
|
3
|
+
export class PluginManager {
|
|
4
|
+
constructor(app = null) {
|
|
5
|
+
this.app = app;
|
|
6
|
+
this.plugins = new Map();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Register a plugin.
|
|
11
|
+
* @param {Plugin|typeof Plugin} PluginClassOrInstance
|
|
12
|
+
*/
|
|
13
|
+
async register(PluginClassOrInstance) {
|
|
14
|
+
const plugin =
|
|
15
|
+
typeof PluginClassOrInstance === 'function'
|
|
16
|
+
? new PluginClassOrInstance()
|
|
17
|
+
: PluginClassOrInstance;
|
|
18
|
+
|
|
19
|
+
if (!plugin.name) {
|
|
20
|
+
throw new Error('Plugin must declare a name property.');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (this.plugins.has(plugin.name)) {
|
|
24
|
+
throw new Error(`Plugin '${plugin.name}' is already registered.`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.plugins.set(plugin.name, plugin);
|
|
28
|
+
|
|
29
|
+
if (typeof plugin.register === 'function' && this.app) {
|
|
30
|
+
await plugin.register(this.app);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return plugin;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async boot() {
|
|
37
|
+
for (const plugin of this.plugins.values()) {
|
|
38
|
+
if (plugin.enabled && typeof plugin.boot === 'function' && this.app) {
|
|
39
|
+
await plugin.boot(this.app);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get(name) {
|
|
45
|
+
return this.plugins.get(name);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
has(name) {
|
|
49
|
+
return this.plugins.has(name);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
list() {
|
|
53
|
+
return Array.from(this.plugins.values()).map((p) => ({
|
|
54
|
+
name: p.name,
|
|
55
|
+
version: p.version,
|
|
56
|
+
description: p.description,
|
|
57
|
+
enabled: p.enabled
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { Plugin, PluginManager } from '../src/index.js';
|
|
4
|
+
|
|
5
|
+
describe('Plugin System', () => {
|
|
6
|
+
test('registers and boots plugins', async () => {
|
|
7
|
+
const app = {
|
|
8
|
+
services: new Map(),
|
|
9
|
+
bind(k, v) {
|
|
10
|
+
this.services.set(k, v);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const manager = new PluginManager(app);
|
|
15
|
+
const bootCalls = [];
|
|
16
|
+
|
|
17
|
+
class StripeBillingPlugin extends Plugin {
|
|
18
|
+
constructor() {
|
|
19
|
+
super({
|
|
20
|
+
name: 'stripe-billing',
|
|
21
|
+
version: '1.2.0',
|
|
22
|
+
description: 'Official Stripe billing provider'
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async register(a) {
|
|
27
|
+
a.bind('stripe', { client: 'active' });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async boot(a) {
|
|
31
|
+
bootCalls.push('stripe-booted');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await manager.register(StripeBillingPlugin);
|
|
36
|
+
|
|
37
|
+
assert.equal(manager.has('stripe-billing'), true);
|
|
38
|
+
assert.deepEqual(app.services.get('stripe'), { client: 'active' });
|
|
39
|
+
|
|
40
|
+
await manager.boot();
|
|
41
|
+
assert.deepEqual(bootCalls, ['stripe-booted']);
|
|
42
|
+
|
|
43
|
+
const list = manager.list();
|
|
44
|
+
assert.equal(list.length, 1);
|
|
45
|
+
assert.equal(list[0].name, 'stripe-billing');
|
|
46
|
+
assert.equal(list[0].version, '1.2.0');
|
|
47
|
+
});
|
|
48
|
+
});
|
package/README.md
DELETED