-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathFindModule.ts
104 lines (83 loc) · 3.68 KB
/
FindModule.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// TODO: PSES does not currently support findModule...so this whole thing is broken!
import vscode = require("vscode");
import { RequestType } from "vscode-languageclient";
import QuickPickItem = vscode.QuickPickItem;
import { LanguageClientConsumer } from "../languageClientConsumer";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IFindModuleRequestArguments {
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IModule {
name: string,
description: string
}
export const FindModuleRequestType =
new RequestType<IFindModuleRequestArguments, IModule[], void>("powerShell/findModule");
export const InstallModuleRequestType =
new RequestType<string, void, void>("powerShell/installModule");
export class FindModuleFeature extends LanguageClientConsumer {
private command: vscode.Disposable;
private cancelFindToken?: vscode.CancellationTokenSource;
constructor() {
super();
this.command = vscode.commands.registerCommand("PowerShell.PowerShellFindModule", async () => {
// It takes a while to get the list of PowerShell modules, display some UI to let user know
this.cancelFindToken = new vscode.CancellationTokenSource();
const response = await vscode.window.showQuickPick(
["Cancel"],
{ placeHolder: "Please wait, retrieving list of PowerShell modules. This can take some time..." },
this.cancelFindToken.token);
if (response === "Cancel") {
this.clearCancelFindToken();
}
// Cancel the loading prompt after 60 seconds
setTimeout(() => {
if (this.cancelFindToken) {
this.clearCancelFindToken();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showErrorMessage(
"The online source for PowerShell modules is not responding. " +
"Cancelling Find/Install PowerShell command.");
}
}, 60000);
const module = await this.pickPowerShellModule();
if (module !== undefined) {
await this.languageClient?.sendRequest(InstallModuleRequestType, module);
}
});
}
public dispose() {
this.command.dispose();
}
private async pickPowerShellModule(): Promise<string | undefined> {
const modules = await this.languageClient?.sendRequest(FindModuleRequestType, undefined);
const items: QuickPickItem[] = [];
// We've got the modules info, let's cancel the timeout unless it's already been cancelled
if (this.cancelFindToken) {
this.clearCancelFindToken();
} else {
// Already timed out, would be weird to display modules after we said it timed out.
return Promise.resolve("");
}
for (const module of modules ?? []) {
items.push({ label: module.name, description: module.description });
}
if (items.length === 0) {
return Promise.reject("No PowerShell modules were found.");
}
const options: vscode.QuickPickOptions = {
placeHolder: "Select a PowerShell module to install",
matchOnDescription: true,
matchOnDetail: true,
};
return vscode.window.showQuickPick(items, options).then((item) => {
return item ? item.label : "";
});
}
private clearCancelFindToken() {
this.cancelFindToken?.dispose();
this.cancelFindToken = undefined;
}
}