From 280231a0163b6d8078c4e379431b755a3642669e Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Wed, 12 Mar 2025 10:23:31 +0800 Subject: [PATCH] refactor: change the return value of uploadPublicKey as promise --- src/devstar-api.ts | 66 ++++++++++++++++++++++++---------------------- src/main.ts | 22 +++++++++++++--- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/devstar-api.ts b/src/devstar-api.ts index a62a525..5923ce8 100644 --- a/src/devstar-api.ts +++ b/src/devstar-api.ts @@ -21,40 +21,44 @@ export default class DevstarAPIHandler { } // 上传公钥 - public async uploadUserPublicKey() { - // 获取机器名称 - const machineName = os.hostname(); - // 组成公钥名称 - const timestamp = Date.now(); - const keyTitle = `${this.user.getUsernameFromLocal()}-${machineName}-${timestamp}` - const postData = { - "key": this.user.getUserPublicKey(), - "title": keyTitle - } + public async uploadUserPublicKey(): Promise { + return new Promise(async (resolve) => { + // 获取机器名称 + const machineName = os.hostname(); + // 组成公钥名称 + const timestamp = Date.now(); + const keyTitle = `${this.user.getUsernameFromLocal()}-${machineName}-${timestamp}` + const postData = { + "key": this.user.getUserPublicKey(), + "title": keyTitle + } - const uploadUrl = this.devstarDomain + `/api/v1/user/keys` + const uploadUrl = this.devstarDomain + `/api/v1/user/keys` + + // 上传公钥 + fetch(uploadUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'token ' + this.user.getUserTokenFromLocal() + }, + body: JSON.stringify(postData) + }) + .then(response => { + response.json().then(data => { + if (response.ok) { + console.log("Successfully upload new created public key.\n", data) + resolve("ok") + } else { + throw new Error(`Failed to upload new created public key!\nError: ${data.message}`) + } + }) + }) + .catch(error => { + console.error(error); + }); - // 上传公钥 - fetch(uploadUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'token ' + this.user.getUserTokenFromLocal() - }, - body: JSON.stringify(postData) - }) - .then(response => { - response.json().then(data => { - if (response.ok) { - console.log("Successfully upload new created public key.\n", data) - } else { - throw new Error(`Failed to upload new created public key!\nError: ${data.message}`) - } - }) }) - .catch(error => { - console.error(error); - }); } } diff --git a/src/main.ts b/src/main.ts index f71cbdf..8a23123 100644 --- a/src/main.ts +++ b/src/main.ts @@ -45,12 +45,20 @@ export class DevStarExtension { // 上传公钥 const devstarAPIHandler = new DevstarAPIHandler(this.user) await devstarAPIHandler.uploadUserPublicKey() + .then(async (res) => { + if (res === "ok") { + // 打开项目 + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) + } + }) }) + } else { + // 直接打开项目 + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) } - await this.remoteContainer.firstOpenProject(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 { // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器 @@ -67,9 +75,17 @@ export class DevStarExtension { // 上传公钥 const devstarAPIHandler = new DevstarAPIHandler(this.user) await devstarAPIHandler.uploadUserPublicKey() + .then(async (res) => { + if (res === "ok") { + // 打开项目 + await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) + } + }) }) + } else { + // 直接打开项目 + await this.remoteContainer.firstOpenProject(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); }