Files
kenyanghui 171003556d aiinvite2.1: 拆约(原邀悦圈)小程序 + Express/SQLite 后端
- wechat-miniprogram: 原生小程序,品牌化拆约+玄龙logo,API 指向 https://xhermes.aibay.asia
- yaoyue-sever: Express 单文件 server.js + better-sqlite3,GLM glm-5.3-flash(thinking disabled)
- 排除 node_modules/.env/*.db
2026-11-11 23:25:28 +08:00

96 lines
3.1 KiB
JavaScript

// test/auto-connect.js
// 自动启动微信开发者工具自动化并检测端口连接
// 用法: node test/auto-connect.js
// 解决 DevTools 忽略 --auto-port 导致随机端口的问题
const automator = require('miniprogram-automator');
const { spawn } = require('child_process');
const { execSync } = require('child_process');
const CLI_NODE = 'E:/微信web开发者工具/node.exe';
const CLI_JS = 'E:/微信web开发者工具/cli.js';
const PROJECT = 'D:/codeProject/邀约-ClaudeCode开发/yao-yue-app/wechat-miniprogram';
function getPorts() {
const out = execSync('netstat -ano', { encoding: 'utf8', windowsHide: true });
const ports = new Set();
for (const line of out.split('\n')) {
if (line.includes('LISTENING') && line.includes('127.0.0.1')) {
const m = line.match(/127\.0\.0\.1:(\d+)/);
if (m) ports.add(parseInt(m[1]));
}
}
return ports;
}
async function main() {
// 1. 关闭现有项目
console.log('[1/4] 关闭已有项目...');
execSync(`"${CLI_NODE}" "${CLI_JS}" close --project "${PROJECT}"`, {
encoding: 'utf8', windowsHide: true, timeout: 15000,
});
const beforePorts = getPorts();
// 2. 用 spawn (非阻塞) 启动 auto
console.log('[2/4] 启动自动化...');
const proc = spawn(CLI_NODE, [CLI_JS, 'auto', '--project', PROJECT], {
windowsHide: true,
stdio: 'ignore',
});
// 3. 检测新端口(最多等 60 秒)
console.log('[3/4] 检测自动化端口...');
let mp = null;
for (let i = 0; i < 60; i++) {
await new Promise(r => setTimeout(r, 1000));
const afterPorts = getPorts();
for (const port of afterPorts) {
if (!beforePorts.has(port) && port > 10000) {
try {
mp = await automator.connect({ wsEndpoint: `ws://127.0.0.1:${port}` });
console.log(`[4/4] ✅ 已连接! 端口=${port}`);
console.log(`AUTO_PORT=${port}`);
return mp;
} catch (e) { /* 不是这个端口 */ }
}
}
if (i % 5 === 0) process.stdout.write(` ${i}s...`);
}
console.error('\n❌ 超时:60秒内未找到自动化端口');
process.exit(1);
}
// 启动后保持连接,让调用方继续使用
main().then(mp => {
// 将 miniProgram 实例挂到全局,供后续使用
global.__automator_miniprogram = mp;
// 如果直接运行此脚本,做一些基础验证
if (require.main === module) {
(async () => {
await mp.waitFor(5000);
const sys = await mp.systemInfo();
console.log(`系统: ${sys.platform}, 微信: ${sys.version}`);
const page = await mp.currentPage();
console.log(`当前页面: ${page.path}`);
// 截图验证
const fs = require('fs');
const dir = 'D:/codeProject/邀约-ClaudeCode开发/yao-yue-app/wechat-miniprogram/test/screenshots';
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
await mp.screenshot({ path: `${dir}/auto-connect-verify.png` });
console.log('截图已保存');
await mp.disconnect();
console.log('测试通过,已断开连接');
process.exit(0);
})();
}
}).catch(err => {
console.error('启动失败:', err.message);
process.exit(1);
});