-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugins.ts
More file actions
81 lines (70 loc) · 2.16 KB
/
Copy pathplugins.ts
File metadata and controls
81 lines (70 loc) · 2.16 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
import ms from "ms";
import type { ExtensionContext, LogOutputChannel, StatusBarItem } from "vscode";
import type { ContainerStatusTracker } from "./utils/container-status.ts";
import type { LocalStackStatusTracker } from "./utils/localstack-status.ts";
import type { SetupStatusTracker } from "./utils/setup-status.ts";
import type { Telemetry } from "./utils/telemetry.ts";
import type { TimeTracker } from "./utils/time-tracker.ts";
export type Deactivate = () => Promise<void> | void;
export interface PluginOptions {
context: ExtensionContext;
outputChannel: LogOutputChannel;
statusBarItem: StatusBarItem;
containerStatusTracker: ContainerStatusTracker;
localStackStatusTracker: LocalStackStatusTracker;
setupStatusTracker: SetupStatusTracker;
telemetry: Telemetry;
timeTracker: TimeTracker;
}
export interface Plugin {
deactivate: Deactivate;
}
export type PluginDefinition = {
name: string;
factory: (options: PluginOptions) => Promise<Plugin>;
};
export const createPlugin = (
name: string,
// biome-ignore lint/suspicious/noConfusingVoidType: required
handler: (options: PluginOptions) => Promise<Deactivate | void> | void,
): PluginDefinition => {
return {
name,
async factory(options: PluginOptions): Promise<Plugin> {
const deactivate = (await handler(options)) ?? (() => {});
return {
deactivate,
};
},
};
};
export class PluginManager {
private plugins: PluginDefinition[];
private deactivatables: Plugin[];
constructor(plugins: PluginDefinition[]) {
this.plugins = plugins;
this.deactivatables = [];
}
async activate(options: PluginOptions) {
for (const activate of this.plugins) {
const startPlugin = Date.now();
options.outputChannel.trace(
`[extension.plugins]: Activating plugin "${activate.name}"...`,
);
const deactivatable = await activate.factory(options);
this.deactivatables.push(deactivatable);
const endPlugin = Date.now();
options.outputChannel.trace(
`[extension.plugins]: Activated plugin "${activate.name}" in ${ms(
endPlugin - startPlugin,
{ long: true },
)}`,
);
}
}
async deactivate() {
for (const plugin of this.deactivatables) {
await plugin.deactivate();
}
}
}