Skip to content

Commit 5f507c3

Browse files
committed
password prompt, live connection status
1 parent 92aad8b commit 5f507c3

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/api/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as request from "request-promise";
44
import * as url from "url";
55
import * as vscode from "vscode";
66
import * as Cache from "vscode-cache";
7-
import { config, extensionContext, FILESYSTEM_SCHEMA, workspaceState, panel } from "../extension";
7+
import { config, extensionContext, FILESYSTEM_SCHEMA, workspaceState, panel, checkConnection } from "../extension";
88
import { currentWorkspaceFolder, outputConsole, outputChannel } from "../utils";
99

1010
const DEFAULT_API_VERSION = 1;
@@ -28,6 +28,10 @@ export class AtelierAPI {
2828
return workspaceState.get(this.workspaceFolder + ":port", this.config.port);
2929
}
3030

31+
private get password(): string {
32+
return workspaceState.get(this.workspaceFolder + ":password", this.config.password);
33+
}
34+
3135
private get iris(): boolean {
3236
return workspaceState.get(this.workspaceFolder + ":iris", false);
3337
}
@@ -70,8 +74,9 @@ export class AtelierAPI {
7074
}
7175

7276
public xdebugUrl(): string {
73-
const { host, username, password, https } = this.config;
77+
const { host, username, https } = this.config;
7478
const port = this.port;
79+
const password = this.password;
7580
const proto = https ? "wss" : "ws";
7681
const auth = this.iris
7782
? `IRISUsername=${username}&IRISPassword=${password}`
@@ -144,8 +149,9 @@ export class AtelierAPI {
144149
}
145150
headers["Cache-Control"] = "no-cache";
146151

147-
const { host, username, password, https } = this.config;
152+
const { host, username, https } = this.config;
148153
const port = this.port;
154+
const password = this.password;
149155
const proto = this.config.https ? "https" : "http";
150156
const http: any = this.config.https ? httpsModule : httpModule;
151157
const agent = new http.Agent({
@@ -209,6 +215,9 @@ export class AtelierAPI {
209215
}
210216
})
211217
.catch(error => {
218+
if (error.error && error.error.code === "ECONNREFUSED") {
219+
setTimeout(checkConnection, 1000);
220+
}
212221
console.error(error);
213222
throw error;
214223
})

src/commands/serverActions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { config, workspaceState, checkConnection } from "../extension";
33
import { currentWorkspaceFolder, terminalWithDocker } from "../utils";
44

55
export async function serverActions(): Promise<void> {
6-
const { active, host, ns, https, port: defaultPort, username, password, links } = config("conn");
6+
const { active, host, ns, https, port: defaultPort, username, password: defaultPassword, links } = config("conn");
77
const workspaceFolder = currentWorkspaceFolder();
88
const port = workspaceState.get(workspaceFolder + ":port", defaultPort);
9+
const password = workspaceState.get(workspaceFolder + ":password", defaultPassword);
910
const nsEncoded = encodeURIComponent(ns);
1011
const connInfo = `${host}:${port}[${nsEncoded}]`;
1112
const serverUrl = `${https ? "https" : "http"}://${host}:${port}`;
@@ -83,7 +84,7 @@ export async function serverActions(): Promise<void> {
8384
break;
8485
}
8586
case "refreshConnection": {
86-
checkConnection();
87+
checkConnection(true);
8788
break;
8889
}
8990
case "openDockerTerminal": {

src/extension.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const OBJECTSCRIPTXML_FILE_SCHEMA = "objectscriptxml";
99
export const FILESYSTEM_SCHEMA = "isfs";
1010
export const schemas = [OBJECTSCRIPT_FILE_SCHEMA, OBJECTSCRIPTXML_FILE_SCHEMA, FILESYSTEM_SCHEMA];
1111

12+
import WebSocket = require("ws");
1213
import {
1314
importAndCompile,
1415
importFolder as importFileOrFolder,
@@ -101,7 +102,7 @@ let reporter: TelemetryReporter;
101102

102103
export const checkConnection = (clearCookies = false): void => {
103104
const conn = config("conn");
104-
const connInfo = `${conn.host}:${conn.port}[${conn.ns}]`;
105+
let connInfo = `${conn.host}:${conn.port}[${conn.ns}]`;
105106
panel.text = connInfo;
106107
panel.tooltip = "";
107108
vscode.commands.executeCommand("setContext", "vscode-objectscript.connectActive", conn.active);
@@ -117,6 +118,7 @@ export const checkConnection = (clearCookies = false): void => {
117118
if (dockerPort !== conn.port) {
118119
workspaceState.update(currentWorkspaceFolder() + ":port", dockerPort);
119120
}
121+
connInfo = `${conn.host}:${dockerPort}[${conn.ns}]`;
120122
}
121123

122124
const api = new AtelierAPI(currentWorkspaceFolder());
@@ -131,10 +133,36 @@ export const checkConnection = (clearCookies = false): void => {
131133
serverVersion: info.result.content.version,
132134
healthshare: hasHS ? "yes" : "no",
133135
});
136+
/// Use xdebug's websocket, to catch when server disconnected
137+
const socket = new WebSocket(api.xdebugUrl());
138+
socket.onopen = () => {
139+
panel.text = `${connInfo} - Connected`;
140+
};
141+
socket.onclose = event => {
142+
panel.text = `${connInfo} - Disconnected`;
143+
};
134144
})
135145
.catch(error => {
136146
let message = error.message;
137147
if (error instanceof StatusCodeError && error.statusCode === 401) {
148+
setTimeout(
149+
() =>
150+
vscode.window
151+
.showInputBox({
152+
password: true,
153+
placeHolder: "Not Authorized, please enter password to connect",
154+
ignoreFocusOut: true,
155+
})
156+
.then(password => {
157+
if (password) {
158+
workspaceState.update(currentWorkspaceFolder() + ":password", password);
159+
checkConnection();
160+
} else {
161+
vscode.workspace.getConfiguration().update("objectscript.conn.active", false);
162+
}
163+
}),
164+
1000
165+
);
138166
message = "Not Authorized";
139167
outputChannel.appendLine(
140168
`Authorization error: please check your username/password in the settings,
@@ -175,7 +203,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
175203
panel.command = "vscode-objectscript.serverActions";
176204
panel.show();
177205

178-
checkConnection();
206+
checkConnection(true);
179207
vscode.workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
180208
if (affectsConfiguration("objectscript.conn")) {
181209
checkConnection(true);
@@ -184,13 +212,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
184212

185213
workspace.onDidSaveTextDocument(file => {
186214
if (schemas.includes(file.uri.scheme) || languages.includes(file.languageId)) {
187-
vscode.commands.executeCommand("vscode-objectscript.compile");
215+
return vscode.commands.executeCommand("vscode-objectscript.compile");
188216
}
189217
});
190218

191219
vscode.window.onDidChangeActiveTextEditor((textEditor: vscode.TextEditor) => {
192220
if (config("autoPreviewXML")) {
193-
xml2doc(context, textEditor);
221+
return xml2doc(context, textEditor);
194222
}
195223
});
196224

@@ -379,4 +407,5 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
379407
export function deactivate() {
380408
// This will ensure all pending events get flushed
381409
reporter.dispose();
410+
terminal.dispose();
382411
}

0 commit comments

Comments
 (0)