forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-registry.ts
163 lines (144 loc) · 5.06 KB
/
command-registry.ts
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
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { Disposable } from './types/disposable.js';
import type { Telemetry } from './telemetry/telemetry.js';
import { type CommandInfo } from './api/command-info.js';
import { type ApiSenderType } from './api.js';
export interface RawCommand {
command?: string;
title?: string;
category?: string;
description?: string;
icon?: string | { light: string; dark: string };
keybinding?: string;
}
export interface CommandHandler {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
thisArg: any;
}
export class CommandRegistry {
constructor(
private apiSender: ApiSenderType,
private readonly telemetry: Telemetry,
) {
// init empty array
this.commandPaletteCommands.set(CommandRegistry.GLOBAL, []);
}
private commands = new Map<string, CommandHandler>();
static readonly GLOBAL = 'GLOBAL';
private commandPaletteCommands = new Map<string, RawCommand[]>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable {
if (this.commands.has(command)) {
throw new Error(`command '${command}' already exists`);
}
// keep command
this.commands.set(command, {
callback,
thisArg,
});
return Disposable.create(() => {
this.commands.delete(command);
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async executeCommand<T = unknown>(commandId: string, ...args: any[]): Promise<T> {
const telemetryOptions = { commandId: commandId };
try {
// command is on node world, just execute it
if (this.commands.has(commandId)) {
const command = this.commands.get(commandId);
if (command) {
return command.callback.apply(command.thisArg, args);
}
}
// should try to execute on client side
throw new Error('Unknown command: ' + commandId);
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(telemetryOptions as any).error = err;
throw err;
} finally {
this.telemetry.track('executeCommand', telemetryOptions);
}
}
hasCommand(commandId: string): boolean {
return this.commands.has(commandId);
}
getCommandPaletteCommands(): CommandInfo[] {
// we return the commands from commandPaletteCommands map
const commandInfos: CommandInfo[] = [];
this.commandPaletteCommands.forEach(commands => {
commands.forEach(command => {
if (!command.command) {
return;
}
commandInfos.push({
id: command.command,
title: command.category ? `${command.category}: ${command.title}` : command.title,
});
});
});
// need to sort based on the title
commandInfos.sort((a, b) => {
if (a.title && b.title) {
return a.title.localeCompare(b.title);
}
return 0;
});
return commandInfos;
}
registerCommandPalette(...extensionCommands: RawCommand[]): Disposable {
const disposables: Disposable[] = [];
extensionCommands.forEach(command => {
this.commandPaletteCommands.get(CommandRegistry.GLOBAL)?.push(command);
disposables.push(
Disposable.create(() => {
// grab the global commands
const globalCommands = this.commandPaletteCommands.get(CommandRegistry.GLOBAL);
if (globalCommands) {
// search matching command
const index = globalCommands.findIndex(cmd => cmd.command === command.command);
if (index > -1) {
// remove it
globalCommands.splice(index, 1);
}
}
}),
);
});
this.apiSender.send('commands-added');
disposables.push(
Disposable.create(() => {
this.apiSender.send('commands-removed');
}),
);
return Disposable.from(...disposables);
}
registerCommandsFromExtension(extensionId: string, extensionCommands: RawCommand[]): Disposable {
// add the commands
this.commandPaletteCommands.set(extensionId, extensionCommands);
this.apiSender.send('commands-added');
return Disposable.create(() => {
this.commandPaletteCommands.delete(extensionId);
this.apiSender.send('commands-removed');
});
}
}