From 1b3130ad6c2ec52287bbaac9abf2f199d10e6015 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Fri, 11 Apr 2025 00:05:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ps1=20=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build_service.ps1 | 23 +++++++++++++++++++ src/extension.ts | 57 +++++++++++++++++++---------------------------- tsconfig.json | 1 + 3 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 build_service.ps1 diff --git a/build_service.ps1 b/build_service.ps1 new file mode 100644 index 0000000..51a3e4d --- /dev/null +++ b/build_service.ps1 @@ -0,0 +1,23 @@ +# Create resources directory if it doesn't exist +New-Item -ItemType Directory -Force -Path .\resources | Out-Null + +# Start both build tasks in parallel +$jobs = @( + Start-Job -ScriptBlock { + Set-Location $using:PWD\renderer + npm run build + Move-Item -Force -Path .\dist -Destination ..\resources\renderer + } + Start-Job -ScriptBlock { + Set-Location $using:PWD\service + npm run build + Move-Item -Force -Path .\dist -Destination ..\resources\service + } +) + +# Wait for all jobs to complete +Wait-Job -Job $jobs | Out-Null +Receive-Job -Job $jobs +Remove-Job -Job $jobs + +Write-Host "构建完成,dist文件已移动到resources目录" \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 2dee1e9..2f8c3f2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,7 +3,7 @@ import * as path from 'path'; import * as fs from 'fs'; import * as fspath from 'path'; -import OpenMCPService from '../resources/service'; +import * as OpenMCPService from '../resources/service'; function getWebviewContent(context: vscode.ExtensionContext, panel?: vscode.WebviewPanel): string | undefined { const viewRoot = fspath.join(context.extensionPath, 'resources', 'renderer'); @@ -23,13 +23,6 @@ export function activate(context: vscode.ExtensionContext) { // 注册 showOpenMCP 命令 context.subscriptions.push( vscode.commands.registerCommand('openmcp.showOpenMCP', async () => { - const htmlPath = path.join(context.extensionPath, 'resources', 'renderer', 'index.html'); - - if (!fs.existsSync(htmlPath)) { - vscode.window.showErrorMessage('未找到 index.html 文件'); - return; - } - const panel = vscode.window.createWebviewPanel( 'openmcpView', 'OpenMCP', @@ -40,8 +33,7 @@ export function activate(context: vscode.ExtensionContext) { } ); - const htmlContent = fs.readFileSync(htmlPath, 'utf8'); - panel.webview.html = htmlContent; + initaliseWebview(context, panel.webview); }) ); @@ -52,6 +44,23 @@ export function activate(context: vscode.ExtensionContext) { ); } +function initaliseWebview(context: vscode.ExtensionContext, webview: vscode.Webview) { + webview.options = { + enableScripts: true, + }; + + // 设置HTML内容 + const html = getWebviewContent(context); + webview.html = html || ''; + + // 处理来自webview的消息 + webview.onDidReceiveMessage(message => { + const { command, data } = message; + + OpenMCPService.messageController(command, data, webview as any); + }); +} + class WebviewViewProvider implements vscode.WebviewViewProvider { private _view?: vscode.WebviewView; @@ -63,30 +72,10 @@ class WebviewViewProvider implements vscode.WebviewViewProvider { _token: vscode.CancellationToken, ) { this._view = webviewView; - webviewView.webview.options = { - enableScripts: true, - }; - - // 设置HTML内容 - const html = getWebviewContent(this.context); - webviewView.webview.html = html || ''; - - // 处理来自webview的消息 - webviewView.webview.onDidReceiveMessage(message => { - const { command, data } = message; - - OpenMCPService.messageController(command, data, webviewView.webview as any); - }); - - // 向webview发送消息的示例 - this.sendMessageToWebview({ command: 'init', data: 'Hello from extension' }); - } - - private sendMessageToWebview(message: any) { - if (this._view) { - this._view.webview.postMessage(message); - } + initaliseWebview(this.context, webviewView.webview); } } -export function deactivate() {} \ No newline at end of file +export function deactivate() { + +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index cb35375..aada78e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ ], "sourceMap": true, "rootDir": "src", + "strict": true, /* enable all strict type-checking options */ /* Additional Checks */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */