-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathhost-commands.ts
More file actions
95 lines (84 loc) · 2.79 KB
/
Copy pathhost-commands.ts
File metadata and controls
95 lines (84 loc) · 2.79 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
import type {
DevToolsCommandHandle,
DevToolsCommandsHost as DevToolsCommandsHostType,
DevToolsServerCommandEntry,
DevToolsServerCommandInput,
} from '../types/commands'
import type { KitNodeContext } from './context'
import { createEventEmitter } from 'devframe/utils/events'
import { diagnostics } from './diagnostics'
export class DevToolsCommandsHost implements DevToolsCommandsHostType {
public readonly commands: DevToolsCommandsHostType['commands'] = new Map()
public readonly events: DevToolsCommandsHostType['events'] = createEventEmitter()
constructor(
public readonly context: KitNodeContext,
) {}
register(command: DevToolsServerCommandInput): DevToolsCommandHandle {
if (this.commands.has(command.id)) {
throw diagnostics.DTK0055({ id: command.id })
}
this.commands.set(command.id, command)
this.events.emit('command:registered', this.toSerializable(command))
return {
id: command.id,
update: (patch: Partial<Omit<DevToolsServerCommandInput, 'id'>>) => {
if ('id' in patch) {
throw diagnostics.DTK0056()
}
const existing = this.commands.get(command.id)
if (!existing) {
throw diagnostics.DTK0057({ id: command.id })
}
Object.assign(existing, patch)
this.events.emit('command:registered', this.toSerializable(existing))
},
unregister: () => this.unregister(command.id),
}
}
unregister(id: string): boolean {
const deleted = this.commands.delete(id)
if (deleted) {
this.events.emit('command:unregistered', id)
}
return deleted
}
async execute(id: string, ...args: any[]): Promise<unknown> {
const found = this.findCommand(id)
if (!found) {
throw diagnostics.DTK0057({ id })
}
if (!found.handler) {
throw new Error(`Command "${id}" has no handler (group-only command)`)
}
return found.handler(...args)
}
list(): DevToolsServerCommandEntry[] {
return Array.from(this.commands.values()).map(cmd => this.toSerializable(cmd))
}
private findCommand(id: string): DevToolsServerCommandInput | undefined {
// Check top-level
const topLevel = this.commands.get(id)
if (topLevel)
return topLevel
// Search children
for (const cmd of this.commands.values()) {
if (cmd.children) {
const child = cmd.children.find((c: DevToolsServerCommandInput) => c.id === id)
if (child)
return child
}
}
return undefined
}
private toSerializable(cmd: DevToolsServerCommandInput): DevToolsServerCommandEntry {
const { handler: _, children, ...rest } = cmd
return {
...rest,
source: 'server',
...(children
? { children: children.map((c: DevToolsServerCommandInput) => this.toSerializable(c)) }
: {}
),
}
}
}