-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin-manager.ts
More file actions
267 lines (223 loc) · 7.56 KB
/
plugin-manager.ts
File metadata and controls
267 lines (223 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Plugin Manager
*
* Manages the plugin lifecycle according to @objectstack/spec/system.
* Handles plugin registration, initialization, and lifecycle hooks.
*/
import type {
PluginDefinition,
PluginContextData,
ObjectStackManifest
} from '@objectstack/spec/system';
import { Logger, createLogger } from './logger';
import { ScopedStorage } from './scoped-storage';
export interface PluginEntry {
manifest: ObjectStackManifest;
definition: PluginDefinition;
enabled: boolean;
installed: boolean;
}
export type PluginLifecycleHook = 'onInstall' | 'onEnable' | 'onLoad' | 'onDisable' | 'onUninstall';
/**
* Plugin Manager
*
* Coordinates plugin lifecycle and maintains the plugin registry.
*/
export class PluginManager {
private plugins: Map<string, PluginEntry> = new Map();
private logger: Logger;
private contextBuilder: (pluginId: string) => PluginContextData;
constructor(contextBuilder: (pluginId: string) => PluginContextData) {
this.logger = createLogger('PluginManager');
this.contextBuilder = contextBuilder;
}
/**
* Register a plugin with its manifest.
*
* @param manifest - Plugin manifest (static configuration)
* @param definition - Plugin definition (lifecycle hooks)
*/
register(manifest: ObjectStackManifest, definition: PluginDefinition): void {
const { id } = manifest;
if (this.plugins.has(id)) {
this.logger.warn(`Plugin '${id}' is already registered. Skipping.`);
return;
}
this.plugins.set(id, {
manifest,
definition,
enabled: false,
installed: false,
});
this.logger.info(`Registered plugin: ${id} v${manifest.version}`);
}
/**
* Install a plugin (first-time setup).
* Calls the onInstall lifecycle hook.
*/
async install(pluginId: string): Promise<void> {
const entry = this.plugins.get(pluginId);
if (!entry) {
throw new Error(`Plugin '${pluginId}' not found`);
}
if (entry.installed) {
this.logger.warn(`Plugin '${pluginId}' is already installed`);
return;
}
this.logger.info(`Installing plugin: ${pluginId}`);
try {
const context = this.contextBuilder(pluginId);
if (entry.definition.onInstall) {
await entry.definition.onInstall(context);
}
entry.installed = true;
this.logger.info(`Installed plugin: ${pluginId}`);
} catch (error) {
this.logger.error(`Failed to install plugin '${pluginId}'`, error as Error);
throw error;
}
}
/**
* Enable a plugin.
* Calls the onEnable lifecycle hook.
*/
async enable(pluginId: string): Promise<void> {
const entry = this.plugins.get(pluginId);
if (!entry) {
throw new Error(`Plugin '${pluginId}' not found`);
}
if (!entry.installed) {
this.logger.info(`Plugin '${pluginId}' not installed, installing first`);
await this.install(pluginId);
}
if (entry.enabled) {
this.logger.warn(`Plugin '${pluginId}' is already enabled`);
return;
}
this.logger.info(`Enabling plugin: ${pluginId}`);
try {
const context = this.contextBuilder(pluginId);
if (entry.definition.onEnable) {
await entry.definition.onEnable(context);
}
entry.enabled = true;
this.logger.info(`Enabled plugin: ${pluginId}`);
} catch (error) {
this.logger.error(`Failed to enable plugin '${pluginId}'`, error as Error);
throw error;
}
}
/**
* Load plugin metadata.
* Note: onLoad is optional in the spec and not all plugins may implement it.
*/
async load(pluginId: string): Promise<void> {
const entry = this.plugins.get(pluginId);
if (!entry) {
throw new Error(`Plugin '${pluginId}' not found`);
}
this.logger.debug(`Loading metadata for plugin: ${pluginId}`);
try {
const context = this.contextBuilder(pluginId);
// onLoad is optional, check if it exists before calling
const onLoadHook = (entry.definition as any).onLoad;
if (onLoadHook && typeof onLoadHook === 'function') {
await onLoadHook(context);
}
} catch (error) {
this.logger.error(`Failed to load plugin '${pluginId}'`, error as Error);
throw error;
}
}
/**
* Disable a plugin.
* Calls the onDisable lifecycle hook.
*/
async disable(pluginId: string): Promise<void> {
const entry = this.plugins.get(pluginId);
if (!entry) {
throw new Error(`Plugin '${pluginId}' not found`);
}
if (!entry.enabled) {
this.logger.warn(`Plugin '${pluginId}' is not enabled`);
return;
}
this.logger.info(`Disabling plugin: ${pluginId}`);
try {
const context = this.contextBuilder(pluginId);
if (entry.definition.onDisable) {
await entry.definition.onDisable(context);
}
entry.enabled = false;
this.logger.info(`Disabled plugin: ${pluginId}`);
} catch (error) {
this.logger.error(`Failed to disable plugin '${pluginId}'`, error as Error);
throw error;
}
}
/**
* Uninstall a plugin.
* Calls the onUninstall lifecycle hook.
*/
async uninstall(pluginId: string): Promise<void> {
const entry = this.plugins.get(pluginId);
if (!entry) {
throw new Error(`Plugin '${pluginId}' not found`);
}
if (entry.enabled) {
this.logger.info(`Plugin '${pluginId}' is enabled, disabling first`);
await this.disable(pluginId);
}
this.logger.info(`Uninstalling plugin: ${pluginId}`);
try {
const context = this.contextBuilder(pluginId);
if (entry.definition.onUninstall) {
await entry.definition.onUninstall(context);
}
entry.installed = false;
this.plugins.delete(pluginId);
this.logger.info(`Uninstalled plugin: ${pluginId}`);
} catch (error) {
this.logger.error(`Failed to uninstall plugin '${pluginId}'`, error as Error);
throw error;
}
}
/**
* Get a plugin entry by ID.
*/
getPlugin(pluginId: string): PluginEntry | undefined {
return this.plugins.get(pluginId);
}
/**
* Get all registered plugins.
*/
getAllPlugins(): Map<string, PluginEntry> {
return new Map(this.plugins);
}
/**
* Get enabled plugins only.
*/
getEnabledPlugins(): PluginEntry[] {
return Array.from(this.plugins.values()).filter(p => p.enabled);
}
/**
* Check if a plugin is registered.
*/
hasPlugin(pluginId: string): boolean {
return this.plugins.has(pluginId);
}
/**
* Check if a plugin is enabled.
*/
isEnabled(pluginId: string): boolean {
const entry = this.plugins.get(pluginId);
return entry ? entry.enabled : false;
}
/**
* Check if a plugin is installed.
*/
isInstalled(pluginId: string): boolean {
const entry = this.plugins.get(pluginId);
return entry ? entry.installed : false;
}
}