From a5168d33caaedddb0ece37f51ca196855e8e2c6d Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 22 May 2025 14:51:32 +0800 Subject: [PATCH] fix localproxy add watch --- assistant/package.json | 2 +- assistant/src/services/proxy/local-proxy.ts | 38 +++++++-------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/assistant/package.json b/assistant/package.json index 8941f99..a8d3d46 100644 --- a/assistant/package.json +++ b/assistant/package.json @@ -21,7 +21,7 @@ "scripts": { "dev": "bun run src/run.ts ", "dev:server": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/run-server.ts ", - "dev:share": "bun --watch src/test/remote-app.ts ", + "dev:share": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/test/remote-app.ts ", "build:lib": "bun run bun-lib.config.mjs", "postbuild:lib": "dts -i src/lib.ts -o assistant-lib.d.ts -d libs -t", "build": "rimraf dist && bun run bun.config.mjs", diff --git a/assistant/src/services/proxy/local-proxy.ts b/assistant/src/services/proxy/local-proxy.ts index 00667df..a5591f0 100644 --- a/assistant/src/services/proxy/local-proxy.ts +++ b/assistant/src/services/proxy/local-proxy.ts @@ -1,27 +1,7 @@ import fs from 'node:fs'; import { AssistantConfig, checkFileExists } from '@/module/assistant/index.ts'; import path from 'node:path'; - -export const localProxyProxyList = [ - { - user: 'root', - key: 'assistant-base-app', - path: '/root/assistant-base-app', - indexPath: 'root/assistant-base-app/index.html', - }, - { - user: 'root', - key: 'talkshow-admin', - path: '/root/talkshow-admin', - indexPath: 'root/talkshow-admin/index.html', - }, - { - user: 'root', - key: 'center', - path: '/root/center', - indexPath: 'root/center/index.html', - }, -]; +import { logger } from '@/module/logger.ts'; type ProxyType = { user: string; @@ -43,8 +23,12 @@ export class LocalProxy { constructor(opts?: LocalProxyOpts) { this.assistantConfig = opts?.assistantConfig; if (this.assistantConfig) { - this.watch = !!this.assistantConfig.config?.watch.enabled; + this.watch = !!this.assistantConfig.getCacheAssistantConfig()?.watch.enabled; this.init(); + console.log('init local proxy', this.assistantConfig.getAppList); + if (this.watch) { + this.onWatch(); + } } } init() { @@ -72,7 +56,7 @@ export class LocalProxy { localProxyProxyList.push({ user: user, key: app, - path: `/${user}/${app}`, + path: `/${user}/${app}/`, indexPath: `${user}/${app}/index.html`, absolutePath: appPath, }); @@ -102,15 +86,17 @@ export class LocalProxy { } timer = setTimeout(() => { fn(); + logger.info('reload local proxy'); }, delay); }; fs.watch(frontAppDir, { recursive: true }, (eventType, filename) => { - if (eventType === 'change') { + if (eventType === 'rename' || eventType === 'change') { const filePath = path.join(frontAppDir, filename); try { const stat = fs.statSync(filePath); - if (stat.isDirectory()) { - debounce(that.init, 5000); + if (stat.isDirectory() || filename.endsWith('.html')) { + // 重新加载 + debounce(that.init.bind(that), 5 * 1000); } } catch (error) {} }