-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
extension.ts
88 lines (84 loc) · 4.46 KB
/
extension.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
import { window as Win, workspace, ConfigurationTarget, window, ProgressLocation, commands } from 'vscode';
import { InstallDepRequest, MissingModuleNotification, ElectronNotSupportedNotification, DependeciesAreBeingInstalledNotification } from '@sqltools/plugins/dependency-manager/contracts';
import SQLTools from '@sqltools/core/plugin-api';
import { openExternal } from '@sqltools/core/utils/vscode';
import { EXT_NAME, DOCS_ROOT_URL } from '@sqltools/core/constants';
import { getConnectionId } from '@sqltools/core/utils';
export default class DependencyManager implements SQLTools.ExtensionPlugin {
public client: SQLTools.LanguageClientInterface;
private extension: SQLTools.ExtensionInterface;
register(extension: SQLTools.ExtensionInterface) {
this.extension = extension;
this.client = extension.client;
this.client.onNotification(MissingModuleNotification, param => this.requestToInstall(param));
this.client.onNotification(DependeciesAreBeingInstalledNotification, param => this.jobRunning(param));
this.client.onNotification(ElectronNotSupportedNotification, this.electronNotSupported);
}
private electronNotSupported = async () => {
const r = await Win.showInformationMessage(
'Electron is not supported. You should enable \'sqltools.useNodeRuntime\' and have NodeJS installed to continue.',
'Enable now',
);
if (!r) return;
await workspace.getConfiguration(EXT_NAME.toLowerCase()).update('useNodeRuntime', true, ConfigurationTarget.Global);
try { await workspace.getConfiguration(EXT_NAME.toLowerCase()).update('useNodeRuntime', true, ConfigurationTarget.Workspace) } catch(e) {}
try { await workspace.getConfiguration(EXT_NAME.toLowerCase()).update('useNodeRuntime', true, ConfigurationTarget.WorkspaceFolder) } catch(e) {}
const res = await Win.showInformationMessage(
'\'sqltools.useNodeRuntime\' enabled. You must reload VSCode to take effect.', 'Reload now');
if (!res) return;
commands.executeCommand('workbench.action.reloadWindow');
}
private installingDialects: string[] = [];
private requestToInstall = async ({ moduleName, moduleVersion, conn, action = 'install' }) => {
conn = conn || {};
const installNow = 'Install now';
const readMore = 'Read more';
const options = [readMore, installNow];
try {
const r = action === 'upgrade' ? installNow : await Win.showInformationMessage(
`You need to ${action} "${moduleName}@${moduleVersion}" to connect to ${conn.name}.`,
...options,
);
switch (r) {
case installNow:
this.installingDialects.push(conn.dialect);
await window.withProgress({
location: ProgressLocation.Notification,
title: `SQLTools is ${action === 'upgrade' ? 'upgrading deps' : 'installing'}`,
cancellable: false,
}, async (progress) => {
progress.report({ message: `${this.installingDialects.join(', ')} dependencies` });
const interval = setInterval(() => {
progress.report({ message: `${this.installingDialects.join(', ')} dependencies` });
}, 1000);
const result = await this.client.sendRequest(InstallDepRequest, { dialect: conn.dialect }).then(result => result, err => {
clearInterval(interval);
return Promise.reject(err);
});
clearInterval(interval);
return result;
});
this.installingDialects = this.installingDialects.filter(v => v !== conn.dialect);
const opt = conn.name ? [`Connect to ${conn.name}`] : [];
const rr = await Win.showInformationMessage(
`"${moduleName}@${moduleVersion}" installed!\n
Go ahead and connect!`,
...opt
);
if (rr === opt[0]) {
await commands.executeCommand(`${EXT_NAME}.showOutputChannel`, getConnectionId(conn));
}
break;
case readMore:
openExternal(`${DOCS_ROOT_URL}/connections/${conn.dialect ? conn.dialect.toLowerCase() : ''}`);
break;
}
} catch (error) {
this.installingDialects = this.installingDialects.filter(v => v !== conn.dialect);
this.extension.errorHandler(`Failed to install dependencies for ${conn.dialect}:`, error);
}
}
private jobRunning = async ({ moduleName, moduleVersion, conn }) => {
return Win.showInformationMessage(`We are installing "${moduleName}@${moduleVersion}" to connect to ${conn.name}. Please wait till it finishes.`);
}
}