fix localproxy add watch
This commit is contained in:
		| @@ -21,7 +21,7 @@ | |||||||
|   "scripts": { |   "scripts": { | ||||||
|     "dev": "bun run src/run.ts ", |     "dev": "bun run src/run.ts ", | ||||||
|     "dev:server": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0  bun --watch src/run-server.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", |     "build:lib": "bun run bun-lib.config.mjs", | ||||||
|     "postbuild:lib": "dts -i src/lib.ts -o assistant-lib.d.ts -d libs -t", |     "postbuild:lib": "dts -i src/lib.ts -o assistant-lib.d.ts -d libs -t", | ||||||
|     "build": "rimraf dist && bun run bun.config.mjs", |     "build": "rimraf dist && bun run bun.config.mjs", | ||||||
|   | |||||||
| @@ -1,27 +1,7 @@ | |||||||
| import fs from 'node:fs'; | import fs from 'node:fs'; | ||||||
| import { AssistantConfig, checkFileExists } from '@/module/assistant/index.ts'; | import { AssistantConfig, checkFileExists } from '@/module/assistant/index.ts'; | ||||||
| import path from 'node:path'; | import path from 'node:path'; | ||||||
|  | import { logger } from '@/module/logger.ts'; | ||||||
| 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', |  | ||||||
|   }, |  | ||||||
| ]; |  | ||||||
|  |  | ||||||
| type ProxyType = { | type ProxyType = { | ||||||
|   user: string; |   user: string; | ||||||
| @@ -43,8 +23,12 @@ export class LocalProxy { | |||||||
|   constructor(opts?: LocalProxyOpts) { |   constructor(opts?: LocalProxyOpts) { | ||||||
|     this.assistantConfig = opts?.assistantConfig; |     this.assistantConfig = opts?.assistantConfig; | ||||||
|     if (this.assistantConfig) { |     if (this.assistantConfig) { | ||||||
|       this.watch = !!this.assistantConfig.config?.watch.enabled; |       this.watch = !!this.assistantConfig.getCacheAssistantConfig()?.watch.enabled; | ||||||
|       this.init(); |       this.init(); | ||||||
|  |       console.log('init local proxy', this.assistantConfig.getAppList); | ||||||
|  |       if (this.watch) { | ||||||
|  |         this.onWatch(); | ||||||
|  |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   init() { |   init() { | ||||||
| @@ -72,7 +56,7 @@ export class LocalProxy { | |||||||
|               localProxyProxyList.push({ |               localProxyProxyList.push({ | ||||||
|                 user: user, |                 user: user, | ||||||
|                 key: app, |                 key: app, | ||||||
|                 path: `/${user}/${app}`, |                 path: `/${user}/${app}/`, | ||||||
|                 indexPath: `${user}/${app}/index.html`, |                 indexPath: `${user}/${app}/index.html`, | ||||||
|                 absolutePath: appPath, |                 absolutePath: appPath, | ||||||
|               }); |               }); | ||||||
| @@ -102,15 +86,17 @@ export class LocalProxy { | |||||||
|       } |       } | ||||||
|       timer = setTimeout(() => { |       timer = setTimeout(() => { | ||||||
|         fn(); |         fn(); | ||||||
|  |         logger.info('reload local proxy'); | ||||||
|       }, delay); |       }, delay); | ||||||
|     }; |     }; | ||||||
|     fs.watch(frontAppDir, { recursive: true }, (eventType, filename) => { |     fs.watch(frontAppDir, { recursive: true }, (eventType, filename) => { | ||||||
|       if (eventType === 'change') { |       if (eventType === 'rename' || eventType === 'change') { | ||||||
|         const filePath = path.join(frontAppDir, filename); |         const filePath = path.join(frontAppDir, filename); | ||||||
|         try { |         try { | ||||||
|           const stat = fs.statSync(filePath); |           const stat = fs.statSync(filePath); | ||||||
|           if (stat.isDirectory()) { |           if (stat.isDirectory() || filename.endsWith('.html')) { | ||||||
|             debounce(that.init, 5000); |             // 重新加载 | ||||||
|  |             debounce(that.init.bind(that), 5 * 1000); | ||||||
|           } |           } | ||||||
|         } catch (error) {} |         } catch (error) {} | ||||||
|       } |       } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user