update: add local-proxy library

This commit is contained in:
2025-06-20 12:07:09 +08:00
parent 3c0cb64478
commit 574317a42d
14 changed files with 361 additions and 133 deletions

View File

@@ -0,0 +1,132 @@
import fs from 'node:fs';
// import { AssistantConfig, checkFileExists } from '@/module/assistant/index.ts';
import { checkFileExists } from './file.ts';
import path from 'node:path';
type AssistantConfig = {
configPath?: {
pagesDir?: string;
};
getCacheAssistantConfig?: () => {
watch?: {
enabled?: boolean;
};
};
};
type ProxyType = {
user: string;
key: string;
path: string;
indexPath: string;
absolutePath?: string;
};
export type LocalProxyOpts = {
watch?: boolean; // 是否监听文件变化
pagesDir?: string; // 前端应用路径
init?: boolean;
};
export class LocalProxy {
localProxyProxyList: ProxyType[] = [];
pagesDir: string;
watch?: boolean;
watching?: boolean;
initing?: boolean;
constructor(opts?: LocalProxyOpts) {
this.pagesDir = opts?.pagesDir || '';
this.watch = opts?.watch ?? false;
if (opts.init) {
this.init();
if (this.watch) {
this.onWatch();
}
}
}
initFromAssistantConfig(assistantConfig?: AssistantConfig) {
if (!assistantConfig) return;
this.pagesDir = assistantConfig.configPath?.pagesDir || '';
this.watch = !!assistantConfig.getCacheAssistantConfig()?.watch.enabled;
this.init();
if (this.watch) {
this.onWatch();
}
}
init() {
const frontAppDir = this.pagesDir;
if (frontAppDir) {
if (this.initing) {
return;
}
if (!checkFileExists(frontAppDir)) {
fs.mkdirSync(frontAppDir, { recursive: true });
}
this.initing = true;
const userList = fs.readdirSync(frontAppDir);
const localProxyProxyList: ProxyType[] = [];
userList.forEach((user) => {
const userPath = path.join(frontAppDir, user);
const stat = fs.statSync(userPath);
if (stat.isDirectory()) {
const appList = fs.readdirSync(userPath);
appList.forEach((app) => {
const appPath = path.join(userPath, app);
const indexPath = path.join(appPath, 'index.html');
if (!checkFileExists(indexPath, true)) {
return;
}
// const appPath = `${appPath}/index.html`;
if (checkFileExists(indexPath, true)) {
localProxyProxyList.push({
user: user,
key: app,
path: `/${user}/${app}/`,
indexPath: `${user}/${app}/index.html`,
absolutePath: appPath,
});
}
});
}
});
this.localProxyProxyList = localProxyProxyList;
this.initing = false;
}
}
onWatch() {
// 监听文件变化
const frontAppDir = this.pagesDir;
const that = this;
if (!this.watch && !frontAppDir) {
return;
}
if (this.watching) {
return;
}
that.watching = true;
let timer: NodeJS.Timeout;
const debounce = (fn: () => void, delay: number) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn();
}, delay);
};
fs.watch(frontAppDir, { recursive: true }, (eventType, filename) => {
if (eventType === 'rename' || eventType === 'change') {
const filePath = path.join(frontAppDir, filename);
try {
const stat = fs.statSync(filePath);
if (stat.isDirectory() || filename.endsWith('.html')) {
// 重新加载
debounce(that.init.bind(that), 5 * 1000);
}
} catch (error) {}
}
});
}
getLocalProxyList() {
return this.localProxyProxyList;
}
reload() {
this.init();
}
}