From 69eed27589cacbefee634b184f7914c59da371e4 Mon Sep 17 00:00:00 2001 From: Kelvin Schoofs Date: Tue, 19 Jan 2021 21:31:52 +0100 Subject: [PATCH] Improve environment variable handling in connect.ts --- src/connect.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/connect.ts b/src/connect.ts index 7f6ad26..49f272f 100644 --- a/src/connect.ts +++ b/src/connect.ts @@ -1,5 +1,6 @@ import { readFile } from 'fs'; import { Socket } from 'net'; +import { userInfo } from 'os'; import { Client, ClientChannel, ConnectConfig, SFTPWrapper as SFTPWrapperReal } from 'ssh2'; import { SFTPStream } from 'ssh2-streams'; import * as vscode from 'vscode'; @@ -17,9 +18,13 @@ const DEFAULT_CONFIG: ConnectConfig = { keepaliveInterval: 30e3, }; -function replaceVariables(string?: string) { +function replaceVariables(string: string): string; +function replaceVariables(string?: string): string | undefined; +function replaceVariables(string?: string): string | undefined { if (typeof string !== 'string') return string; - return string.replace(/\$\w+/g, key => process.env[key.substr(1)] || ''); + return string + .replace(/\${(\w+)}/g, (_, key) => process.env[key] || '') + .replace(/\$(\w+)/g, (_, key) => process.env[key] || ''); } const PROMPT_FIELDS: Partial p.startsWith('~') ? `${userInfo().homedir}${p.substr(1)}` : p); + paths = paths.map(replaceVariables); const { buildHolder, fillFileSystemConfig } = await import('./ssh-config'); const holder = await buildHolder(paths); await fillFileSystemConfig(config, holder);