-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
settings.ts
87 lines (81 loc) · 3.63 KB
/
settings.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
import { EXT_NAME } from '@sqltools/core/constants';
import { getConnectionId } from '@sqltools/core/utils';
import WebviewProvider from '@sqltools/plugins/connection-manager/screens/provider';
import { commands, ExtensionContext, Uri, workspace } from 'vscode';
import path from 'path';
import { DatabaseDialect } from '@sqltools/core/interface';
const relativeToWorkspace = (file: string) => {
const fileUri = workspace.asRelativePath(Uri.file(file), true);
if (file === fileUri) return file;
if (fileUri.startsWith('/') || fileUri.startsWith('.//')) return file;
return `.${path.sep}${fileUri}`;
}
export default class SettingsWebview extends WebviewProvider {
protected id: string = 'Settings';
protected title: string = 'SQLTools Settings';
constructor(context: ExtensionContext) {
super(
context,
Uri.file(path.join(context.extensionPath, 'icons')).with({ scheme: 'vscode-resource' }),
Uri.file(path.join(context.extensionPath, 'ui')).with({ scheme: 'vscode-resource' })
);
this.setMessageCallback(({ action, payload }) => {
switch (action) {
case 'createConnection':
return this.createConnection(payload);
case 'updateConnection':
return this.updateConnection(payload);
case 'testConnection':
return this.testConnection(payload);
default:
break;
}
});
}
private updateConnection = async ({ connInfo, globalSetting, transformToRelative, editId }) => {
if (connInfo.dialect === DatabaseDialect.SQLite && transformToRelative) {
connInfo.database = relativeToWorkspace(connInfo.database);
}
return commands.executeCommand(`${EXT_NAME}.updateConnection`, editId, connInfo, globalSetting ? 'Global' : undefined)
.then(() => {
this.postMessage({ action: 'updateConnectionSuccess', payload: { globalSetting, connInfo: { ...connInfo, id: getConnectionId(connInfo) } } });
}, (payload = {}) => {
payload = {
message: (payload.message || payload || '').toString(),
}
this.postMessage({ action: 'updateConnectionError', payload });
});
}
private createConnection = async ({ connInfo, globalSetting, transformToRelative }) => {
if (connInfo.dialect === DatabaseDialect.SQLite && transformToRelative) {
connInfo.database = relativeToWorkspace(connInfo.database);
}
return commands.executeCommand(`${EXT_NAME}.addConnection`, connInfo, globalSetting ? 'Global' : undefined)
.then(() => {
this.postMessage({ action: 'createConnectionSuccess', payload: { globalSetting, connInfo: { ...connInfo, id: getConnectionId(connInfo) } } });
}, (payload = {}) => {
payload = {
message: (payload.message || payload || '').toString(),
}
this.postMessage({ action: 'createConnectionError', payload });
});
}
private testConnection = async ({ connInfo }) => {
if (connInfo.dialect === DatabaseDialect.SQLite) {
connInfo.database = relativeToWorkspace(connInfo.database);
}
return commands.executeCommand(`${EXT_NAME}.testConnection`, connInfo)
.then((res: any) => {
if (res && res.notification) {
const message = `You need to fix some issues in your machine first. Check the notifications on bottom-right before moving forward.`
return this.postMessage({ action: 'testConnectionWarning', payload: { message } });
}
this.postMessage({ action: 'testConnectionSuccess', payload: { connInfo } });
}, (payload = {}) => {
payload = {
message: (payload.message || payload || '').toString(),
}
this.postMessage({ action: 'testConnectionError', payload });
});
}
}