From 04e34d5129bdf74fa2bd9e848d2a49978c46b58c Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Sun, 2 Mar 2025 23:33:18 +0800 Subject: [PATCH] refactor: centralize the code that first opens the project into a single function --- src/main.ts | 32 +++++++------------------------- src/remote-container.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main.ts b/src/main.ts index 7d583dd..2dd5679 100644 --- a/src/main.ts +++ b/src/main.ts @@ -35,24 +35,12 @@ export class DevStarExtension { // 如果没有用户登录,则直接登录; this.user.setUserTokenToLocal(access_token) this.user.setUsernameToLocal(devstar_username) - // 使用登录已经登录过的容器连接方法 - await this.remoteContainer.firstConnect(container_host, container_username, container_port) - .then((_res) => { - if (_res == 'success') { - // only success then open folder - this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path); - } - }) - } else if (devstar_username === this.user.getUsernameFromLocal()){ + + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) + } else if (devstar_username === this.user.getUsernameFromLocal()) { // 如果同用户已经登录,则忽略; - // 使用登录已经登录过的容器连接方法 - await this.remoteContainer.firstConnect(container_host, container_username, container_port) - .then((_res) => { - if (_res == 'success') { - // only success then open folder - this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path); - } - }) + + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) } else { // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器 const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`, @@ -60,14 +48,8 @@ export class DevStarExtension { if (selection === 'Yes') { this.user.setUserTokenToLocal(access_token); this.user.setUsernameToLocal(devstar_username); - // 使用登录已经登录过的容器连接方法 - await this.remoteContainer.firstConnect(container_host, container_username, container_port) - .then((_res) => { - if (_res == 'success') { - // only success then open folder - this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path); - } - }) + + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) } else if (selection === 'No') { await openProjectWithoutLogging(container_host, container_port, container_username, project_path); } diff --git a/src/remote-container.ts b/src/remote-container.ts index f21552f..20b85ad 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -15,6 +15,16 @@ export default class RemoteContainer { this.user = user } + async firstOpenProject(host: string, port: number, username: string, path: string) { + this.firstConnect(host, username, port) + .then((res) => { + if (res == 'success') { + // only success then open folder + this.openRemoteFolder(host, port, username, path); + } + }) + } + async firstConnect(host: string, username: string, port: number): Promise; // connect with key // deprecated async firstConnect(host: string, username: string, port: number, password: string): Promise;