From 09485fbd0a587f458c2d09c1163d0842d2d75912 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Fri, 9 Feb 2018 13:50:57 +0800 Subject: [PATCH] using promise.all() when upload files (#108) * using promise.all() when upload files * add promise all --- src/cloudShell.ts | 40 +++++++++++++++++++++++----------------- src/extension.ts | 2 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/cloudShell.ts b/src/cloudShell.ts index 6c5144e..8761a2e 100644 --- a/src/cloudShell.ts +++ b/src/cloudShell.ts @@ -13,34 +13,28 @@ import { delay } from "./cloudConsoleLauncher"; import { aciConfig, Constants, exportContainerCmd, exportTestScript } from "./constants"; import { azFilePush, escapeFile, TerminalType, TestOption, TFTerminal } from "./shared"; import { terraformChannel } from "./terraformChannel"; -import { DialogOption } from "./utils/uiUtils"; +import { DialogOption, DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; const tempFile = path.join(os.tmpdir(), "cloudshell" + vscode.env.sessionId + ".log"); export class CloudShell extends BaseShell { - public async pushFiles(files: vscode.Uri[], syncAllFiles: boolean): Promise { + public async pushFiles(files: vscode.Uri[]): Promise { terraformChannel.appendLine("Attempting to upload files to CloudShell..."); if (await this.connectedToCloudShell()) { + const promises: Array> = []; for (const file of files.map((a) => a.fsPath)) { - try { - if (await fsExtra.pathExists(file)) { - terraformChannel.appendLine(`Uploading file ${file} to cloud shell`); - await azFilePush( - vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file)).name, - this.tfTerminal.storageAccountName, - this.tfTerminal.storageAccountKey, - this.tfTerminal.fileShareName, file); - } - } catch (err) { - terraformChannel.appendLine(err); - } + promises.push(this.pushFilePromise(file)); } - if (syncAllFiles) { - vscode.window.showInformationMessage( - "Synced all matched files in the current workspace to CloudShell"); + try { + await Promise.all(promises); + vscode.window.showInformationMessage("Synced all matched files in the current workspace to CloudShell"); + } catch (error) { + terraformChannel.appendLine(error); + await promptForOpenOutputChannel("Failed to push files to the cloud. Please open the output channel for more details.", DialogType.error); + } } } @@ -154,7 +148,19 @@ export class CloudShell extends BaseShell { console.log("Open CloudShell cancelled by user."); return false; + } + private async pushFilePromise(file: string): Promise { + if (await fsExtra.pathExists(file)) { + terraformChannel.appendLine(`Uploading file ${file} to cloud shell`); + await azFilePush( + vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file)).name, + this.tfTerminal.storageAccountName, + this.tfTerminal.storageAccountKey, + this.tfTerminal.fileShareName, + file, + ); + } } private async resolveContainerCmd(TestType: string): Promise { diff --git a/src/extension.ts b/src/extension.ts index ecdd6d0..d35ea44 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -78,7 +78,7 @@ export function activate(ctx: vscode.ExtensionContext) { // Create a function that will sync the files to Cloudshell if (terminalSetToCloudshell()) { vscode.workspace.findFiles(filesGlobSetting()).then((tfFiles) => { - cs.pushFiles(tfFiles, true); + cs.pushFiles(tfFiles); }); } else { vscode.window.showErrorMessage("Push function only available when using cloudshell.");