refactor: change the return value of uploadPublicKey as promise<string>

This commit is contained in:
Levi Yan
2025-03-12 10:23:31 +08:00
parent cf1efabc77
commit 280231a016
2 changed files with 54 additions and 34 deletions

View File

@@ -21,7 +21,8 @@ export default class DevstarAPIHandler {
} }
// 上传公钥 // 上传公钥
public async uploadUserPublicKey() { public async uploadUserPublicKey(): Promise<string> {
return new Promise(async (resolve) => {
// 获取机器名称 // 获取机器名称
const machineName = os.hostname(); const machineName = os.hostname();
// 组成公钥名称 // 组成公钥名称
@@ -47,6 +48,7 @@ export default class DevstarAPIHandler {
response.json().then(data => { response.json().then(data => {
if (response.ok) { if (response.ok) {
console.log("Successfully upload new created public key.\n", data) console.log("Successfully upload new created public key.\n", data)
resolve("ok")
} else { } else {
throw new Error(`Failed to upload new created public key!\nError: ${data.message}`) throw new Error(`Failed to upload new created public key!\nError: ${data.message}`)
} }
@@ -55,6 +57,8 @@ export default class DevstarAPIHandler {
.catch(error => { .catch(error => {
console.error(error); console.error(error);
}); });
})
} }
} }

View File

@@ -45,12 +45,20 @@ export class DevStarExtension {
// 上传公钥 // 上传公钥
const devstarAPIHandler = new DevstarAPIHandler(this.user) const devstarAPIHandler = new DevstarAPIHandler(this.user)
await devstarAPIHandler.uploadUserPublicKey() await devstarAPIHandler.uploadUserPublicKey()
}) .then(async (res) => {
} if (res === "ok") {
// 打开项目
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 {
// 直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
}
} else if (devstar_username === this.user.getUsernameFromLocal()) { } else if (devstar_username === this.user.getUsernameFromLocal()) {
// 如果同用户已经登录,则忽略; // 如果同用户已经登录,则忽略;
// 直接打开项目
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 { } else {
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器 // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
@@ -67,9 +75,17 @@ export class DevStarExtension {
// 上传公钥 // 上传公钥
const devstarAPIHandler = new DevstarAPIHandler(this.user) const devstarAPIHandler = new DevstarAPIHandler(this.user)
await devstarAPIHandler.uploadUserPublicKey() await devstarAPIHandler.uploadUserPublicKey()
}) .then(async (res) => {
} if (res === "ok") {
// 打开项目
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 {
// 直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
}
} else if (selection === 'No') { } else if (selection === 'No') {
await openProjectWithoutLogging(container_host, container_port, container_username, project_path); await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
} }