diff --git a/src/extension.ts b/src/extension.ts index b65a589..51d429e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -47,13 +47,8 @@ async function launch(context: vscode.ExtensionContext) { }); - await vscode.window.withProgress({ - location: vscode.ProgressLocation.Window, - title: t('progress.active-lsp-server') - }, async (progress: vscode.Progress, token: vscode.CancellationToken) => { - await lspClient.activate(context, progress, versionString); - await LspClient.DigitalIDE?.onReady(); - }); + await lspClient.activate(context, versionString); + await LspClient.DigitalIDE?.onReady(); await vscode.window.withProgress({ location: vscode.ProgressLocation.Window, diff --git a/src/function/lsp-client/index.ts b/src/function/lsp-client/index.ts index 31e352a..86035be 100644 --- a/src/function/lsp-client/index.ts +++ b/src/function/lsp-client/index.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import * as fs from 'fs'; import { platform } from "os"; import { IProgress, LspClient } from '../../global'; -import axios from "axios"; +import axios, { AxiosResponse } from "axios"; import { getGiteeDownloadLink, getPlatformPlatformSignature } from "./cdn"; @@ -23,7 +23,7 @@ function getLspServerExecutionName() { return 'digital-lsp'; } -async function checkAndDownload(context: vscode.ExtensionContext, progress: vscode.Progress, version: string): boolean { +async function checkAndDownload(context: vscode.ExtensionContext, version: string): boolean { const { t } = vscode.l10n; const versionFolderPath = context.asAbsolutePath( @@ -35,22 +35,47 @@ async function checkAndDownload(context: vscode.ExtensionContext, progress: vsco return true; } - // 流式下载 - progress.report({ message: t('progress.download-digital-lsp') }); - const signature = getPlatformPlatformSignature().toString(); - const giteeDownloadLink = getGiteeDownloadLink(signature, version) - console.log('download link ', giteeDownloadLink); - - const response = await axios.get(giteeDownloadLink, { responseType: 'stream' }); - const totalLength = response.headers['content-length']; - console.log('totalLength ', totalLength); + await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: t('progress.download-digital-lsp') + }, async (progress: vscode.Progress, token: vscode.CancellationToken) => { + progress.report({ increment: 0 }); + const signature = getPlatformPlatformSignature().toString(); + const downloadLink = getGiteeDownloadLink(signature, version); + const response = await axios.get(downloadLink, { responseType: 'stream' }); + + }); + + function streamDownload(progress: vscode.Progress, response: AxiosResponse): Promise { + const totalLength = response.headers['content-length']; + const totalSize = parseInt(totalLength); + let downloadSize = 0; + const savePath = context.asAbsolutePath( + path.join('resources', 'dide-lsp', 'server', 'tmp.tar.gz') + ); + const fileStream = fs.createWriteStream(savePath); + + response.data.on('data', (chunk: Buffer) => { + downloadSize += chunk.length; + let increment = Math.ceil(downloadSize / totalSize * 100); + progress.report({ message: t('progress.download-digital-lsp'), increment }); + }); + return new Promise((resolve, reject) => { + fileStream.on('finish', () => { + resolve(); + }); + fileStream.on('error', (error) => { + reject(error); + }); + }); + } return false; } -export async function activate(context: vscode.ExtensionContext, progress: vscode.Progress, version: string) { - await checkAndDownload(context, progress, version); +export async function activate(context: vscode.ExtensionContext, version: string) { + await checkAndDownload(context, version); const lspServerName = getLspServerExecutionName();