|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { getServerNames } from "./getServerNames"; |
| 3 | + |
| 4 | +export async function addServer(scope?: vscode.ConfigurationScope): Promise<string | undefined> { |
| 5 | + const serverNames = getServerNames(scope); |
| 6 | + const spec = { webServer: { scheme: "", host: "", port: 0 } }; |
| 7 | + return await vscode.window |
| 8 | + .showInputBox({ |
| 9 | + placeHolder: "Name of new server definition", |
| 10 | + validateInput: (value) => { |
| 11 | + if (value === "") { |
| 12 | + return "Required"; |
| 13 | + } |
| 14 | + if (serverNames.filter((server) => server.name === value).length) { |
| 15 | + return "Name already exists"; |
| 16 | + } |
| 17 | + if (!value.match(/^[a-z0-9-._~]+$/)) { |
| 18 | + return "Can only contain a-z, 0-9 and punctuation -._~"; |
| 19 | + } |
| 20 | + return null; |
| 21 | + }, |
| 22 | + }) |
| 23 | + .then( |
| 24 | + async (name): Promise<string | undefined> => { |
| 25 | + if (name) { |
| 26 | + const host = await vscode.window.showInputBox({ |
| 27 | + placeHolder: "Hostname or IP address of web server", |
| 28 | + validateInput: (value) => { |
| 29 | + return value.length ? undefined : "Required"; |
| 30 | + }, |
| 31 | + }); |
| 32 | + if (host) { |
| 33 | + spec.webServer.host = host; |
| 34 | + const portString = await vscode.window.showInputBox({ |
| 35 | + placeHolder: "Port of web server", |
| 36 | + validateInput: (value) => { |
| 37 | + const port = +value; |
| 38 | + return value.match(/\d+/) && |
| 39 | + port.toString() === value && |
| 40 | + port > 0 && |
| 41 | + port < 65536 |
| 42 | + ? undefined |
| 43 | + : "Required, 1-65535"; |
| 44 | + }, |
| 45 | + }); |
| 46 | + if (portString) { |
| 47 | + spec.webServer.port = +portString; |
| 48 | + const scheme = await vscode.window.showQuickPick( |
| 49 | + ["http", "https"], |
| 50 | + { placeHolder: "Confirm connection type, then definition will be stored in your User Settings. 'Escape' to cancel.", } |
| 51 | + ); |
| 52 | + if (scheme) { |
| 53 | + spec.webServer.scheme = scheme; |
| 54 | + try { |
| 55 | + const config = vscode.workspace.getConfiguration( |
| 56 | + "intersystems", |
| 57 | + scope |
| 58 | + ); |
| 59 | + // For simplicity we always add to the user-level (aka Global) settings |
| 60 | + const servers: any = |
| 61 | + config.inspect("servers")?.globalValue || {}; |
| 62 | + servers[name] = spec; |
| 63 | + await config.update("servers", servers, true); |
| 64 | + return name; |
| 65 | + } catch (error) { |
| 66 | + vscode.window.showErrorMessage( |
| 67 | + "Failed to store server definition" |
| 68 | + ); |
| 69 | + return undefined; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + ); |
| 77 | +} |
0 commit comments