"feat: 新增远程应用测试脚本,升级 pm2 依赖"

This commit is contained in:
2025-05-22 13:19:57 +08:00
parent 1c2aa26dd0
commit 1e340ec2b3
8 changed files with 415 additions and 259 deletions

View File

@@ -32,19 +32,28 @@ type ProxyType = {
};
export type LocalProxyOpts = {
assistantConfig?: AssistantConfig; // 前端应用路径
// watch?: boolean; // 是否监听文件变化
};
export class LocalProxy {
localProxyProxyList: ProxyType[] = [];
assistantConfig?: AssistantConfig;
watch?: boolean;
watching?: boolean;
initing?: boolean;
constructor(opts?: LocalProxyOpts) {
this.assistantConfig = opts?.assistantConfig;
if (this.assistantConfig) {
this.watch = !!this.assistantConfig.config?.watch.enabled;
this.init();
}
}
init() {
const frontAppDir = this.assistantConfig.configPath?.pagesDir;
if (frontAppDir) {
if (this.initing) {
return;
}
this.initing = true;
const userList = fs.readdirSync(frontAppDir);
const localProxyProxyList: ProxyType[] = [];
userList.forEach((user) => {
@@ -72,12 +81,45 @@ export class LocalProxy {
}
});
this.localProxyProxyList = localProxyProxyList;
this.initing = false;
}
}
onWatch() {
// 监听文件变化
const frontAppDir = this.assistantConfig.configPath?.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 === 'change') {
const filePath = path.join(frontAppDir, filename);
try {
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
debounce(that.init, 5000);
}
} catch (error) {}
}
});
}
getLocalProxyList() {
return this.localProxyProxyList;
}
reload() {
// 重新加载本地代理列表
this.init();
}
}