-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule-manager.ts
More file actions
145 lines (120 loc) · 3.66 KB
/
module-manager.ts
File metadata and controls
145 lines (120 loc) · 3.66 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
import { CLIProviderImpl } from './provider';
import type {
CLIModule,
CLIProvider,
CommandContext,
CommandDefinition,
CommandResult,
CustomWorkflow,
} from './types';
export class ModuleManager {
private modules: Map<string, CLIModule> = new Map();
private provider: CLIProvider;
private commands: Map<string, CommandDefinition> = new Map();
private workflows: Map<string, CustomWorkflow> = new Map();
constructor() {
this.provider = new CLIProviderImpl();
}
registerModule(module: CLIModule): void {
if (this.modules.has(module.name)) {
throw new Error(`Module '${module.name}' is already registered`);
}
this.modules.set(module.name, module);
if (module.commands) {
for (const command of module.commands) {
this.registerCommand(command);
}
}
if (module.workflows) {
for (const workflow of module.workflows) {
this.registerWorkflow(workflow);
}
}
if (module.init) {
module.init(this.provider);
}
}
unregisterModule(moduleName: string): void {
const module = this.modules.get(moduleName);
if (!module) {
throw new Error(`Module '${moduleName}' is not registered`);
}
if (module.commands) {
for (const command of module.commands) {
this.commands.delete(command.name);
}
}
if (module.workflows) {
for (const workflow of module.workflows) {
this.workflows.delete(workflow.name);
}
}
if (module.cleanup) {
module.cleanup();
}
this.modules.delete(moduleName);
console.log(`Module '${moduleName}' unregistered successfully`);
}
registerCommand(command: CommandDefinition): void {
if (this.commands.has(command.name)) {
throw new Error(`Command '${command.name}' is already registered`);
}
this.commands.set(command.name, command);
}
registerWorkflow(workflow: CustomWorkflow): void {
if (this.workflows.has(workflow.name)) {
throw new Error(`Workflow '${workflow.name}' is already registered`);
}
this.workflows.set(workflow.name, workflow);
this.provider.registerWorkflow(workflow);
}
async executeCommand(
commandName: string,
context: CommandContext,
): Promise<CommandResult> {
const command = this.commands.get(commandName);
if (!command) {
throw new Error(`Command '${commandName}' not found`);
}
return await command.handler(context);
}
async executeWorkflow(
workflowName: string,
context: CommandContext,
): Promise<CommandResult> {
return await this.provider.executeWorkflow(workflowName, context);
}
getProvider(): CLIProvider {
return this.provider;
}
listCommands(): CommandDefinition[] {
return Array.from(this.commands.values());
}
listWorkflows(): CustomWorkflow[] {
return Array.from(this.workflows.values());
}
listModules(): CLIModule[] {
return Array.from(this.modules.values());
}
listAll(): void {
console.log('\n=== Registered Commands ===');
for (const command of this.commands.values()) {
console.log(
` ${command.name}: ${command.description || 'No description'}`,
);
}
console.log('\n=== Registered Workflows ===');
for (const workflow of this.workflows.values()) {
console.log(
` ${workflow.name}: ${workflow.description || 'No description'}`,
);
}
console.log('\n=== Registered Modules ===');
for (const module of this.modules.values()) {
console.log(
` ${module.name} (v${module.version}): ${module.commands?.length || 0} commands, ${module.workflows?.length || 0} workflows`,
);
}
}
}
export const moduleManager = new ModuleManager();