@honor-minigame/cli
Version:
honor minigame pack cli
45 lines (41 loc) • 1.88 kB
JavaScript
import { execSync } from 'child_process'
import { openBrowser } from './openBrowser.js'
async function rpkDebugRouter(context, next) {
// 查看连接的设备的ip 与 request中的ip 是否一致
try{
// 检查设备连接
const devices = execSync('adb devices').toString();
const lines = devices.trim().split(/\r?\n/);
const connectedDevices = lines.filter(line => line.includes('\tdevice'));
if (connectedDevices.length === 0) {
console.log('#### ⚠️ 未检测到设备连接,无法自动拉起浏览器调试, 正在下载RPK, 请等待! ####');
}else{
connectedDevices.forEach((deviceLine, index) => {
const deviceId = deviceLine.split('\t')[0];
// console.log(`\n设备 ${index + 1}: ${deviceId}`);
try {
const ipOutput = execSync(`adb -s ${deviceId} shell ip addr show wlan0 2>nul || echo 无wlan0接口`).toString();
const ipMatch = ipOutput.match(/inet (\d+\.\d+\.\d+\.\d+)\//);
console.log(`####设备 ${index + 1}: ${deviceId} -- IP: ${ipMatch[1]} ####`)
if(ipMatch[1] === context.ip.replace('::ffff:','')){
setTimeout(() => {
execSync('adb forward tcp:5086 tcp:5086');
const url = 'devtools://devtools/bundled/inspector.html?v8only=true&ws=127.0.0.1:5086/00010002-0003-4004-8005-000600070008&panel=console'
openBrowser(url)
}, 1000)
}else{
console.error('#### ❌ 扫码设备未连接到当前电脑,请检查网络或设备是否连接正常! ####');
}
} catch (error) {
console.error('#### ❌ ip获取失败: ', error);
}
});
}
}catch(e){
console.error('#### ❌ 拉起浏览器失败: ', e);
}
await next();
}
export {
rpkDebugRouter
}